Connecting to a simple sockets python server remotely

Kex picture Kex · Sep 14, 2015 · Viewed 9.2k times · Source

I am trying to setup a very simply sockets app. My server code is:

import socket

s = socket.socket()
host = socket.gethostname()

port = 1234
s.bind((host,port))

s.listen(5) #Here we wait for a client connection
while True:
    c, addr = s.accept()
    print "Got a connection from: ", addr
    c.send("Thanks for connecting")
    c.close()

I placed this file on my remote Linode server and run it using python server.py. I have checked that the port is open using nap:

PORT     STATE SERVICE
22/tcp   open  ssh
80/tcp   open  http
1234/tcp open  hotline

I now run the client.py on my local machine:

import socket              # Import socket module

s = socket.socket()        # Create a socket object
port = 1234                # Reserve a port for your service.

s.connect(("139.xxx.xx.xx", port))
print s.recv(1024)
s.close                    # Close the socket when done

However I am not getting any kind of activity or report of connection. Could someone give me some pointers to what I might have to do? Do I need to include the hostname in the IP address I specify in the client.py? Any help would be really appreciated!

Answer

Casimir Crystal picture Casimir Crystal · Sep 14, 2015

I've just summarize our comments, so your problem is this:

When you trying to using the client program connect to the server via the Internet, not LAN. You should configure the port mapping on your router.

And however, you just need configure the port mapping for your server machine.
After you did that, then you can use the client program connect to your server prigram.