Python: sending data between two computers via sockets

28,129

This does probably not have to do with your code which looks okay. I rather think that this is a problem with the IP addresses that you're using.

If the computers are on different networks you need to make sure that the IP address that you're passing is the one accessible to the net. Basically what this means is that if the IP you're using starts with 192.168.?.? then you're using the wrong IP.

You can easily check this by running the command:
(windows): ipconfig
(linux): ifconfig

If you're using a correct IP address then I'd check my router settings and/or firewall settings which may very well block the port number that you're trying to use.

Share:
28,129
Piotr Dabkowski
Author by

Piotr Dabkowski

Check out my JavaScript to Python translator: https://github.com/PiotrDabkowski/Js2Py Full implementation of ECMA 5.1 in 100% pure Python.

Updated on February 09, 2020

Comments

  • Piotr Dabkowski
    Piotr Dabkowski about 4 years

    I am working on a script that would transmit the data between two distinct computers with access to the internet. I am using python's socket standard module. It works fine when I run both client and server on single computer but I am not able to make the things work when they run on different computers.

    Here is a part of my server code:

    import socket, time,os, random
    
    class Server():
      def __init__(self,Adress=('',5000),MaxClient=1):
          self.s = socket.socket()
          self.s.bind(Adress)
          self.s.listen(MaxClient)
      def WaitForConnection(self):
          self.Client, self.Adr=(self.s.accept())
          print('Got a connection from: '+str(self.Client)+'.')
    
    
    s = Server()
    s.WaitForConnection()
    

    And here is a part of my client code:

    import socket
    
    class Client():
       def __init__(self,Adress=("Here is the IP of the computer on which the \
                                       server scrip is running",5000)):
          self.s = socket.socket()
          self.s.connect(Adress)
    
    c = Client()
    

    When I run these scripts on two different computers with internet access the client is unable to connect and raises an error and the server is waiting for connections forever.

    What am I doing wrong?

    • Nikolai Fetissov
      Nikolai Fetissov over 11 years
      How are these two computers are connected? Any routers/firewalls/NAT devices on the path.
    • Piotr Dabkowski
      Piotr Dabkowski over 11 years
      Both computers are connected to different WiFi's
    • Nikolai Fetissov
      Nikolai Fetissov over 11 years
      How these two wifi routers are configured?
    • Piotr Dabkowski
      Piotr Dabkowski over 11 years
      I dont really know. So you mean that my code should run fine?
    • Andreas Florath
      Andreas Florath over 11 years
      Your code is fine: IMHO there is some other problem on the network layer.
    • l4mpi
      l4mpi over 11 years
      Your problem is probably that you don't have proper port forwarding between the NAT router and the server, or a similar network configuration error. On another note, why are you using socket directly when you could just use the excellent built-in SocketServer library?
  • l4mpi
    l4mpi over 11 years
    I'm pretty sure it's a configuration issue because the router would need to forward the port to the lan address of the server...
  • Piotr Dabkowski
    Piotr Dabkowski over 11 years
    Problem solved. It was a problem with a router that was now redirecting the data packet to the computer where the server was set up. I have changed it in the router settings and made it to redirect the packet to the private IP of this computer (192.168.1.35) and port 5000. Now it works fine. Thank you very much :)