C++ 套接字接收混合消息

标签 c++ multithreading sockets winsock winsock2

您好,我遇到了套接字服务器和客户端的问题。

问题是当我非常快地发送消息时,消息会混淆。当我向他们发送时,假设每秒发送 1 条消息,一切运行良好,但当我每 40 毫秒向他们发送 1 条消息时,他们就会混淆。

这是我的接收代码:

    std::string* AteneaClient::readSocket () {

    std::string finalString = std::string("");
    int size = MSG_SIZE;

    bool receiving = true;
    int timesBufferInc=0;
    while (receiving) {
        std::string temporalString;

        //create an empty buffer
        char* RCV_BUFFER = (char*) malloc (size* sizeof(char));
        for(int i=0;i<size;i++){
            RCV_BUFFER[i]=' ';
        }
        RCV_BUFFER[size-1]='\0';

        int result =  recv(sock,RCV_BUFFER,size-1,NULL);
        if ( result== SOCKET_ERROR ) {
            free(RCV_BUFFER);
            return NULL;
        }
        else if(result<size-1){
            receiving=false;
        }

        temporalString = std::string(RCV_BUFFER);
        finalString+=temporalString;
    }
    return new std::string(finalString);
}

这是我的发送代码:

    int sendThread(void* data){
    SND_THREAD_DATA* parameters =(SND_THREAD_DATA*)data;
    SOCKET* individualSocket = parameters->individualSocket;
    std::string * message = parameters->message;

    char RCV_BUFFER[MSG_SIZE];
    std::string converter;
    std::cout <<"(!)Thread: Iniciando sendThread Individual.."<<std::endl;
    SOCKET default_socket = *individualSocket;

    bool running=true;

    while(running){


        int length=message->length();
        char *cstr = new char[length + 1];
        strcpy(cstr, message->c_str());
        if(::send(*individualSocket,cstr,length + 1,NULL)==SOCKET_ERROR){
            logSendError();
            running=false;
        }
        delete cstr;
        Sleep(SLEEPTIME);
    }

}

这是我设置套接字时的代码:

    void AteneaClient::startUp(){
    int iResult = 0;
    iResult = WSAStartup(MAKEWORD(2, 2), &WinSockData);
    if (iResult != NO_ERROR) {
        wprintf(L"(!)Main:WSAStartup() failed with error: %d\n", iResult);
        return;
    }

    ADDR.sin_addr.s_addr= inet_addr(IP);
    ADDR.sin_family = AF_INET;
    ADDR.sin_port = htons(PORT);
    sock = socket(AF_INET,SOCK_STREAM,0);
    running=true;
}

有人知道为什么套接字消息会混淆吗?

谢谢!


编辑:

这是我当前的接收方法,根据 Maxim 评论进行了改进:

    std::string* AteneaClient::readSocket () {

    int HEADER_SIZE=4;
    std::string finalString = std::string("");
    int sizeFirstBuffer = HEADER_SIZE*sizeof(char);
    char* RCV_BUFFER=(char*) malloc(sizeFirstBuffer+1);

    //clean new buffer
    for(int i=0;i<HEADER_SIZE;i++){
        RCV_BUFFER[i]=' ';
        }
    RCV_BUFFER[sizeFirstBuffer]='\0';



    int result =  recv(sock,RCV_BUFFER,sizeFirstBuffer,NULL);
    //cout << "The Size to read is:" <<RCV_BUFFER << endl;


    //now i create a buffer with that size
    int sizeThatIHaveToRead= atoi(RCV_BUFFER);
    int sizeSecondBuffer = sizeThatIHaveToRead*sizeof(char);
    char* RCV_BUFFER_SECOND=(char*) malloc(sizeSecondBuffer+1);

    //clean new buffer
    for(int i=0;i<sizeSecondBuffer;i++){
        RCV_BUFFER_SECOND[i]=' ';
        }
    RCV_BUFFER_SECOND[sizeSecondBuffer]='\0';



    result =  recv(sock,RCV_BUFFER_SECOND,sizeSecondBuffer,NULL);
    //cout << "RCV_BUFFER_SECOND:" <<RCV_BUFFER_SECOND << endl;
    finalString+=RCV_BUFFER_SECOND;
    return new std::string(finalString);
}

最佳答案

您正在通过流套接字发送字符串,并希望它们以原子方式发送和接收,例如要么没有发送/接收任何内容,要么发送/接收整个字符串。这不是流套接字的工作方式。

Stream 套接字通常只发送部分数据,因此您需要继续发送,直到所有数据发送完毕。同样用于接收。

您还需要以某种方式分隔消息,否则在接收时您将不知道一条消息何时结束以及下一条消息何时开始。两种最常见的方法是 a) 以消息大小作为前缀,b) 使用消息定界符(例如换行符)。

ZeroMQ可以为您完成这两项任务:您的应用程序最终会发送和接收完整的消息,而您不必在字节级别上实现消息框架和发送/接收。


更新后的代码仍然没有正确使用 sendrecv 调用。

下面是发送和接收 std::string 函数的正确用法:

#include <stdexcept>
#include <stdint.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>

ssize_t recv_all(int fd, void* buf, size_t buf_len) {
    for(size_t len = buf_len; len;) {
        ssize_t r = ::recv(fd, buf, len, 0);
        if(r <= 0)
            return r;
        buf = static_cast<char*>(buf) + r;
        len -= r;
    }
    return buf_len;
}

ssize_t send_all(int fd, void const* buf, size_t buf_len) {
    for(size_t len = buf_len; len;) {
        ssize_t r = ::send(fd, buf, len, 0);
        if(r <= 0)
            return r;
        buf = static_cast<char const*>(buf) + r;
        len -= r;
    }
    return buf_len;
}

void send_string(int fd, std::string const& msg) {
    ssize_t r;
    // Send message length.
    uint32_t len = msg.size();
    len = htonl(len); // In network byte order.
    if((r = send_all(fd, &len, sizeof len)) < 0)
        throw std::runtime_error("send_all 1");
    // Send the message.
    if((r = send_all(fd, msg.data(), msg.size())) < 0)
        throw std::runtime_error("send_all 2");
}

std::string recv_string(int fd) {
    ssize_t r;
    // Receive message length in network byte order.
    uint32_t len;
    if((r = recv_all(fd, &len, sizeof len)) <= 0)
        throw std::runtime_error("recv_all 1");
    len = ntohl(len);
    // Receive the message.
    std::string msg(len, '\0');
    if(len && (r = recv_all(fd, &msg[0], len)) <= 0)
        throw std::runtime_error("recv_all 2");
    return msg;
}

关于C++ 套接字接收混合消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20247473/

相关文章:

C++ 可变说明符

javascript - 在Javascript中,是否有相当于 "find if"的东西,或者是一种紧凑的方式来完成我想做的事情?

multithreading - 在 Rust 中使用 Mutex 和 Condvar 进行缓冲

java - 使用图形填充颜色时出现 StackOverFlowException - 包含代码

python - 如何解决 Alpine docker 容器内 Flask 应用程序的 Gunicorn exec 引发的 'OSError: libc not found'

c++ - Mat D =(Mat_<double>(3,3)<< 1,0,1,1,1,1,1,1,0);

c++ - C++ 中的 STL 列表指针

Java套接字 - 客户端不从服务器的套接字读取字符串

ios - 有没有办法在 iPhone 上获取启动的 VPN 隧道的套接字描述符,以便我可以在 C++ 库中使用它

python - 在python中将字节转换为字符串