knshen
knshen

Reputation: 45

How can I use Mininet Python API to send UDP packets from one host to another?

Firstly,in SingleSwitchTopo.py, I set up a network containing 2 hosts and 1 switch. Ping and iperf between h1 and h2 are OK. Then I let h1 runs server.py, it serves as a server. h2 runs client.py, it sends UDP packets to h1, then h1 will receive data and write to a file. But why can't h1 get data from h2? How to do this correctly?

SingleSwitchTopo.py

from mininet.topo import Topo
from mininet.net import Mininet
from mininet.util import dumpNodeConnections
from mininet.log import setLogLevel
from mininet.node import CPULimitedHost
from mininet.link import TCLink

class SingleSwitchTopo(Topo):
    "Single switch connected to n hosts."
    def build(self, n=2):
        switch = self.addSwitch('s1')
        for h in range(n):    
            host = self.addHost('h%s' % (h + 1), cpu=.6/n)     
            self.addLink(host, switch, bw=500, delay='10ms', loss=10, max_queue_size=100, use_htb=True)

def simpleTest():
    "Create and test a simple network"
    topo = SingleSwitchTopo(n=2)
    net = Mininet(topo=topo, host=CPULimitedHost, link=TCLink)
    net.start()
    h1 = net.get('h1')
    h2 = net.get('h2')
    h1.cmd('kill %python')
    h2.cmd('kill %python')
    h1.cmd('python server.py &')
    h2.cmd('python client.py %s ' % h1.IP())
    net.stop()

if __name__ == '__main__':
    setLogLevel('info')
    simpleTest()

server.py

import socket
address = ('127.0.0.1', 9999)
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(address)
f = open('/home/knshen/a.txt', 'w+')

while True:
    data, addr = s.recvfrom(1024)
    print 'data', data
    f.write(data)
    f.flush()
f.close()
s.close()

client.py

import socket
import sys
from time import sleep

address = (sys.argv[1], 9999)
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
i = 1
while True:
    s.sendto('hi : %d\n' % i, address)
    i += 1
    sleep(3)

s.close()

Upvotes: 1

Views: 7706

Answers (2)

Lorenzo Barbagli
Lorenzo Barbagli

Reputation: 1281

I know it's a late answer, here there's a simple but effective solution.

Many thanks to Brian O'Connor for the code.

Upvotes: 1

SH151
SH151

Reputation: 136

You can set up a client server using iperf commands.

Start xterms for both server(h1) and client (h2) by using

xterm h1 h2

Then in h1 run

iperf -s -u -i 1 

to start a udp server which sends packets with an interval of 1 second

Then in h2 run

iperf -c 10.0.0.1 -u -b 1m -n 1000 

which creates a udp client that connects to h1 at address 10.0.0.1, bandwidth = 1M, number of bytes to transport = 1000

Here is a link showing various iperf parameters/features http://openmaniak.com/iperf.php

Upvotes: 3

Related Questions