我正在編寫一個簡單的客戶端/服務器應用程序,並遇到了一個非常奇怪的問題。客戶端讀取其他文件後不讀取任何數據
我想發送一個.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