gpt4 book ai didi

c - 在 C 中使用第二次调用 send() 时出现问题

转载 作者:行者123 更新时间:2023-11-30 18:08:57 26 4
gpt4 key购买 nike

现在我正在一个简单的服务器中工作,该服务器从客户端接收引用特定操作的代码。服务器接收该数据并发回正在等待正确数据的信号。

                   /*Server Side*/
if (codigoOperacao == 0)
{
printf("A escolha foi 0\n");
int bytesSent = SOCKET_ERROR;
char sendBuff[1080] = "0";
/*Here "send" returns an error msgm while trying to send back the signal*/
bytesSent = send(socketEscuta, sendBuff, 1080, 0);
if (bytesSent == SOCKET_ERROR)
{
printf("Erro ao enviar");
return 0;
}
else
{
printf("Bytes enviados : %d\n", bytesSent);
char structDesmontada[1080] = "";
bytesRecv = recebeMensagem(socketEscuta, structDesmontada);
printf("structDesmontada : %s", structDesmontada);
}
}

下面是客户端代码,负责发送操作代码和接收信号

                char sendMsg[1080] = "0";
char recvMsg[1080] = "";
bytesSent = send(socketCliente, sendMsg, sizeof(sendMsg), 0);
printf("Enviei o codigo (%d)\n", bytesSent);
/*Here the program blocks in a infinite loop since the server never send anything*/
while (bytesRecv == SOCKET_ERROR)
{
bytesRecv = recv(socketCliente, recvMsg, 1080, 0);
if (bytesRecv > 0)
{
printf("Recebeu\n");
}

为什么只有在第二次尝试发送一些数据时才会发生这种情况?因为第一次调用 send() 工作正常。希望有人能帮忙!!谢谢

最佳答案

我已经整理好了。第一个接收缓冲区对于传入消息来说太小,并且溢出,从而删除了 SOCKET socketEscuta 变量。现在一切正常。感谢您的提示!

关于c - 在 C 中使用第二次调用 send() 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2946817/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com