Search code examples
networkingudpipudpclientapplication-layer

Network example - IP address


I just started to learn about networking and follow the book by Kurose and Ross. They have the following snippets of python code to illustrate the UDP protocol.

The code for the simple client is given by

from socket import *
serverName = ‘hostname’     # Use IP adresse here
serverPort = 12000
clientSocket = socket(socket.AF_INET, socket.SOCK_DGRAM) 
message = raw_input(’Input lowercase sentence:’) 
clientSocket.sendto(message,(serverName, serverPort)) 
modifiedMessage, serverAddress = clientSocket.recvfrom(2048) 
print modifiedMessage
clientSocket.close()

and the code for the server is given by


from socket import *
serverPort = 12000
serverSocket = socket(AF_INET, SOCK_DGRAM) 
serverSocket.bind((’’, serverPort))
print ”The server is ready to receive” 
while 1:
    message, clientAddress = serverSocket.recvfrom(2048) 
    modifiedMessage = message.upper() 
    serverSocket.sendto(modifiedMessage, clientAddress)

I am lucky to have two laptops and thought about letting one running the client and one the server part. Is this naive? I am struggling to find out how to specify the serverName variable or IP address here. Both laptops are in the same WiFi network (even though its eduroam which might cause a problem?)

When I use the terminal to find out the local IP addresses one gives me 10.17.47.158 and the other says 100.112.82.103. But just using these IP addresses doesn't seem to work. What am I doing wrong? Also why are they so different, does it mean they are not connected to the same router?

Can I just run both applications on two different laptops and everything should work fine normally when I specify the correct IP address? Or am I getting something completely wrong here?


Solution

  • You have two options

    1. Run the client and the server using the same computer - this way all you'll need to do is to write localhost or 127.0.0.1 in the IP field.
    2. Run on 2 computers - In this may you may encountered some problems. They can be from a bunch of reasons but one of the most known issue is firewall.

    What I would do is follow these steps:

    1. Run the server and the client on the same computer to make sure the code is working
    2. Send ping from one computer to another and make sure I receive answer.
    3. Close the firewall on both computers - and try to run the softwares. If you didn't receive ping in step 3- try now. If now ping isn't working jump to step 5/6
    4. If it worked up to step 3- turn on firewall- and put firewall rules on the server to allow port 12000.
    5. Usually- up to here you'll have a working server a client. But if step 4 didn't work - I would open wireshark and check if the packet received to the server and sent from the client- and will continue the research from here using the data I have.
    6. It looks like your two computers are on different subnet - therefor what I would do is put them in the same network with the same subnet. If you want them at different subnets- you would have to configure route in your router between those two subnets.

    Good Luck!