我见过很多与我正在做的事情有关的问题,但我还没有能够得到任何解决方案为我工作。在服务器套接字中多次写入客户端套接字程序中的单次读取?
我试着写两个套接字程序(一个客户端和一个服务器),以便服务器能够发送任何类型的文件(按字节)到客户端。我不知道如何构建/协调如下:
(服务器)的读取声明(S),从文件中获取数据(我用的fread())
(在服务器端)向客户端发送数据的写入语句(我正在使用write())
(在客户端中)读取语句以从服务器接收数据(我正在使用read())
(在客户端中)写入wr的语句迭代从服务器接收到的数据到文件(我正在使用fwrite())
我想以字节块传输数据。无论文件有多大,服务器中的一个fread()语句和一个write()语句是否足够? (或者这取决于我的缓冲区的大小?) 在客户端,一个调用read()和一个调用fwrite()就足够了吗? (或者这取决于我的缓冲区的大小?)
我是新来的套接字编程。提前致谢!我的服务器和客户端下面的 配件:
客户端:服务器代码的
/*try to get file from server*/
char receive_buffer[256];
FILE * new_file;
int bytes_received = 0;
new_file = fopen("newfile.txt", "w");
bytes_received = read(conn_s, receive_buffer, 256);
printf("Received %d bytes\n", bytes_received);
while(bytes_received > 0)
{
bytes_received = read(conn_s, receive_buffer, 256);
printf("Received %d bytes\n", bytes_received);
fwrite(receive_buffer, 1, bytes_received, new_file);
printf("writing bytes!\n");
break;
}
if(bytes_received < 0)
{
printf("/nError reading bytes of data/n");
}
配件:
FILE * file_to_get = fopen("sample.txt", "rb");
if(file_to_get == NULL)
{
printf("No such file!");
exit(0);
}
while(1)
{
unsigned char buff[256];
int num_read = fread(buff, 1, 256, file_to_get);
printf("read %d bytes\n", num_read);
if(num_read > 0)
{
write(conn_s, buff, num_read);
printf("writing %d bytes\n", num_read);
}
if(num_read < 256)
{
if(feof(file_to_get))
printf("End of file\n");
if(ferror(file_to_get))
printf("Error reading bytes\n");
break;
}
}
只是一个简短的提示,一定要检查**每个**系统调用失败(即'fopen()'等)(指您的客户端) – RastaJedi
作为一般规则,您应该检查任何系统调用的返回值都会返回字节数并正确处理它返回的任何返回值,这意味着当它返回相同的字节数时(即完全读/写你的缓冲区),并且当你返回的值小于你传入的值时(比如缓冲区的部分读/写),你需要处理它。最后,当它返回0(也就是说,文件),当它返回-1(又名一个错误)。 –