我开发了UDP客户端服务器应用程序。现在我想检查客户端是否发送字符串“EXIT”,服务器收到此字符串,将其与“EXIT”字符串进行比较并退出程序。 但在我的情况下,服务器无法将收到的字符串与“EXIT”字符串进行比较。 以下是我编码的内容:
客户端:
char exitBuffer[]="EXIT";
if (sendto(socketIdentifier,exitBuffer,strlen(exitBuffer) , 0 , (struct sockaddr *) &connectedSocket, sizeof(connectedSocket)) == SOCKET_ERROR)
{
exit(EXIT_FAILURE);
}
服务器
if ((recv_len = recvfrom(socketIdentifier, receiveBuffer, sizeof(receiveBuffer), 0, (struct sockaddr *) &clientSocket, &clientSocketLength)) == SOCKET_ERROR)
{
MessageBox(NULL,
exit(EXIT_FAILURE);
}
// Now comparing the contents of receive Buffer
if (receiveBuffer == "EXIT")
{
exit(0);
}
答案 0 :(得分:0)
我尝试了memcmp,它做到了:) 现在在服务器端进行的比较如下:
if(memcmp(receiveBuffer,"EXIT",5) == 0)
{
//receiveBuffer is eqaul to "EXIT".
}