misaochan
misaochan

Reputation: 890

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:

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.

Upvotes: 10

Views: 38207

Answers (3)

ephemient
ephemient

Reputation: 204668

What is happening here is the server is sending out 'Response 1' to localhost:6005, and then recieving it immediately as it is also listening on localhost:6005.

The server binds to and listens on its (address, port), as is correct. When the client connects without binding first, it is automatically assigned an unused (address, port). You'll need to determine what (address, port) the client is using in order to respond to it — either by bind to explicitly set a known (address, port), or by using the addr returned by recvfrom.

Upvotes: 2

Mathias
Mathias

Reputation: 398

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)

Upvotes: 14

H_7
H_7

Reputation: 287

maybe line 10 on server side

recv_data, addr = server_socket.recvfrom(2048)

should be

recv_data, addr = server_socket.sendto(2048)

?

ps> Im a noob. =P

Upvotes: 1

Related Questions