我正在编写一个简单的客户端/服务器应用程序,并遇到了一个非常奇怪的问题。客户端读取其他文件后不读取任何数据
我想发送一个.zip文件到客户端,然后再发送一些数据。 发送.zip工作正常,服务器写入套接字,客户端从套接字读取,正如预期的那样。
之后的问题是。 服务器运行正常,并一直写入套接字,但客户端不会读取任何内容。无论我尝试发送什么,它都会停留在接下来的read()调用中。
我检查过套接字描述符是否正常,它们是。我也认为,可能没有足够的数据在客户端读取套接字,但肯定是。
我也试图做同样的读/写之前和发送该.zip文件后:它工作正常之前,但客户没有看到它后发送该.zip文件。 我没有想法。
这是我用来发送的.zip功能:
typedef struct thData{
int idThread; //thread ID
int cl; //client descriptor
}thData;
void send_info(struct thData tdL)
{
char file_path[256]="v1.zip";
char sd_buffer[256];
bzero(sd_buffer, 256);
FILE *fd = fopen(file_path, "rb");
if(fd == NULL)
{
printf("ERROR: %s not found.\n", file_path);
exit(1);
}
int read_size;
int write_size;
while((read_size = fread(sd_buffer, sizeof(char), 256, fd)) > 0)
{
if((write_size=write(tdL.cl, sd_buffer, read_size)) < 0)
{
perror("ERROR: writing to client: \n");
break;
}
bzero(sd_buffer, 256);
}
}
我使用接收的.zip什么:
void receive_info(int sd) //sd being the socket descriptor
{
char* file_path = "subject.zip";
char received_buffer[256];
int total_received=0;
int total_wrote=0;
FILE *fd = fopen(file_path, "wb");
if(fd == NULL)
printf("Cannot open %s\n", file_path);
else
{
bzero(received_buffer, 256);
int read_size = 0;
while((read_size = read(sd, received_buffer, 256)) > 0)
{
total_received=total_received+read_size;
int write_size = fwrite(received_buffer, sizeof(char), read_size, fd);
total_wrote=total_wrote+read_size;
if(write_size < read_size)
{
perror("ERROR: \n");
}
bzero(received_buffer, 256);
if (read_size == 0 || read_size != 256)
{
break;
}
}
if(read_size < 0)
{
perror("ERROR: reading: ");
exit(1);
}
fclose(fd);
}
}
任何帮助将不胜感激。
谢谢!你对额外的字节是正确的! – user3211838