Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python UDP client/server program, problems

I'm trying to write a basic client/server echo program, to test the usage of timers for retransmission based on select() (though I had to comment out that bit to simplify debugging when it wasn't working as intended). Here are snippets of the relevant code:

Server:

from socket import *
import sys
import select
address = ('localhost', 6005)
server_socket = socket(AF_INET, SOCK_DGRAM)
server_socket.bind(address)

while(1):
    print "Listening"
    recv_data, addr = server_socket.recvfrom(2048)
    print recv_data
    if recv_data == "Request 1" :
        print "Received request 1"
        server_socket.sendto("Response 1", address)
    elif recv_data == "Request 2" :
        print "Received request 2"
        data = "Response 2"
        server_socket.sendto(data, address)

Client:

from socket import *
import sys
import select

address = ('localhost', 6005)
client_socket = socket(AF_INET, SOCK_DGRAM)

num_retransmits = 0
while(num_retransmits < 60):
    num_retransmits = num_retransmits + 1


    data = "Request 1"
    client_socket.sendto(data, address)
    print "Sending request 1"

    recv_data, addr = client_socket.recvfrom(2048)

    print recv_data, "!!"

The output on the client is simply 'Sending request 1', and when a breakpoint is used on anything at or below the recvfrom call, it does not reach the breakpoint. So I figure the client is not receiving anything and is holding out til it does. On the other hand, the output on the server is:

  • Listening
  • Request 1
  • Received request 1
  • Listening
  • Response 1

and so on and so forth

After the first loop, the server loops again and prints RESPONSE 1. This means that what the server did was to receive request 1, send response 1 to the client, loop... but after it loops the second time, response 1 is STILL in its socket! That is why when it prints recv_data,it prints response 1. On the other hand, client is not printing recv_data, because client has not received it - it is still in the buffer of the server's socket.

Please help - I have tried looking at other echo programs but they all seem to use TCP and are fairly straightforward (and I think I have pretty much followed the same steps). I have no idea why my UDP program is not working. I tried looking at the sendall() call but it only seems to work for TCP.

like image 355
misaochan Avatar asked Mar 25 '12 18:03

misaochan


People also ask

What is UDP socket and explain UDP client server with example?

UDP - User Datagram Protocol sockets It is a connection-less protocol where you directly send packets without have to establish a proper connection. UDP packets have smaller headers compared to TCP headers. Also data communication is faster since no acknowledgement is exchanged for reliable packet delivery.

Can UDP client communicate with TCP server?

No, you can't have a TCP server communicating with UDP clients.

Can UDP handle multiple clients?

It is often required for the server applications to be able to accept several UDP connections from clients on the same port. Such application is for example a TFTP server. The multiplex handling must be implemented in the session layer of the user application.


1 Answers

You have to send to addr instead of address.

from socket import *
import sys
import select
address = ('localhost', 6005)
server_socket = socket(AF_INET, SOCK_DGRAM)
server_socket.bind(address)

while(1):
    print "Listening"
    recv_data, addr = server_socket.recvfrom(2048)
    print recv_data
    if recv_data == "Request 1" :
        print "Received request 1"
        server_socket.sendto("Response 1", addr)
    elif recv_data == "Request 2" :
        print "Received request 2"
        data = "Response 2"
        server_socket.sendto(data, addr)
like image 117
Mathias Avatar answered Sep 25 '22 17:09

Mathias