I am trying to send files via sockets. I created the program and works for file types such as .cpp, .txt and other text files. But binary files, images (.jpg, .png) and compressed files such as .zip and .rar are not sent properly. I know that this is not related to file size because I tested large .txt files. I do not know the problem, I get all the bytes sent, but the file cannot be opened. In most cases, the file is damaged and cannot be viewed. I searched through Google for a solution and just found others with the same problem and without a solution. Therefore, by helping me, you are also helping someone else need a solution.
Server Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
int main ( int agrc, char *argv[] )
{
int Main_Socket;
int Communication_Socket;
int Status;
struct sockaddr_in Server_Address;
struct sockaddr_in Client_Address;
int Port;
char Buff[100] = "";
Port = atoi(argv[2]);
printf ("Server Communicating By Using Port %d\n", Port);
Main_Socket = socket ( AF_INET, SOCK_STREAM, 0 );
if ( Main_Socket == -1 )
{
printf ("Sorry System Can Not Create Socket!\n");
}
Server_Address.sin_family = AF_INET;
Server_Address.sin_port = htons(Port);
Server_Address.sin_addr.s_addr = inet_addr(argv[1]);
Status = bind ( Main_Socket, (struct sockaddr*)&Server_Address, sizeof(Server_Address) );
if ( Status == -1 )
{
printf ("Sorry System Can Not Bind Address to The Socket!\n");
}
listen (Main_Socket,12);
socklen_t Lenght = sizeof (Client_Address);
while (1)
{
Communication_Socket = accept ( Main_Socket, (struct sockaddr*)&Client_Address, &Lenght );
if (!fork())
{
FILE *fp=fopen("recv.jpeg","w");
while(1)
{
char Buffer[2]="";
if (recv(Communication_Socket, Buffer, sizeof(Buffer), 0))
{
if ( strcmp (Buffer,"Hi") == 0 )
{
break;
}
else
{
fwrite(Buffer,sizeof(Buffer),1, fp);
}
}
}
fclose(fp);
send(Communication_Socket, "ACK" ,3,0);
printf("ACK Send");
exit(0);
}
}
return 0;
}
Client code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
int main ( int agrc, char *argv[] )
{
int Socket;
struct sockaddr_in Server_Address;
Socket = socket ( AF_INET, SOCK_STREAM, 0 );
if ( Socket == -1 )
{
printf ("Can Not Create A Socket!");
}
int Port ;
Port = atoi(argv[2]);
Server_Address.sin_family = AF_INET;
Server_Address.sin_port = htons ( Port );
Server_Address.sin_addr.s_addr = inet_addr(argv[1]);
if ( Server_Address.sin_addr.s_addr == INADDR_NONE )
{
printf ( "Bad Address!" );
}
connect ( Socket, (struct sockaddr *)&Server_Address, sizeof (Server_Address) );
FILE *in = fopen("background.jpeg","r");
char Buffer[2] = "";
int len;
while ((len = fread(Buffer,sizeof(Buffer),1, in)) > 0)
{
send(Socket,Buffer,sizeof(Buffer),0);
}
send(Socket,"Hi",sizeof(Buffer),0);
char Buf[BUFSIZ];
recv(Socket, Buf, BUFSIZ, 0);
if ( strcmp (Buf,"ACK") == 0 )
{
printf("Recive ACK\n");
}
close (Socket);
fclose(in);
return 0;
}