openSocket
recvData
sendData
closeSocket
getErrMsg
The UDPSock class provides a cross-platform UDP socket. All UDPSock functions are non-blocking.
bool openSocket(int portNumber);
Opens the socket for communication on the specified port number. You need to call this before using sendData and recvData.
It returns false if it couldn't open the socket, and stores a description of the problem in errMsg.
int recvData(string &senderAddr, int &senderPort, unsigned char buffer[], unsigned int bufferSize);
Listen for incoming data. senderAddr and senderPort are set to the IP address and port number of the program that's sending to us. The message is stored in buffer, which should be bufferSize bytes long. The return value is the length of the received message in bytes. It will be negative if we didn't get anything.
If the return value is negative, it will be equal to one of these error codes:
DY_BUSYUNREACHABLE We didn't receive anything this time, try again later.
DY_NOTOPEN An error occurred and this socket was closed. errMsg is set with a description of the problem.
Sends the data in buffer to the specified address and port. messageLength tells UDPSock how long the message is, and should be less than or equal to the size of the buffer.
You need to pay special attention to the return value here. If it's positive, it tells us the number of bytes sent. This may or may not be the entire message! If we had a message with a length of 200 bytes, and sendData() returns 100, then the first 100 bytes have been sent. You would need to call sendData() again to send the rest.
If the return value is negative, it will be equal to one of these error codes:
DY_BUSYUNREACHABLE We couldn't send anything this time, try again later.
DY_MSGTOOLONG The messageLength was too long.
DY_NOTOPEN An error occurred and this socket was closed. errMsg is set with a description of the problem.
DY_INVALIDADDR destinationAddr does not contain a valid address.
Closes the socket after we're done with it.
Returns a description of this socket's most recent error.