UDP Client Send & Receive Data from Server Python

697 Views Asked by At

I am trying to write a UDP client that can both send and receive data. However, the only way I can get it to work is to repeatedly create and then close the socket each time I want to send or receive data. I am guessing this is probably not the right way of doing things, and that I should only need to create the socket once before entering the loop and close it after exiting the loop.

I noticed that if I comment out the receive portion of the code, then I only need to create the socket once before entering the for loop, and then close the socket after exiting the for loop and the sending portion still works fine. If I then add back the receive code it either hangs on the s.recvfrom() call, or the s.bind() call, depending on where it's placed in the code.

I am not sure what I am doing wrong, please see the attached code below,

import socket
import time
import struct

TX_UDP_IP = '172.24.15.34' #IP address of ETH SFP
TX_UDP_PORT = 4575

#TX Packet Header
TX_PACKETVERSION = 1
TX_P8ADDRESSOFFSET = 0
TX_SEQUENCENUM = 0

RX_UDP_IP = '172.24.2.7' #IP address of local machine
RX_UDP_PORT = 4575

BUFFER_SIZE = 1024

print("Entered Loop")
start = time.perf_counter()
for i_python in range(1,11):
    
    f_python = float(i_python) + 0.15

  
    send_data_str = struct.pack('>iiiid', TX_PACKETVERSION, TX_P8ADDRESSOFFSET, TX_SEQUENCENUM, i_python, f_python)
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.sendto(send_data_str, (TX_UDP_IP, TX_UDP_PORT))
    s.close()

    
    TX_SEQUENCENUM = TX_SEQUENCENUM + 1
    if TX_SEQUENCENUM > 1000:
        TX_SEQUENCENUM = 0
    
    print("Transmitted i_python = ",i_python," and f_python = ",f_python)
    
    time.sleep(1.0)
    
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.bind((RX_UDP_IP, RX_UDP_PORT))
    recv_data_str = s.recvfrom(BUFFER_SIZE)
    s.close()
    
    recv_data_str_unpacked = struct.unpack(">iiiid",recv_data_str[0])
    RX_PACKETVERSION = recv_data_str_unpacked[0]
    RX_P8ADDRESSOFFSET = recv_data_str_unpacked[1]
    RX_SEQUENCENUM = recv_data_str_unpacked[2]
    j_RTDS = recv_data_str_unpacked[3]
    t_RTDS = recv_data_str_unpacked[4]
    
    print("Recieved j_RTDS = ",j_RTDS," and t_RTDS = ",t_RTDS)
    
    
print ("Exited Loop")
finish = time.perf_counter() - start
print ('The execution time is %fs.' %finish)

EDIT 1 2021-07-26:

Ok, I got some comments asking me to post the version of the code that is not working and the associated error. So here is the version of the code that does not work and the associated error.

import socket
import time
import struct

TX_UDP_IP = '172.24.15.34' #IP address of ETH SFP
TX_UDP_PORT = 4575

#TX Packet Header
TX_PACKETVERSION = 1
TX_P8ADDRESSOFFSET = 0
TX_SEQUENCENUM = 0

RX_UDP_IP = '172.24.2.7' #IP address of local machine
RX_UDP_PORT = 4575

BUFFER_SIZE = 1024

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


print("Entered Loop")
start = time.perf_counter()
for i_python in range(1,11):
    
    f_python = float(i_python) + 0.15

  
    send_data_str = struct.pack('>iiiid', TX_PACKETVERSION, TX_P8ADDRESSOFFSET, TX_SEQUENCENUM, i_python, f_python)
    s.sendto(send_data_str, (TX_UDP_IP, TX_UDP_PORT))

    
    TX_SEQUENCENUM = TX_SEQUENCENUM + 1
    if TX_SEQUENCENUM > 1000:
        TX_SEQUENCENUM = 0
    
    print("Transmitted i_python = ",i_python," and f_python = ",f_python)
    
    time.sleep(1.0)
    
    s.bind((RX_UDP_IP, RX_UDP_PORT))
    recv_data_str = s.recvfrom(BUFFER_SIZE)
    
    recv_data_str_unpacked = struct.unpack(">iiiid",recv_data_str[0])
    RX_PACKETVERSION = recv_data_str_unpacked[0]
    RX_P8ADDRESSOFFSET = recv_data_str_unpacked[1]
    RX_SEQUENCENUM = recv_data_str_unpacked[2]
    j_RTDS = recv_data_str_unpacked[3]
    t_RTDS = recv_data_str_unpacked[4]
    
    print("Recieved j_RTDS = ",j_RTDS," and t_RTDS = ",t_RTDS)
    
    
print ("Exited Loop")
s.close()
finish = time.perf_counter() - start
print ('The execution time is %fs.' %finish)

Here is the error that occurs,

Exception has occurred: OSError
[WinError 10022] An invalid argument was supplied
  File "C:\UDP_SKT_Python_R06.py", line 51, in <module>
    s.bind((RX_UDP_IP, RX_UDP_PORT))
0

There are 0 best solutions below