Python Socket Server/Client Error



  • I have installed Python 3 on my Omega2+ board.

    I'm trying to use the Python Socket library to create a simple socket communication server/client setup between my Omega2+(server) and Windows laptop(client).

    I have been using the example code given here:
    Server:

    # first of all import the socket library 
    import socket			 
    
    # next create a socket object 
    s = socket.socket()	 
    print ("Socket successfully created") 
    
    # reserve a port on your computer in our 
    # case it is 12345 but it can be anything 
    port = 12345				
    
    # Next bind to the port 
    # we have not typed any ip in the ip field 
    # instead we have inputted an empty string 
    # this makes the server listen to requests 
    # coming from other computers on the network 
    s.bind(('', port))	 
    print ("socket binded to %s" %(port)) 
    
    # put the socket into listening mode 
    s.listen(5) 
    print ("socket is listening")		 
    
    # a forever loop until we interrupt it or 
    # an error occurs 
    while True: 
    
      # Establish connection with client. 
      c, addr = s.accept()	 
      print ('Got connection from', addr ) 
    
      # send a thank you message to the client. 
      c.send('Thank you for connecting') 
    
      # Close the connection with the client 
      c.close()
    

    Client:

    # Import socket module 
    import socket			 
    
    # Create a socket object 
    s = socket.socket()	 
    
    # Define the port on which you want to connect 
    port = 12345				
    
    # connect to the server on local computer 
    s.connect(('127.0.0.1', port)) 
    
    # receive data from the server 
    print (s.recv(1024) ) 
    # close the connection 
    s.close()
    

    I run the server script first followed by the client script but each returns a different error:

    Server Error:

    File "server.py", line 17, in <module>
        s.bind(('', port))
    OSError: [Errno 125] Address in use
    

    Client Error:

    File "client.py", line 11, in <module>
        s.connect(('127.0.0.1', port))
    ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
    

    For the server error, I read on some other forums that suggest killing other processes that use this port. But none of the terminal commands to kill processes seem to work with the Omega2+.

    Another suggestion is to change the port number. I've tried this but I seem to get the same error.

    I was hoping someone would have encountered similar problems and would be aware of some sort of solution.



  • @jonty I would suggest you use a specific ip address and a sensible port number to listen on.

    I would use 127.0.0.1 and port 9090, by doing so you know exactly which ip you are listening on.

    What terminal commands are you trying to use to kill processes?



  • @crispyoz

    I made the changes you recommended.
    When I run the Server script, I get the following output:

    Socket successfully created
    socket binded to 9090
    socket is listening
    

    But I keep getting the previous error (WinError 10061) as mentioned earlier with the Client script.

    I was trying to view the list of open processes using the lsof command, but that doesn't work. So I'm not even sure about which process to kill.



  • @jonty So your server is now bound correctly, so now look at the client. Did you update the client code to use the specific port 9090?

    lsof is not installed by default, you need to install it:

    opkg install lsof

    If this fails then you need to update your distfeeds. Edit /etc/opkg/distfeeds.conf uncomment lines 2 and 5. Run opkg update; opkg install lsof

    But since your server will now bind correctly you don't need lsof



  • @crispyoz said in Python Socket Server/Client Error:

    Did you update the client code to use the specific port 9090?

    Even after updating the client code to use the specific port 9090, it returns the same WinError10061

    # Import socket module 
    import socket			 
    
    # Create a socket object 
    s = socket.socket()	 
    
    # Define the port on which you want to connect 
    port = 9090				
    
    # connect to the server on local computer 
    s.connect(('127.0.0.1', port)) 
    
    # receive data from the server 
    print (s.recv(1024) ) 
    # close the connection 
    s.close()
    
    python client.py
    Traceback (most recent call last):
      File "client.py", line 11, in <module>
        s.connect(('127.0.0.1', port))
    ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
    


  • @jonty Sorry I missed part of your original post where you mentioned you are connecting from a windows machine.

    127.0.0.1 is a local only ip which means while your omega can listen on that IP no other machine can connect to it, so instead you need to bind to the public ip on the omega. I assume you are connecting via wifi, so type ifconfig on the omega and find the apcli0 interface and record the IP, probably something like 192.168.x.x or 10.x.x.x. Now change your server code to bind to that IP. On the client machine connect to that IP.

    If the client on your windows machine still won't connect you may need to check your windows firewall is not blocking the port.



  • @crispyoz Thanks a lot. Your solution worked.

    Since the Server contained a while() loop and never closed the connection if I terminated( CTRL+Z ) the python program, it would produce this error, when I run the Server again:

    File "server.py", line 17, in <module>
        s.bind(('', port))
    OSError: [Errno 125] Address in use
    

    The fix is to lookup the process that's still using the port
    lsof - Gives a list of active processes with their PID numbers.
    kill -9 <PID> - Will kill the process.



  • @jonty instead of lsof you can us ps command that doesn't require anything to be installed.



  • @jonty The reason why you have to kill the process is because you are not exiting gracefully. You should capture SIGINT and close the port, or there is a KeyboardInterrupt exception you could capture and close the port.

    Since this is not a python forum and your underlying issue is not specific to Omega or OpenWRT I'll leave you to look into this further.



Looks like your connection to Community was lost, please wait while we try to reconnect.