Send UDP Package to Python Server

Hi there, I’m following this tutorial to set up a UDP sender but I can only get false result of following variable.

bool connected = Socket->Connect(*addr);

I’m not acquainted with those UDP and network things so not sure why this keeps happening. Anyone may suggest on this?

Hello PhoenixThrough,

The tutorial you posted focuses on TCP sockets. UDP and TCP communication is fundamentally different in a few key aspects. One of these major differences is that UDP communication is actually connection-less.

In TCP, the flow looks something like this:

  1. Server is started, and begins “listening” on a given address / port.
  2. A client with knowledge of the address requests a connection to the server.
  3. The server “hears” this request.
  4. The connection is established, and a new socket is created specifically for the server and client to communicate with (note, this connection is actually done via a separate port which is determined at runtime, and which is unique for each client connection).

For UDP, the flow looks more like this:

  1. A UDP “server” is started. This is effectively just a standard client that listens to a specific address / port.
  2. A client with knowledge of the address sends data explicitly tot eh address / port.
  3. The server receives this data (as well as the address / port of where the data was sent from).

    // Initialize the socket.
    
    ///////// Sending Data /////////
    
    uint8* DataToSend;
    int32 NumberOfBytesToSend;
    int32 NumberOfBytesActuallySent;
    int32 TotalNumberOfBytesSent;
    bool SuccessfullySent = true;
    
    // Populate data
    
    // We may not be able to send all the data in a single packet. This typically only happens with very large amounts of data.
    
    while (TotalNumberOfBytesSent < NumberOfBytesToSend)
    {
        // DataToSend+ TotalNumberOfBytesSent = The new head location of the data to send.
        // NumberOfBytesToSend - TotalNumberOfBytesSent: The remaining number of bytes.
        // NumberOfBytesActuallySent: The number of bytes sent this time (return).
        SuccessfullySent = socket->SendTo( DataToSend+ TotalNumberOfBytesSent, NumberOfBytesToSend - TotalNumberOfBytesSent, NumberOfBytesActuallySent );
    
        TotalNumberOfBytesSent += NumberOfBytesActuallySent
    }
    
    // Check to ensure all data was sent.
    
    ///////// Receiving Data /////////
    
    uint8* ReceivedData;
    int32 TotalBufferSize;
    int32 NumberOfBytesReceived;
    bool ReceivedSuccessfully = true;
    ESocketReceiveFlags Flags = ESocketReceiveFlags::None;
    
    ReceivedSuccessfully = socket->RecvFrom( ReceivedData, TotalBufferSize, NumberOfBytesReceived, Flags);
    
    // Ensure the read was successful.
    // Validate the data against your own application protocol / format.

Thanks,
Jon N.

1 Like

These types of flows should be valid, regardless what language the server / client are written in, so you should have no problem using Python.