Chat Program Using UDP Socket perform operation in Client Side CS1305- NETWORK LAB

Step by step Algorithm UDP Client
Start the UDP program
Create a socket and connect is with the server
Pass the message to server using sendto().
Receive the message from server using recvfrom()
Chat with the server until the message bye
Finally the terminate the client program
See the BASIC syntax for UDP CHAT program
Source code in C language Programming CS1305- NETWORK LAB
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<string.h>
int main(int argc,char **argv)
{
if(argc<2)
{
printf("insuffcient parameters");
exit(0);
}
struct sockaddr_in servsock;
int sockfd,size;
char msg[1024],str[1024];
if((sockfd=socket(AF_INET,SOCK_DGRAM,0))<0)
{
perror("");
exit(0);
}
printf("socket created\n");
size=sizeof(struct sockaddr);
socklen_t len=sizeof(servsock);
bzero(&servsock,size);
servsock.sin_port=htons(3000);
servsock.sin_family=AF_INET;
servsock.sin_addr.s_addr=inet_addr(argv[1]);
if(strcmp(msg,"bye")==0)
exit(0);
while(strcmp(msg,"bye")!=0)
{ printf("\n client");
scanf("%s",msg);
if((sendto(sockfd,msg,sizeof(msg),0,(struct sockaddr*)&servsock,len))<0)
{
perror("not send");
exit(0);
}
if(strcmp(msg,"bye")==0)
exit(0);
printf("\n from server");
if((recvfrom(sockfd,str,1024,0,NULL,NULL))<0)
{
perror("not received");
exit(0);
}
printf("%s",str);
}
close(sockfd);
return 0; }

Output UDP client
[it28@localhost lapprogram]$cc chat.c
[it28@localhost lapprogram]$./a.out 192.168.1.111
Socket created
Client:lapprogram
From server:john
Client:jebrose
From server:Kevin
Client:jansi

Result
Thus the UDP chat client was performed successfully. CS1305- NETWORK LAB.

Related post