Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket connection over internet in Python?

I have created a basic client server socket program in Python 2.7.x and it is running absolutely fine over the same network even on different machines but when I run server and client on different networks(server on my friend's network while client on mine) it does not return any error and keeps on waiting. I just can't understand how to debug the code. I am using port 80 by killing all the services on port 80. I have also done port forwarding on port 80 on both the machines.

My codes is as follows:

client.py

import socket              

s = socket.socket()        
host = '103.47.59.130' 
port = 80               

s.connect((host, port))
while True: 
    print "From Server: ", s.recv(1024)  #This gets printed after sometime
    s.send(raw_input("Client please type: "))

s.close()                     

server.py

import socket               

s = socket.socket()         # Create a socket object
host = '192.168.0.104'    #private ip address of machine running fedora
port = 80                
s.bind((host, port))       

s.listen(5)                
c, addr = s.accept()       
print 'Got connection from', addr    #this line never gets printed
while True:
   c.send(raw_input("Server please type: "))
   print "From Client: ", c.recv(1024)

c.close()                

It sometimes output **From Server: ** but doesnot send any message back and forth.

PS: I have searched on Stack Overflow earlier but I am unable to find anything relevant.

like image 354
Sanyam Jain Avatar asked Feb 13 '16 19:02

Sanyam Jain


3 Answers

Use this software to implement port-forwarding. I recommend you use another port for your server, say 5006, to prevent any problems related to using a very commonly used port like 80. Basically, the software works like this:

  • You click Connect, and it searches for routers and if it finds yours, it lists the existing port mappings.
  • You create a port mapping (on the right), default protocol is TCP
  • You select a port on your router, say 5001 (called the External port)
  • You select a port on your server, maybe 5006 (called the Internal port)
  • The router will then be instructed to forward all data that arrives at port 5001 to your server, using your private IP, specifically to port 5006 on your server.

So all your client has to do is send data to the public IP of your server, specifically to port 5001. This data will of course first arrive at your router, which will behave as configured and forward everything to your server's port 5006. All this only works if your internet gateway supports port forwarding.

Client:

import socket              

s = socket.socket()        
host = '103.47.59.130' 
port = 5001               

s.connect((host, port))
while True: 
    try:
        print "From Server: ", s.recv(1024) 
        s.send(raw_input("Client please type: "))
    except:
        break
s.close()

Server:

import socket               

s = socket.socket()         # Create a socket object
host = '192.168.0.104'    #private ip address of machine running fedora
port = 5006                
s.bind((host, port))       

s.listen(5)                
c, addr = s.accept()       
print 'Got connection from', addr    
while True:
   c.send(raw_input("Server please type: "))
   print "From Client: ", c.recv(1024)

c.close()
like image 115
SoreDakeNoKoto Avatar answered Sep 28 '22 22:09

SoreDakeNoKoto


You need to use your public IP address or 0.0.0.0 in your server, and make sure your modem/router allows incoming connections at the specified port (usually called Port Forwarding).

Hope it helps!

like image 32
cdonts Avatar answered Sep 28 '22 23:09

cdonts


Seems like you need to perform basic network troubleshooting. Your description says you can connect to other machines on your own network, but not a machine on another network. You might try the same sorts of tests on the machine on the other network: can it connect to other machines on its own network, can it connect to other machines off of its network.

Some tools which are very valuable include ping, tracepath, tcpdump, and nc (netcat).

Ultimately, if you can make the connection with netcat, you can assume the problem is with your code, but if you can't you can try and find the networking problem.

like image 29
dan4thewin Avatar answered Sep 28 '22 23:09

dan4thewin