通过C 中的TCP/IP协议发送.TXT文件

sending a .txt file via tcp/ip protocol in c++

本文关键字:TXT 文件 协议 IP 中的 TCP 通过      更新时间:2023-10-16

我正在尝试制作一个程序,该程序将发送到服务器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批次中收到数据,这意味着循环的许多迭代。