Code:
#include <unistd.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/socket.h>
#include <stdio.h>
#include <readline/readline.h>
#define max 4096
int main()
{
int cpid,sd,nsd,i,port=5200;
char fname[30],line[max];
struct sockaddr_in ser;
struct sockaddr_in cli;
ssize_t n;
if ((sd=socket(AF_INET,SOCK_STREAM,0))<0)
{
printf("\nError:Socket Creation");
return 0;
}
bzero((char*)&ser,sizeof(ser));
printf("\nPort Address is %d",port);
ser.sin_family=AF_INET;
ser.sin_port=htons(port);
ser.sin_addr.s_addr=htonl(INADDR_ANY);
if (bind(sd,(struct sockaddr*)&ser,sizeof(ser))<0)
{
printf("\nError:Binding");
return 0;
}
i=sizeof(cli);
listen(sd,1);
printf("Server Module\n");
nsd=accept(sd,(sd,(struct sockaddr*)&cli),&i);
if (nsd==-1)
{
printf("\nError:Client accepts the problem");
return 0;
}
printf("Client Accepted");
printf("Start Chatting");
if (cpid=(fork()<0)) { // this is the child process
close(sd); // child doesn't need the listener
scanf("%s",line);
if (send(nsd, line,sizeof(line), 0) == -1)
printf("send");
close(nsd);
}
if(cpid>0)
{
//receive the message steps
}
close(sd);
return 0;
}
// client program
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define max 4096
int main()
{
int n,sd,nsd,i,port=5200;
char line[max],file[30];
struct sockaddr_in ser;
int childpid;
if ((sd=socket(AF_INET,SOCK_STREAM,0))<0)
{
printf("\nError:Socket Creation");
return 0;
}
bzero((char *)&ser,sizeof(ser));
printf("\nPort Address is %d",port);
ser.sin_family=AF_INET;
ser.sin_port=htons(port);
ser.sin_addr.s_addr=htonl(INADDR_ANY);
if (connect(sd,(struct sockaddr*)&ser,sizeof(ser))==-1)
{
printf("\nError:Binding");
return 0;
}
printf("Client Module");
printf("\nClient:");
if (fork()) { // this is the child process
close(sd); // child doesn't need the listener
if ((n=recv(sd, line, max, 0)) == -1) {
printf("recv");
exit(1);
}
line[n] = '\0';
printf("Received: %s",line);
close(nsd);
}
close(sd);
return 0;
}