Click here to Skip to main content
15,887,430 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi , i'm currently trying to make a Server-Client Chat Application for a school project ,However i can't manage to send a file. If you'd like to execute the code below :
The server takes the port as an arg
the client takes the IP address ( 127.0.0.1 if local) and the port.

What I have tried:

This the code i'm using Server wise :

C++
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>



int sockfd,n,newsockfd;
char buffer[255];


void error(const char *msg)
{
	perror(msg);
	exit(1);
}

void ReceiveFile()
{
	FILE *fp=fopen("fichier.txt","ab+");
	n=read(newsockfd,buffer,255);
	printf("%s",buffer);
	fprintf(fp,"%s",buffer);
	fclose(fp);
}
void EnvoyerMessage() // SEND MESSAGE
{
	fgets(buffer,255,stdin);
	n=write(newsockfd,buffer,strlen(buffer));
	if(n<0)
	{
		error("ERROR writing to socket");
	}
	int i=strncmp("Bye",buffer,3);
}

void Chat()
{
	while(1)
	{
		bzero(buffer,256);
		n=read(newsockfd,buffer,255);
		if(n<0)
		{
			error("ERROR reading from socket");
		}

		if(strncmp(buffer,"/SEND",5)==0)
		{
			ReceiveFile();
		}
		printf("Client : %s\n",buffer);
		EnvoyerMessage();
	}
	close(newsockfd);
	close(sockfd);
}

void Serveur(char **argv)
{
	int portno;
	socklen_t clilen;
	struct sockaddr_in serv_addr,cli_addr;
	sockfd=socket(AF_INET,SOCK_STREAM,0);
	if(sockfd<0)
	{
		error("ERROR opening socket");
	}
	bzero((char *) &serv_addr,sizeof(serv_addr));
	portno=atoi(argv[1]);
	serv_addr.sin_family=AF_INET;
	serv_addr.sin_addr.s_addr=INADDR_ANY;
	serv_addr.sin_port=htons(portno);
	if(bind(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) <0)
	{
		error("ERROR on binding");
	}
	listen(sockfd,5);
	clilen=sizeof(cli_addr);
	newsockfd=accept(sockfd,(struct sockaddr *) &cli_addr,&clilen);
	if(newsockfd < 0)
	{
		error("ERROR on accept");
	}

	if(n<0)
	{
		error("ERROR reading from socket");
	}
	Chat();
}

int main(int argc,char *argv[])
{
	if(argc<2)
	{
		fprintf(stderr,"ERROR, no port provided\n");
		exit(1);
	}
	Serveur(argv);
}


And here is the code client wise :
C++
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/stat.h>


int sockfd,n;
char buffer[256];

void error(const char *msg)
{
	perror(msg);
	exit(0);
}
void SendFile()
{
	FILE *fp;
	fp=fopen("fichier.txt","ab+");
	fgets(buffer,255,fp);
	n=write(sockfd,buffer,255);
	fclose(fp);
}
void RecevoirMessage() // RECEIVE MESSAGE
{
	bzero(buffer,256);
	n=read(sockfd,buffer,255);
	if(n<0)
	{
		error("ERROR reading from socket");
	}
	printf("Server : %s\n",buffer);
	int i=strncmp("Bye",buffer,3);
}
void Chat()
{
	while(1)
	{
		bzero(buffer,256);
		fgets(buffer,255,stdin);
		n=write(sockfd,buffer,strlen(buffer));
		if(strncmp(buffer,"/SEND",5)==0)
		{
			
			SendFile();
		}
		if(n<0)
		{
			error("ERROR writing to socket");
		}
		RecevoirMessage();
	}
}
int ConnexionServeur(char *argv[])
{
	int portno;
	struct sockaddr_in serv_addr;
	struct hostent *server;
	portno=atoi(argv[2]);
	sockfd=socket(AF_INET, SOCK_STREAM, 0);
	if(sockfd < 0)
	{
		error("ERROR opening socket");
	}
	server = gethostbyname(argv[1]);
	if(server==NULL)
	{
		fprintf(stderr,"ERROR ,no such host\n");
		exit(0);
	}
	bzero((char *) &serv_addr,sizeof(serv_addr));
	serv_addr.sin_family=AF_INET;
	bcopy((char *)server->h_addr,(char*)&serv_addr.sin_addr.s_addr,server->h_length);
	serv_addr.sin_port=htons(portno);
	if(connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
	{
		error("ERROR connecting");
	}
	Chat();
}
int main(int argc,char **argv)
{

	if(argc < 3)
	{
		fprintf(stderr,"usage %s hostname port\n", argv[0]);
		exit(0);
	}
	ConnexionServeur(argv);
}


What i can't figure out is that when i call Send / Receive Files functions directly without having to go through the Chat() it works . However , as soon as i go through the chat function is stops working and i receive a blank file :/ .
Posted
Updated 17-Dec-18 4:49am
v2
Comments
Afzaal Ahmad Zeeshan 16-Dec-18 17:51pm    
And what exactly is the problem ?

1 solution

The app I work on maintains socket connections with a group of slave computers. We send a variety of different data across the connection and we have a header field that goes with every piece of data. It has a flag, a type, a command field, and a length. With the type and command we can determine what to do with the data. One type of data we send is the actual program so it can update itself. I recommend adopting something similar so you can distinguish between textual data from the chat and data for a file.

One more thing to keep in mind is the size of data in a single ethernet packet is less than 1500 bytes. That is not a problem with a chat app but it can be an issue when transmitting files. You will need to implement a multiple packet receiving function. Sending a multi-packet message is not much of an issue but receiving one can be. The alternative is to split the file into chucks and send them one by one. I think it is better to handle multi-packet messages because it can be very useful once you have it figured out.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900