我遇到了通过TCP网络发送字符串的问题,其中字符串正在发送未输入的附加字符。使用C++中的套接字通过网络发送字符串
下面是我用来发送字符串的代码。
string input;
printf("Please input what you want to send...\n");
printf(">");
cin >> input;
const char* ch = (const char*)&input;
int lengthOfBytes = sizeof(input);
for (int i = 0; i < lengthOfBytes; i++)
{
n = send(socket_d, &*ch, 10, 0);
}
//Reading in characters.
if (ch == (const char*)'\r')
{
cout << "\n";
}
这里是用于接收字符串的代码。
int n;
int count = 0;
char byte;
n = recv(socket_d, &byte, 1, 0);
if (n <= 0)
{
if (WSAGetLastError() != WSAEWOULDBLOCK)
{
cout << "Terminated " << WSAGetLastError() << "\n";
return;
}
}
else
{
cout << (char) byte;
if ((char) byte == '\r')
cout << "\n";
}
我在通过网络发送字符串时做错了什么?