通过C 中的TCP/IP协议发送.TXT文件
sending a .txt file via tcp/ip protocol in c++
我正在尝试制作一个程序,该程序将发送到服务器a .txt文件,修改其内容,然后将修改后的内容发送回C 中的客户端。我设法使TCP/IP通信可以很好地用于简单的字符串文本消息,但是当我尝试发送.txt文件的内容时,它就无法做到。
这是我的代码的片段:(
客户端:
fstream file;
string fileName = "Test.txt";
string textToSend;
int sendResult;
file.open(fileName);
while (!file.eof())
{
getline(file, textToSend);
sendResult = send(sock, textToSend.c_str(), textToSend.size() + 1, 0);
}
string end_msg = "end";
sendResult = send(sock, end_msg.c_str(), end_msg.size() + 1 , 0);
file.close();
if (sendResult != SOCKET_ERROR)
{
ZeroMemory(buf, 4096);
int bytesReceived = recv(sock, buf, 4096, 0);
if (bytesReceived > 0)
{
cout << "Server> " << string(buf, 0, bytesReceived) << endl;
}
else
{
cout << "Error on client - bytesReceived" << endl;
}
}
// Gracefully close down everything
closesocket(sock);
WSACleanup();
}
服务器:
// While loop: accept and echo message back to client
char buf[4096];
while (true)
{
ZeroMemory(buf, 4096);
// Wait for client to send data
int bytesReceived = recv(clientSocket, buf, 4096, 0);
if (bytesReceived == SOCKET_ERROR)
{
cerr << "Error in recv(). Quitting!" << endl;
break;
}
else
{
if (bytesReceived == 0)
{
cout << "Client disconnected!" << endl;
break;
}
}
buf[bytesReceived] = ' ';
while (buf != "end")
{
cout << buf << endl;
bytesReceived = recv(clientSocket, buf, 4096, 0); //Here seems to be the problem in debug mode
buf[bytesReceived] = ' ';
}
cout << string(buf, 0, bytesReceived) << endl;
// Echo message back to client
send(clientSocket, buf, bytesReceived + 1, 0);
}
// Close socket
closesocket(clientSocket);
// Shutdown Winsocket
WSACleanup();
}
使用调试模式,我注意到服务器端
bytesReceived = recv(clientSocket, buf, 4096, 0);
它无法到达我的文本文件的第二行。
P.S。:我是C 上TCP/IP的新手,在Java上拥有基本经验,任何帮助都很好,谢谢。
通常需要定义某种通信协议以干净地发送和接收数据。特别是如果您发送和收回多个消息。
这里的数据是从客户端和服务器发送和接收的,而无需考虑哪个状态是其他应用程序。
我想客户端也许已经关闭了连接,并且服务器无法发送答复,因此它不会继续接收下一个消息。
我必须猜测,因为没有提供整个代码来复制。
您应该进行一些调试 - 检查在哪个状态应用程序应用程序以及代码内发生的情况。使用两个IDE-一个用于服务器的IDE,一个用于客户端。
下面是一个简单的客户端,将其信息发送到服务器,服务器只是接收它。
如果需要一个更复杂的方案,那么您必须考虑客户端和服务器之间如何彼此达成共识,如何知道下一步。
客户端和服务器代码根据MSDN:运行Winsock客户端和服务器代码示例
在// Send an initial buffer
中,功能应发送数据并检查已经发送了多少数据。
类似的东西:
std::ifstream file( "Test.txt" );
std::string content( ( std::istreambuf_iterator< char >( file ) ),
( std::istreambuf_iterator< char >() ) );
// Send an initial buffer
int bytes_to_send = content.length();
int bytes_sent = 0;
do
{
iResult = send( ConnectSocket, content.data() + bytes_sent, bytes_to_send, 0 );
if ( iResult != SOCKET_ERROR )
{
bytes_to_send -= iResult;
bytes_sent += iResult;
}
} while ( iResult != SOCKET_ERROR && bytes_to_send > 0 );
在接收方面,代码还必须在示例中的循环中收到:
// Receive until the peer shuts down the connection
do
{
iResult = recv( ClientSocket, recvbuf, recvbuflen, 0 );
if ( iResult > 0 )
{
//...
} while ( iResult > 0 );
我使用了一个2MB测试文件,send
命令通过发送所有数据来工作。
在接收端,在512 Byte批次中收到数据,这意味着循环的许多迭代。
相关文章:
- 如何将 txt 文件中的行分隔为组件C++
- 无法打开.txt文件
- 如何使用CLion在Mac上创建一个新的.txt文件
- 使用fstream对txt文件中的数字进行循环
- Visual Studio在尝试读取resource.txt文件时崩溃
- 如何从txt文件中读取多个不同长度的数组?
- 在一次迭代中从 txt 文件中读取多行
- 通过比较C++中的行在 txt 文件中搜索的最简单方法是什么?
- 写入超过 1GB 的.txt文件时出现奇怪的行为
- 视觉 如何循环访问C++中遵循类似格式的多个.txt文件?
- txt 文件中浮点数的最大和最小值
- 从.txt文件中读取浮点型数字并在公式中使用它们
- C++将文本从.txt文件解析为变量
- 在树结构.txt文件中搜索节点
- 为什么我无法打开/读取从 Python 调用的 C 扩展名中的 txt 文件?
- 在 txt 文件中显示前两个数字的程序
- 如何从具有两列的.txt文件创建并行数组?
- 如何使用c ++读取带有非英文字母的*.txt文件?(国际化)
- 如何像在 C++ 中处理数组一样对 .txt 文件中的字符进行寻址?
- 如何读取 TXT 文件并仅将 int 存储在 C 中