首先,在SingleSwitchTopo.py中,我建立了一個包含2臺主機和1臺交換機的網絡。 ping和h1和h2之間的iperf都可以。然後我讓h1運行server.py,它充當服務器。 h2運行client.py,它將UDP數據包發送給h1,然後h1將接收數據並寫入文件。但爲什麼不能H1從H2獲取數據?如何正確執行此操作?如何使用Mininet Python API將UDP數據包從一臺主機發送到另一臺主機?
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()