'Sending file through socket goes wrong C
I'm on Windows. I try to send a file from client to server. It works when the server and client are on the same machine. But when I move the client to another machine, the server creates the desired file but fails to copy the client file. I don't understand why it works when server and client are on the same machine but when I move the client to another machine it doesn't.
How can I make my program work so that I can copy a file from client to server?
Server:
char ch[70];
printf("where do you want to save it(full path + name): ");
scanf_s("%s", ch, 70);
char c[70];
printf("which file you want to copy(full path + name): ");
scanf_s("%s", c, 70);
r = send(s, c, 70, 0);
if (r == SOCKET_ERROR)
{
printf("Client disconnected\n");
break;
}
char buf[BUFSIZ];
size_t size = BUFSIZ;
FILE* copyFile;
fopen_s(©File, ch, "wb");
while (size == BUFSIZ)
{
recv(s, buf, BUFSIZ, 0);
if (strncmp(buf, "exit", 10) == 0)
{
break;
}
size = fwrite(buf, 1, BUFSIZ, copyFile);
}
fclose(copyFile);
Client:
char c[70];
res = recv(ClientSocket, c, 70, 0);
if (res == SOCKET_ERROR)
{
printf("Server disconeccted\n");
break;
}
char buf[BUFSIZ];
size_t size = BUFSIZ;
FILE* originalFile;
fopen_s(&originalFile, c, "rb");
while (size == BUFSIZ)
{
size = fread(buf, 1, BUFSIZ, originalFile);
send(ClientSocket, buf, BUFSIZ, 0);
}
send(ClientSocket, "exit", 10, 0);
fclose(originalFile);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
