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.
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:
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()
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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With