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 :
#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() {
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 :
#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() {
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 :/ .