客户端发送消息但服务器在客户端结束程序之前不接收消息

时间:2018-06-11 15:12:15

标签: c++ aes crypto++

    do{

        /// send msgs
        cout << "Client: ";
        cin.getline(clientMSG, 1024, '\n');

        if(strcmp(clientMSG, "quit") == 0){

            StringSource msgEncryptor(clientMSG, true, new StreamTransformationFilter(encrypt, new StringSink(encryptMSG)));
            StringSource encryptEncode(encryptMSG, true, new HexEncoder(new StringSink(encodeMSG)));

            memset(clientSEND, '\0', 3000);
            copy(encodeMSG.begin(), encodeMSG.end(), clientSEND);

            send(sock, clientSEND, strlen(clientSEND), 0);

            break;
        }

        StringSource msgEncryptor(clientMSG, true, new StreamTransformationFilter(encrypt, new StringSink(encryptMSG)));
        StringSource encryptEncode(encryptMSG, true, new HexEncoder(new StringSink(encodeMSG)));

        memset(clientSEND, '\0', 3000);
        copy(encodeMSG.begin(), encodeMSG.end(), clientSEND);

        send(sock, clientSEND, strlen(clientSEND), 0);

        /** ------------------------------------------------ **/

        /// receive msgs
        memset(clientRECV, '\0', 3000);
        valread = read(sock, clientRECV, 3000);

        StringSource encryptDecode(clientRECV, true, new HexDecoder(new StringSink(decodeMSG)));
        StringSource msgDecryptor(decodeMSG, true, new StreamTransformationFilter(decrypt, new StringSink(decryptMSG)));

        cout << "Server: " << decryptMSG << endl;





    }while(1);

以上是客户端程序

do{

        /// receive msgs

        memset(serverRECV, '\0', 3000);
        valread = read(new_socket, serverRECV, 3000);

        StringSource encryptDecode(serverRECV, true, new HexDecoder(new StringSink(decodeMSG)));
        StringSource msgDecryptor(decodeMSG, true, new StreamTransformationFilter(decrypt, new StringSink(decryptMSG)));

        if(decryptMSG == "quit"){
            cout << "Client disconnected!" << endl;
            break;
        }

        cout << "Client: " << decryptMSG << endl;

        /** ------------------------------------------------------------------------ **/

        /// send msgs

        cout << "Server: ";
        cin.getline(serverMSG, 1024, '\n');

        if(strcmp(serverMSG, "quit") == 0){
            StringSource msgEncryptor(serverMSG, true, new StreamTransformationFilter(encrypt, new StringSink(encryptMSG)));
            StringSource encryptEncode(encryptMSG, true, new HexEncoder(new StringSink(encodeMSG)));

            memset(serverSEND, '\0', 3000);
            copy(encodeMSG.begin(), encodeMSG.end(), serverSEND);

            send(new_socket, serverSEND, strlen(serverSEND), 0);

            break;
        }

        StringSource msgEncryptor(serverMSG, true, new StreamTransformationFilter(encrypt, new StringSink(encryptMSG)));
        StringSource encryptEncode(encryptMSG, true, new HexEncoder(new StringSink(encodeMSG)));

        memset(serverSEND, '\0', 3000);
        copy(encodeMSG.begin(), encodeMSG.end(), serverSEND);

        send(new_socket, serverSEND, strlen(serverSEND), 0);

        cin.clear();
        memset(serverMSG, '\0', 3000);
    }while(1);

这是服务器。

我正在尝试编写一些套接字程序,我突然遇到这个问题,当客户端发送消息时,服务器不显示它,直到我强制结束程序。

程序应该从客户端输入消息开始,然后加密消息并将其发送到服务器。然后,服务器将解密它并显示该消息。但我不明白为什么只有当我结束它显示的程序时它才显示消息...

我使用错误的发送/读取方式吗?我很确定这不是加密/解密问题,因为当我在客户端结束程序时,我在服务器端看到了消息。

1 个答案:

答案 0 :(得分:0)

我假设您没有将套接字设置为非阻塞,但是在服务器端设置了该行:

valread = read(new_socket, serverRECV, 3000);

...正试图从套接字读取3000个字节。 read()函数阻塞当前线程,直到从描述符中读取了所有数据。关闭程序会中止read(),此时会执行其余的代码。

使用您的代码,如果您希望服务器处理它,请确保客户端发送3000个字节。

如果您希望执行非阻塞读取,则应相应地设置套接字:

int flags = fcntl(new_socket, F_GETFL, 0);
fcntl(new_socket, F_SETFL, flags | O_NONBLOCK);

请注意,您需要检查read()的大量返回值,并创建一个从套接字读取的逻辑,直到您收到正确的字节数。