How Implementation Of Chat Between Client And Server | Source code in C Network Programming

To write a program to implement a chat between the client and the server.
Algorithm chat application client and server:
Server Side Chat application algorithm
STEP 1: Start the program.
STEP 2: Declare the variables and structure for the socket.
STEP 3: Create a socket using socket functions
STEP 4: The socket is binded at the specified port.
STEP 5: Using the object the port and address are declared.
STEP 6: If the binding is successful write the message to the client.
STEP 7: Close the socket if the client sends a goodbye message.How Implementation Of Chat Between Client And Server
STEP 8: Execute the client program.
Client Side Chat application Algorithm
STEP 1: Start the program.
STEP 2: Declare the variables and structure.
STEP 3: Socket is created and connects function is executed.
STEP 4: If the connection is successful then server sends the message.
STEP 5: The message from the server is responded by the client.
STEP 6: Stop the program
Chat Application Server Coding
#include<stdio.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<string.h>
main()
{
struct sockaddr_in sa,cli;
int sockfd,conntfd,len,i,l=0,n;
char msg[100],str[100],s;
strcpy(str,”\n message from client:\n);
sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd<0)
{
printf(“\nerror in socket\n);
exit(0);
}
else
printf(“\nsocket opened\n”);
bzero(&sa, sizeof(sa));
sa.sin_family=AF_INET;
sa.sin_port=htons(12345);
sa.sin_addr.s_addr=hton!(0);
if(bind(sockfd,(struct sockaddr*)&sa,sizeof(sa))<0)
{
print(“\nbinding failed\n);
exit(0);
}
else
printf(“\nbinded successfully\n);
listen(sockfd,50);
len=sizeof(cli);
conntfd=accept(sockfd,(struct sockaddr*) &cli,&len);
printf(“\naccepted\n);
memset(msg,’\0’,100);
strcpy(msg,”\nhai\n”);
while(strcmp(msg,”\nbye\n”!=0)
{
l=0;
printf(“\n write your message to your client\n”);
while ((s=getchar()!=’\n’)
{
msg(l)=s;
1++;
}
msg[l]=’\0’;
len=strlen(msg);
write(conntfd,msg,len0;
if(strcmp(msg,”bye”)==0)
{
write(1,”goodbye”,strlen(goodbye));
close(sockfd);
exit(0);
}
memset(msg,’\0’,len-1);
fflush(stdin);
memset(msg,’\0’,100);
n=read(conntfd,msg,100);
{
msg[n]=’\0’;
len=strlen(msg);
msg[len]=’\0’;
write(1,str,strlen(str));
write(1,”\n”,1);
}
if(strcmp(msg,”bye”)==0)
{
write(1,”goodbye”,strlen(goodbye));
close(sockfd);
exit(0);
}
fflush(stdin);
memset(msg,’\0’,len-1);
}
close(sockfd);
}
Client Chat Application Coding
#include<stdio.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<string.h>
main()
{
struct sockaddr_in sa,cli;
char buff[100],str2[100],s;
int sockfd,conntfd,len,i,1=0,n;
char msg[100],str([00],s;
strcpy(str,”\n message from server:\n);
sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd<0)
{
printf(“\nerror in socket\n);
exit(0);
}
else
printf(“\nsocket opened\n”);
bzero(&sa, sizeof(sa));
sa.sin-_family=AF_INET;
sa.sin_port=htons(12345);
sa.sin_addr.s_addr=htonl(0);
if(connect(sockfd,(struct sockaddr*)&sa,sizeof(sa))<0)
{
print(“\nsocket cannot be opened\n);
exit(0);
}
else
printf(“\nconnected successfully\n);
memset(buff,’\0’,100);
while(strcmp(buff,”\nbye\n”!=0)
{
n=read(sockfd,buff,100);
buff[n]=’\0’;
len=strlen(buff);
buff[len]=’\0’;
write(1,str2,strlen(str2));
write(1,buff,len);
write(1,’\n’,1);
if(strcmp(buff,”bye”)==0)
{
write(1,”goodbye”,strlen(goodbye));
close(sockfd);
exit(0);
}
memset(buff,’\0’,len-1);
fflush(stdin);
printf(“write your message to server”);
l=0;
while(s=getchar()!=’\n’)
{
buff[l]=’\0’;
l++;
}
buff[len]=’\0’;
len=strlen(buff);
write(sockfd,buff,len);
if(strcmp(buff,”bye”)==0)
{
write(1,”goodbye”,strlen(goodbye));
close(sockfd);
exit(0);
}
fflush(stdin);
memset(buff,’\0’,len-1);
}
close(sockfd);
}
Output CS1305 Network Lab
CLIENT side output
Socket is opened
Connected successfully
Message from server : hai
Write your message to the server: hello
Message from server: ok
Write your message to server: bye
Good bye
SERVER Side Output
Socket opened
Binded successfully
Accepted
Write your message to the client: hai
Message from client: hello
Message from client: bye
Good bye
RESULT:
Thus the program to implement a chat between the client and the server is executed
and verified . Lab programming in C C++ Free C programming Source code.

Related post