Python UDP client/server program, problems

37,404

Solution 1

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)

Solution 2

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.

Solution 3

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

Share:
37,404
misaochan
Author by

misaochan

Updated on March 26, 2020

Comments

  • misaochan
    misaochan about 4 years

    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.

  • H_7
    H_7 about 12 years
    Thanks. I learned more trying to answer you than anything you can get from me.
  • misaochan
    misaochan about 12 years
    Thanks, I was thinking so as well. However, surely this is the only way to test both server and client on the same machine - bind server to port A and 'localhost', then use the client to send stuff to Port A and localhost? Can bind() also be used for clients?
  • misaochan
    misaochan about 12 years
    Oh, thank you! It's all fixed now. For some reason I was under the impression that server and client both had to be USING the same ports on localhost...
  • misaochan
    misaochan about 12 years
    Thank you! For some reason this post didn't show up :S But yes, that fixed it
  • ephemient
    ephemient about 12 years
    @JosephineLim Bind can be used on client sockets, but it's uncommon.