jweinst1
4/6/2020 - 7:48 PM

Using recvfrom in a UDP server on winsock

Using recvfrom in a UDP server on winsock

int bytes_received;
char serverBuf[1025];
int serverBufLen = 1024;

// Keep a seperate address struct to store sender information. 
struct sockaddr_in SenderAddr;
int SenderAddrSize = sizeof (SenderAddr);

printf("Receiving datagrams on %s\n", "127.0.0.1");
bytes_received = recvfrom(serverSocket, serverBuf, serverBufLen, 0 /* no flags*/, (SOCKADDR *) & SenderAddr, &SenderAddrSize);
if (bytes_received == SOCKET_ERROR) {
     printf("recvfrom failed with error %d\n", WSAGetLastError());
}
serverBuf[bytes_received] = '\0'