10

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.

Sanyam Jain
  • 113
  • 1
  • 1
  • 9

4 Answers4

7

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()
SoreDakeNoKoto
  • 1,175
  • 1
  • 9
  • 16
  • By default, 5001 external will not go through to 5001 internal? But if you map the port you can make other ports go to other internal ports? – Luke Jul 12 '16 at 11:33
  • @Luke Exactly. Your external ports dont map to internal ones, by default. You can make any external port map to any internal one; be sure to avoid ports below 1000. – SoreDakeNoKoto Jul 12 '16 at 11:38
  • True, those ports are used by some services. I also found something hogging port 9000 which I thought was strange. – Luke Jul 13 '16 at 11:42
  • If I know the remote IP address why should I use port forwarding? – Prashant Nov 19 '20 at 18:47
  • @Prashant Not exactly sure what you mean by remote IP address here. If the server you're trying to reach is outside your local network and behind NAT, then: (1) trying to reach it by its private/local address (usually 192.168.x.x.) is no use, since that address is...well, private. For local networks only, not used on the open web. (2) trying to reach it by its public (a.k.a. router) address is also no use, since you'd merely be addressing the server's router who has no idea to which local device it should route this totally-unknown incoming connection. – SoreDakeNoKoto Nov 21 '20 at 20:26
  • And this latter point is why you need port forwarding. To instruct the router, to direct all traffic arriving at one of its own ports to a specific port on your server. – SoreDakeNoKoto Nov 21 '20 at 20:30
  • @SoreDakeNoKoto Suppose I know the remote/server address is 25.8.69.8, why should I need port forwarding? – Prashant Dec 14 '20 at 15:22
  • @Prashant Like I said, if that address is public (and yours looks like it), then you don't need port forwarding. Simply put, if you can connect successfully to specific ports on that remote server from any device across the web (without any prior port forwarding), then you clearly don't need port forwarding -- at least for the actual functionality. Security is a different matter; it may not be the wisest thing to expose your server directly to the web that way, at least not without a decent firewall, but I'm no expert there. – SoreDakeNoKoto Dec 16 '20 at 18:48
2

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!

cdonts
  • 9,304
  • 4
  • 46
  • 72
  • Do I need to add public ip address in the server code? The IP address in the client.py is public only. – Sanyam Jain Feb 13 '16 at 19:40
  • @SanyamJain Yes. Using `192.168.0.104` will just listen for LAN connections. Use your public IP address or `0.0.0.0`. – cdonts Feb 13 '16 at 19:41
  • The server should use `0.0.0.0` so that the code can run on many machines, but if `192.168.0.104` is in fact the server machine's address, it still works. The server is behind a NAT and as pointed out here, you need configure port-forwarding on the NAT to send port 80 to `192.168.0.104`. Your friend's ISP may block incoming connection requests or maybe just 80 specifically, so experiment with others. – tdelaney Feb 13 '16 at 19:52
  • I have tried using 0.0.0.0 but Server still waiting for client to connect. I have change the port to 8009 also, do I need to do something else? – Sanyam Jain Feb 14 '16 at 07:57
  • @SanyamJain Did you open the port 8009 in your modem settings? – cdonts Feb 14 '16 at 14:26
  • @cdonts I have done port forwarding on port 8009 too. – Sanyam Jain Feb 14 '16 at 14:55
1

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.

dan4thewin
  • 1,134
  • 7
  • 10
0

i know this isn't a part of your question but you wrote the print function wrong

how i see your code: print "Hello world"

how the code should be: print("Hello world")

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 17 '22 at 07:57