C套接字:收到所有数据后退出客户端(C sockets: Exit client after all data is received)

我是一个C新手学习套接字。 我写了一个客户端服务器程序来接收文件并将其写入另一个文件。

程序本身工作正常 - 文件由服务器正确读取,客户端完全接收,但客户端在收到所有数据后不会退出。

客户端如何知道收到整个文件的时间以及如何退出? 以下是我的客户端代码段。

注意:我添加条件while (data > 0)作为此尝试的一部分。 如果错误请更正。

#define BUFFER 2048 char recived_data[BUFFER]; bzero(recived_data, BUFFER); FILE *new_file = fopen(“Test.jpg”, “w”); int data; do { data = recv(sockfd, recived_data, BUFFER, 0); fwrite(recived_data, 1, sizeof(recived_data), new_file); } while (data > 0);

I am a C newbie learning sockets. I wrote a client-server program to receive a file and write it to another file.

The program itself works fine - the file is read by server properly and the client receives it in full, but the client does not exit after receiving all the data.

How would the client know when the entire file is received and how do I make it exit? Following is my client snippet.

Note: I added the condition while (data > 0) as a part of this attempt. Please correct this if wrong.

#define BUFFER 2048 char recived_data[BUFFER]; bzero(recived_data, BUFFER); FILE *new_file = fopen(“Test.jpg”, “w”); int data; do { data = recv(sockfd, recived_data, BUFFER, 0); fwrite(recived_data, 1, sizeof(recived_data), new_file); } while (data > 0);

最满意答案

在发送完整个文件内容后,您的服务器应该关闭套接字。 这将导致您的recv函数返回并结束客户端的接收循环。

如果由于某种原因想要保持连接,则需要首先向客户端发送一些附加信息(例如文件长度) - 这样客户端就知道一个文件何时结束并且(可能)另一个文件开始。 不过,我不确定你是否对此感兴趣。

Your server should close the socket after the whole file content has been sent. This would cause your recv function to return zero and end the client's receive loop.

If you want to keep the connection for some reason, then you would need to send some additional information to the client first (e.g. file length) - so the client knows when one file ends and (potentially) another begins. I'm not sure if you are interested in that, though.

C套接字:收到所有数据后退出客户端(C sockets: Exit client after all data is received)

我是一个C新手学习套接字。 我写了一个客户端服务器程序来接收文件并将其写入另一个文件。

程序本身工作正常 - 文件由服务器正确读取,客户端完全接收,但客户端在收到所有数据后不会退出。

客户端如何知道收到整个文件的时间以及如何退出? 以下是我的客户端代码段。

注意:我添加条件while (data > 0)作为此尝试的一部分。 如果错误请更正。

#define BUFFER 2048 char recived_data[BUFFER]; bzero(recived_data, BUFFER); FILE *new_file = fopen(“Test.jpg”, “w”); int data; do { data = recv(sockfd, recived_data, BUFFER, 0); fwrite(recived_data, 1, sizeof(recived_data), new_file); } while (data > 0);

I am a C newbie learning sockets. I wrote a client-server program to receive a file and write it to another file.

The program itself works fine - the file is read by server properly and the client receives it in full, but the client does not exit after receiving all the data.

How would the client know when the entire file is received and how do I make it exit? Following is my client snippet.

Note: I added the condition while (data > 0) as a part of this attempt. Please correct this if wrong.

#define BUFFER 2048 char recived_data[BUFFER]; bzero(recived_data, BUFFER); FILE *new_file = fopen(“Test.jpg”, “w”); int data; do { data = recv(sockfd, recived_data, BUFFER, 0); fwrite(recived_data, 1, sizeof(recived_data), new_file); } while (data > 0);

最满意答案

在发送完整个文件内容后,您的服务器应该关闭套接字。 这将导致您的recv函数返回并结束客户端的接收循环。

如果由于某种原因想要保持连接,则需要首先向客户端发送一些附加信息(例如文件长度) - 这样客户端就知道一个文件何时结束并且(可能)另一个文件开始。 不过,我不确定你是否对此感兴趣。

Your server should close the socket after the whole file content has been sent. This would cause your recv function to return zero and end the client's receive loop.

If you want to keep the connection for some reason, then you would need to send some additional information to the client first (e.g. file length) - so the client knows when one file ends and (potentially) another begins. I'm not sure if you are interested in that, though.