Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Winsock2:当我尝试发送带有空格的字符串时,函数在遇到空格时显示为停止发送_C++_Sockets_Winsock2 - Fatal编程技术网

C++ Winsock2:当我尝试发送带有空格的字符串时,函数在遇到空格时显示为停止发送

C++ Winsock2:当我尝试发送带有空格的字符串时,函数在遇到空格时显示为停止发送,c++,sockets,winsock2,C++,Sockets,Winsock2,我一整天都在与这个密码抗争,我几乎要拔出我剩下的头发了 我有一个服务器和客户机类,最初的目标是让服务器类有一个可以与之交互的“客户机”列表。撇开所有的问题不谈,我有一些基本的工作。服务器确实注册了新的连接,我甚至可以从客户端发送字符串 这里的交易,但当我试图发送一个字符串与空格,整个事情失败 这是我的发送函数 int Send(std::string message) { const char* cstr = message.c_str(); si

我一整天都在与这个密码抗争,我几乎要拔出我剩下的头发了

我有一个服务器和客户机类,最初的目标是让服务器类有一个可以与之交互的“客户机”列表。撇开所有的问题不谈,我有一些基本的工作。服务器确实注册了新的连接,我甚至可以从客户端发送字符串

这里的交易,但当我试图发送一个字符串与空格,整个事情失败

这是我的发送函数

    int Send(std::string message)
    {
        const char* cstr = message.c_str();
        size_t      len = message.length();

        //The +1 is for the \0 character that c_str() adds
        //this->server is a socket that has already been connected to and accepted by the server
        int bytes = send(this->server, cstr, len + 1, 0);  
        return bytes;
    }
在服务器端:

void Run()
    {
        char buffer[1024];
        while (1)
        {
            listen(server, 0);
            SOCKET incoming_sock;
            int clientAddrSize = sizeof(clientAddr);
            if ((incoming_sock = accept(server, (SOCKADDR*)&clientAddr, &clientAddrSize)) != INVALID_SOCKET)
            {
                std::cout << "Error: " << WSAGetLastError() << std::endl;
                std::cout << "Connection occured " << printIP(clientAddr.sin_addr.s_addr) << std::endl;
                int bytes = recv(incoming_sock, buffer, sizeof(buffer), 0);
                std::cout << bytes << " Bytes With the message: " << buffer << std::endl;
                std::cout << "Error: " << WSAGetLastError() << std::endl;
            }
            else
            {
                std::cout << "Error: " << WSAGetLastError() << std::endl;
            }
        }
    }
如果我取消对输入的注释,并在提示符中键入“Hello”,我将得到以下输出:

In the Constructor Error code: 0Bind code: 0
Error: 0
Connection occured 127.0.0.0
6 Bytes With the message: hello
Error: 0
但是如果我输入“你好,世界!” 我只得到:

In the Constructor Error code: 0Bind code: 0
Error: 0
Connection occured 127.0.0.0
6 Bytes With the message: Hello
Error: 0

std::cin>>味精最多读取第一个空格。如果你想读一整行直到行尾字符,那就让它

std::getline(cin, msg);

std::cin>>字符串
在第一个空格处停止!请尝试
std::getline(std::cin,string)
读取第一个换行符。另一方面,您的
Send()
代码不能保证一次性发送整个字符串。返回值告诉您实际接受发送的字节数,您需要在循环中调用
send()
,直到所有字节实际发送完毕。同样,在
Run()
中,您需要在循环中调用
recv()
,直到收到所有预期的字节。您正在发送字符串的null终止符,因此请一直读取直到终止符到达。它。。。作品事后看来,我应该把输入的内容打印出来。这会帮我省下几个小时。非常感谢。
In the Constructor Error code: 0Bind code: 0
Error: 0
Connection occured 127.0.0.0
6 Bytes With the message: Hello
Error: 0
std::getline(cin, msg);