Home > OS >  UDP Listening server doesn't detect the traffic needed but WS does
UDP Listening server doesn't detect the traffic needed but WS does

Time:12-28

I have a server that is listening for UDP traffic being received from a remote location, I do not have access to the client code but I can confirm the continual stream of UDP packets being received by the server using WS, my current goal is simple, dump the UDP traffic using PyCharm. Here is the little code I have so far:

import socket

ip = 'x.x.x.x'<-- Masked the IP for security reason but that IP is the public IP of my server.
port = 3659
UDPServerSocket = socket.socket(socket.AF_INET, type=socket.SOCK_DGRAM)
server_address = (ip, port)
UDPServerSocket.bind(server_address)
print("Listening for UDP")
while True:
    data, address = UDPServerSocket.recvfrom(1024)
    print("Server received: ", data.decode('utf-8'))

When listening, the code just hangs on the recvfrom command, as it doesn't detect the traffic being received, meanwhile WS does see the inbound traffic, and the destination matches the server IP from the code: enter image description here

While PyCharm is running I can confirm it is listening to the correct port using tcpview aswell: enter image description here

So WS picks up the traffic without issues, I can confirm PyCharm is listening to the correct interface/port however none of the traffic is detected, could it be because the traffic is received from a remote location? all of the example I have seen are using local address/loopback.

Apologies if this was a lot of writting, thank you for whoever read this through, I'll make sure to at least thumb up whoever may have a solution for me, thanks.

CodePudding user response:

I don't believe you are telling the created socket to listen on UDP.
Try socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)

CodePudding user response:

port = 3659
...
server_address = (ip, port)
UDPServerSocket.bind(server_address)

The screenshot shows packets being sent from port 3659 to port 25208. In order to receive the packets you would therefore need to bind to port 25208 (receiver) not 3659 (sender) as you currently do.

Note that this code must be run on the server side instead of the actual server, i.e. a normal socket cannot be used to get the packets in parallel to another socket getting the same packets too.

  • Related