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

                   /*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()可以正常工作。
希望有人能帮忙!
Thnks

最佳答案

我整理了一下。对于接收的消息,第一个接收缓冲区很小,并且溢出,从而擦除了SOCKET socketEscuta变量。现在一切正常。感谢您的提示!

09-13 13:18