intrig-unicamp / mininet-wifi

Emulator for Software-Defined Wireless Networks
https://mn-wifi.readthedocs.io/
Other
454 stars 244 forks source link

Running emulation multiple times: Socket / Address already in use #264

Closed Diggensag closed 5 years ago

Diggensag commented 5 years ago

Hi Ramon,

is there a given time limit that I have to wait until I can run a emulation again (regarding reading the params via socket)? In the first run, mnwifi gets the appropriate socket and listens for connections. When I close the emulation and start it again, then it's still blocked, therefore no nodes can get the params via socket.

I've been able to reproduce this with socket_server.py.

root@Xonster:~/GITs/mininet-wifi/examples# ./socket_server.py                                                                                                           
*** Creating nodes                                                                                                                                                      
*** Configuring wifi nodes
*** Creating links
*** Starting network
*** Configuring nodes
*** Running CLI
*** Starting CLI:
mininet-wifi> sta1 python2 /somedir/getgeocoords.py
Received from server: [30.0, 60.0, 0.0]
mininet-wifi> exit
*** Stopping network
*** Stopping 0 controllers

*** Stopping 4 links
.....
*** Stopping switches/access points
ap1 
*** Stopping nodes
h1 nat0 sta1 sta2 

*** Removing WiFi module and Configurations

*** Done
Killed
root@Xonster:~/GITs/mininet-wifi/examples# ./socket_server.py 
*** Creating nodes
Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
    self.run()
  File "/usr/lib/python2.7/threading.py", line 754, in run
    self.__target(*self.__args, **self.__kwargs)
  File "/usr/local/lib/python2.7/dist-packages/mininet_wifi-2.4.1-py2.7.egg/mn_wifi/net.py", line 198, in start_socket
    s.bind((host, port))
  File "/usr/lib/python2.7/socket.py", line 228, in meth
    return getattr(self._sock,name)(*args)
error: [Errno 98] Address already in use

*** Configuring wifi nodes
*** Creating links
*** Starting network
*** Configuring nodes
*** Running CLI
*** Starting CLI:

Is this fixable or has it a certain timeout that I have to wait?

Backround: I have to run my emulations n-times, to be able to create median values and so on ;-)

ramonfontes commented 5 years ago

It seems that the TCP port is still running when you start the script. Did you try to kill its process?

Diggensag commented 5 years ago

Wow, you're fast.

Yes, i think so too. But after the first time I exit mnwifi I couldn't find a process (ps -fax | grep socket_server, usually this shows the process) and also not hints in netstat.

The second time (the time when the socket is already occupied) and I exit mnwifi, then it isn't able to stop and I have to kill it (then there is a process left).

ramonfontes commented 5 years ago

Weird. Is there a way in which I could reproduce such behaviour?

Diggensag commented 5 years ago

I used the socket_server.py example with a modified socket_client.py script accessible from the stations via the call 'sta1 python2 /somedir/getgeocoords.py'

getcoords.py:

import socket

def client():
    host = 'yourhostip'
    port = 12345  # Make sure it's within the > 1024 $$ <65535 range
    s = socket.socket()
    s.connect((host, port))
    message="get.sta1.position"
    s.send(str(message).encode('utf-8'))
    data = s.recv(1024).decode('utf-8')
    print('Received from server: ' + data)
    s.close()

if __name__ == '__main__':
    client()

and then just ran socket_server.py it immediately after the first run had finished.

ramonfontes commented 5 years ago

Try https://github.com/intrig-unicamp/mininet-wifi/commit/2e957e89e29c3f70e7f28bc2f425568313bac710 and let me know whether it solves the problem.

Diggensag commented 5 years ago

Awesome, small line, huge impact.

Looks good on my socket_server.py example. I will close this issue for now :-)