I have a problem that if I play this program, the connection between client and server has been successful. But after that, when we try the client to upload or download jpg file from server, there is some problem. I guess there is some while statement going forever.
client.c
int upload(int sock, const char* file_name){
FILE *file_fd;
char buff[1024];
int bytesread, bytessent = 0;
send(sock, "UPLOAD", strlen("UPLOAD"), 0);
send(sock, file_name, strlen(file_name), 0);
file_fd = fopen(file_name, "rb");
while(bytesread = fread(buff, 1, sizeof(buff), file_fd) > 0)
{
bytessent = send(sock, buff, bytesread, 0);
}
fclose(file_fd);
remove(file_name);
return True;
}
int download(int sock, const char* file_name){
FILE *file_fd;
char buff[1024];
int bytesgot = 0;
send(sock, "DOWNLOAD", strlen("DOWNLOAD"), 8);
send(sock, file_name, strlen(file_name), 0);
file_fd = fopen(file_name, "wb");
while(bytesgot = recv(sock, buff, sizeof(buff), 0) > 0)
{
fwrite(buff, 1, bytesgot, file_fd);
}
fclose(file_fd);
return True;
}
server.c
while(True){
n = recv(clnt_sock, buffer1, sizeof(buffer1), 0);
buffer1[n] = '';
// upload
if(strncmp(buffer1, "UPLOAD", 6) == 0)
{
n = recv(clnt_sock, filename, sizeof(filename), 0);
file_fd = fopen(filename, "wb");
filename[n] = '';
while((bytesread = recv(clnt_sock, buffer2, sizeof(buffer2), 0)) > 0) //problem
{
fwrite(buffer2, 1, bytesread, file_fd);
}
fclose(file_fd);
}
// download
else if(strncmp(buffer1, "DOWNLOAD", 8) == 0)
{
n = recv(clnt_sock, filename, sizeof(filename), 0);
filename[n] = '';
file_fd = fopen(filename, "rb");
while((bytesread = fread(buffer2, 1, sizeof(buffer2), file_fd)) > 0)
{
send(clnt_sock, buffer2, bytesread, 0);
}
fclose(file_fd);
remove(filename);
}
I tried many different codes, but it has not been solved.
New contributor
JUSTIN JANG is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.