1

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 : 192.168.1.2 (I am connected to internet through a router)

My gateway address : 192.168.1.1

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

#server.py

import socket
import sys

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

MAX = 65535
PORT = 1060

server.bind(('192.168.1.2', 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 :

#client.py

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?)

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

4 Comments

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
It goes out connecting to a server somewhere, i.e. the TCP connection is initiated from your PC.
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.
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?
1

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 192.168.1.2 port 1060.

14 Comments

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.
Well known ports like 80 and 25 are often blocked by ISPs on incoming. Use a random high number port like 42424.
I forwarded 1060 to 192.168.1.2 my local IP, and it still isn't working.
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.
Two other things to check - whether local firewall on windows pc allows udp:1060 in, and if you have any logging capability on that router, see if you are actually getting those packets from your friend (they might be filtered/dropped somewhere else).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.