Peter N Peter N - 1 month ago 22
C Question

Client-side not creating file

So, I have a working socket. I'm basically making a socket to allow me to transfer files from one program to another program. Everything is working except on the client side, it won't create the file?

Server:

int main() {
WSADATA Winsock;
SOCKET Socket, Sub;
Addr addr;
IncomingAddress incomingAddress;
int AddressLen = sizeof(IncomingAddress);

// Start up Winsock
WSAStartup(MAKEWORD(2, 2), &Winsock);

if (LOBYTE(Winsock.wVersion) != 2 || HIBYTE(Winsock.wVersion) != 2) {
WSACleanup();
return 0;
}

Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

// Memset alternative
ZeroMemory(&addr, sizeof(Addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(portNumber);
bind(Socket, (sockaddr*) &addr, sizeof(Addr));

if (listen(Socket, 1) == SOCKET_ERROR) {
printf("Listening error!\n");
} else {
printf("Listening...\n");
}

if (Sub = accept(Socket, (sockaddr*) &incomingAddress, &AddressLen)) {
char *ClientIP = inet_ntoa(incomingAddress.sin_addr);
int ClientPort = ntohs(incomingAddress.sin_port);
printf("Client connected!\n");
printf("IP: %s:%d\n", ClientIP, ClientPort);

printf("Sending file... \n");

FILE *File;
char *Buffer;
unsigned long Size;

File = fopen("test.txt", "rb+");
if (!File) {
printf("Error while reading the file!\n");
getchar();
return 0;
}

fseek(File, 0, SEEK_END);
Size = ftell(File);
fseek(File, 0, SEEK_SET);

Buffer = malloc(Size);

fread(Buffer, Size, 1, File);
char cSize[MAX_PATH];
sprintf(cSize, "%i", Size);

fclose(File);
send(Sub, cSize, MAX_PATH, 0); // File size
}
return 0;
}


Client:

int main() {
WSADATA Winsock;
SOCKET Socket;
Addr addr;
IncomingAddress incomingAddress;
int AddressLen = sizeof(IncomingAddress);

WSAStartup(MAKEWORD(2, 2), &Winsock);

if (LOBYTE(Winsock.wVersion) != 2 || HIBYTE(Winsock.wVersion) != 2) {
WSACleanup();
return 0;
}

Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

ZeroMemory(&addr, sizeof(Addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr("127.0.0.1");
addr.sin_port = htons(6000);

if (connect(Socket, (sockaddr*) &addr, sizeof(Addr)) < 0) {
printf("Connection Failed!\n");
getchar();
return 0;
}

printf("Connection successful! \n");
printf("Receiving file... \n");

int Size;
char *Filesize = malloc(1024);

if (recv(Socket, Filesize, 1024, 0)) { // File Size
Size = atoi((const char*) Filesize);
printf("File size: %d\n", Size);
}

char *Buffer = malloc(Size);

int Offset = 0;
while (Size > Offset) {
int Amount = recv(Socket, Buffer + Offset, Size - Offset, 0);

if (Amount <= 0) {
printf("Error: " + WSAGetLastError());
break;
} else {
Offset += Amount;
printf("2\n");
}
}

FILE *File;
File = fopen("test.txt", "wb+");
fwrite(Buffer, 1, Size, File);
fclose(File);

getchar();
closesocket(Socket);
WSACleanup();
return 0;
}

Answer

You're sending merely the lenght of the file, but not the file itself to the client:

    send(Sub, cSize, MAX_PATH, 0); // File size

This never recieves anything:

    int Amount = recv(Socket, Buffer + Offset, Size - Offset, 0);

So yeah, just send the actual file as well.