Search code examples
c#c++socketsrecvfrom

UDP Socket: recvfrom() returns -1 (SOCKET_ERROR)


I'm working on a server-client application based on UDP protocols. The server is written in c++ and the client is in C#.

On the server side I get the buffer using the recvfrom():

int bufLen = recvfrom(s, buf, 1024, NULL, (sockaddr*)&addr, &addrlen);

The weird thing is that the received buf is exactly what I've sent from the client but the bufLen is -1 which is SOCKET_ERROR.

I have no idea what the problem might be. please help. Thanks a million.


Solution

  • Your datagram is too big, or your buffer is too small.

    The datagram is larger than your buffer, so it gets trucated, you get an error return from recvfrom and GetLastError() returns 10040, ('WSAEMSGSIZE').