Winsock2:当我尝试发送带有 space 的字符串时,该函数在遇到 space 时出现停止发送
Winsock2: When I try to send a string with spaces, the function appears stop sending when it encounters a space
我现在一整天都在和这个代码作斗争,我快要拔掉我剩下的头发了。
我有一个服务器和客户端 class,最初的目标是让服务器 class 拥有一个可以与之交互的 'Clients' 列表。撇开所有问题不谈,我已经掌握了一些基础知识。服务器确实注册了新连接,我什至可以从客户端发送字符串。
事情是这样的,当我尝试发送带有 space 的字符串时,整个事情就崩溃了。
这是我的发送函数
int Send(std::string message)
{
const char* cstr = message.c_str();
size_t len = message.length();
//The +1 is for the [=11=] 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 World" 服务器打印出来就好了。但是当我尝试使用 std::cin 解析用户输入时,消息在第一个空白 space.
后中断
客户端主要功能:
#include "Client.h"
#include <iostream>
int main()
{
Client c("127.0.0.1", 5555, 1);
std::string msg = "Hello World!";
while (msg.compare("exit") != 0)
{
//std::cout << "Send: ";
//std::cin >> msg;
int bytes = c.Send(msg);
std::cout << "Sent \"" << msg << "\"" << "Bytes: " << bytes << std::endl;
}
while (1);
return 0;
}
服务器上的输出:
In the Constructor Error code: 0Bind code: 0
Error: 0
Connection occured 127.0.0.0
13 Bytes With the message: Hello World!
Error: 0
如果我取消注释输入,并在提示中输入 "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
但是如果我输入 "Hello World!"
我只得到:
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 >> msg;
读取到第一个空格。如果要阅读整行直到行尾字符,请将其设置为
std::getline(cin, msg);
我现在一整天都在和这个代码作斗争,我快要拔掉我剩下的头发了。
我有一个服务器和客户端 class,最初的目标是让服务器 class 拥有一个可以与之交互的 'Clients' 列表。撇开所有问题不谈,我已经掌握了一些基础知识。服务器确实注册了新连接,我什至可以从客户端发送字符串。
事情是这样的,当我尝试发送带有 space 的字符串时,整个事情就崩溃了。
这是我的发送函数
int Send(std::string message)
{
const char* cstr = message.c_str();
size_t len = message.length();
//The +1 is for the [=11=] 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 World" 服务器打印出来就好了。但是当我尝试使用 std::cin 解析用户输入时,消息在第一个空白 space.
后中断客户端主要功能:
#include "Client.h"
#include <iostream>
int main()
{
Client c("127.0.0.1", 5555, 1);
std::string msg = "Hello World!";
while (msg.compare("exit") != 0)
{
//std::cout << "Send: ";
//std::cin >> msg;
int bytes = c.Send(msg);
std::cout << "Sent \"" << msg << "\"" << "Bytes: " << bytes << std::endl;
}
while (1);
return 0;
}
服务器上的输出:
In the Constructor Error code: 0Bind code: 0
Error: 0
Connection occured 127.0.0.0
13 Bytes With the message: Hello World!
Error: 0
如果我取消注释输入,并在提示中输入 "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
但是如果我输入 "Hello World!" 我只得到:
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 >> msg;
读取到第一个空格。如果要阅读整行直到行尾字符,请将其设置为
std::getline(cin, msg);