Take the 2-minute tour ×

I am a newbie to python, and just few days back I started trying my hands on network programming(I am a newbie there too)

Now I found a neat client server program which was running quite simply on my computer, but when I replaced the local addresses, and told my friend to run the client script, it just wont respond.

My global I.P address : 120.59.XX.XXX

My Ipv4 address as returned by ipconfig : (I am connected to internet through a router)

My gateway address :

Port used : 1060 (I tested this port locally and it wasn't in use)


import socket
import sys

server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

MAX = 65535
PORT = 1060

server.bind(('', PORT))
print 'Listening at', server.getsockname()
while True:
    data, address = server.recvfrom(MAX)
    print 'The client at', address, 'says', repr(data)
    server.sendto('Your data was %d bytes' % len(data), address)

Client Code :


import socket
import sys

client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

PORT = 1060
MAX = 65536

client.sendto('Hello Server!', ('120.59.XX.XXX', PORT))
data, address = client.recvfrom(MAX)
print 'The server', address, 'says', repr(data)

I started server.py on my computer and told my friend to start client.py, I allowed incoming connections to python through firewall, also I added 1060 port to windows incoming connections list.

Still it is not responding, and I am unable to decipher why(I have a dynamic IP address, but for the current session it remains constant and hence should work, also 1060 is a well known port and shouldn't be a problem right?)

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

You need to add a port forwarding rule in your router! something like from port 1060 forward to port 1060.

share|improve this answer
yes this is what I first thought, but then how come utorrent is able to use ports without me forwarding them?and 1060 is well known used by SAP –  Kartik Anand May 22 '12 at 15:55
It goes out connecting to a server somewhere, i.e. the TCP connection is initiated from your PC. –  Nikolai N Fetissov May 22 '12 at 16:07
NAT techniques often work kind of intelligent. If a connection is initiated by a machine within the local network, most NAT implementations map the response back to this machine. If a connection is initiated from the outside, it comes unexpectedly (from the "router"'s point of view). If there is no rule specified how to handle such an unexpected connection (like a port forward rule), the router rejects the connection. –  Jan-Philip Gehrcke May 22 '12 at 16:36
so if I could trick the router, I mean send a random(or empty) message to any URL by UDP, get my random port number(which OS would assign), and then bind my server to that port number.will it work? –  Kartik Anand May 22 '12 at 16:40
add comment

You need a port-forward on the router, which would forward connections to router's external (global) address on port 1060 to your desktop IP port 1060.

share|improve this answer
Ok but if I use a well known port, should it work then?because I used 80(I know I shouldn't) and it still didn't work. –  Kartik Anand May 22 '12 at 15:58
Well known ports like 80 and 25 are often blocked by ISPs on incoming. Use a random high number port like 42424. –  Nikolai N Fetissov May 22 '12 at 16:08
I forwarded 1060 to my local IP, and it still isn't working. –  Kartik Anand May 22 '12 at 16:09
The router might assume TCP when you do port-forward. See if there's an option there to enable UDP, which you are trying to do. –  Nikolai N Fetissov May 22 '12 at 16:11
i've added for all protocols here the link tinypic.com/r/3byiq/6 –  Kartik Anand May 22 '12 at 16:15
show 10 more comments

Your Answer


