C windows sendto()

时间:2009-11-23 20:56:59

标签: c sockets udp winsock sendto

我正在尝试使用以下代码通过UDP发送,但我得到了奇怪的结果。

    if((sendto(newSocket, sendBuf, totalLength, 0, (SOCKADDR *)&sendAddr, sizeof(sendAddr)) == bytesSent) < 0)
    {
    printf("Send error! - %d\n", WSAGetLastError());

    }

然而,当totalLength变量设置为30时,sendto函数实际返回2292556,如果它不返回至少30左右的标记?我在使用sendto之前检查了totalLength变量,它会很高兴地返回一个我同意的值,但是sendto会返回一个很大的值。总长度永远不会大于实际的缓冲区大小。

WSAGetLastError只返回0.

感谢。

2 个答案:

答案 0 :(得分:1)

我认为你的问题是条件的 == bytesSent) < 0)部分。这应该是if (( bytesSent = sendto( ... )) < 0 )吗?

答案 1 :(得分:0)

如果sendto(...)等于bytesSent而不是bytesSent=sendto

,那你就是在挑衅