Echo Server - UDP - Networks & DBMS Lab - C Program

Program :

//Server program : echos.c

#include<stdio.h>
#include<string.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
main()
{
    struct sockaddr_in client,server;
    int s,n;
    char b1[100],b2[100];
    s=socket(AF_INET,SOCK_DGRAM,0);
    server.sin_family=AF_INET;
    server.sin_port=2000;
    server.sin_addr.s_addr=inet_addr("127.0.0.1");
    bind(s,(struct sockaddr *)&server,sizeof(server));
    printf("\nEcho Server ready,waiting for client....\n");
    n=sizeof(client);
    while(1)
    {
        recvfrom(s,b1,sizeof(b1),0,(struct sockaddr *) &client,&n);
    if(!(strcmp(b1,"end")))
            break;
        sendto(s,b1,sizeof(b1),0,(struct sockaddr *) &client,n);
              
    }
 
}

// Client Program : (echoc.c)

#include<stdio.h>
#include<string.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
main()
{
    struct sockaddr_in server,client;
    int s,n;
    char b1[100],b2[100];
    s=socket(AF_INET,SOCK_DGRAM,0);
    server.sin_family=AF_INET;
    server.sin_port=2000;
    server.sin_addr.s_addr=inet_addr("127.0.0.1");
    printf("\nClient ready....\n");
    n=sizeof(server);
    while(1)
    {
        printf("\nClient:");
        gets(b2);
        sendto(s,b2,sizeof(b2),0,(struct sockaddr *) &server,n);
        if(strcmp(b2,"end")==0)
            break;
        recvfrom(s,b1,sizeof(b1),0,NULL,NULL);
        printf("\nServer:%s",b1);
    }

}

Output :

Terminal 1: (Server)
student@cselab-desktop:~$ gcc echos.c -o server
student@cselab-desktop:~$ ./server

Echo Server ready,waiting for client....
student@cselab-desktop:~$

Terminal 2: (Client)
student@cselab-desktop:~$ gcc echoc.c -o client
/tmp/cc0rsfpG.o: In function `main':
echoc.c:(.text+0x8b): warning: the `gets' function is dangerous and should not be used.
student@cselab-desktop:~$ ./client

Client ready....

Client:hi,2k8618...

Server:hi,2k8618...
Client:www.2k8cs.tk

Server:www.2k8cs.tk
Client:end
student@cselab-desktop:~$

3 comments:

  1. Yeah It's good I appreciate this article. I recommand it to my friends,I,ll be back here again and again . Visit our blog for more .

    ReplyDelete
  2. StoreemartEcommerce Website Development Companyin india . We are Provided E-Commerce Web Design and development services increase your conversion rates and online visibility You Can Visit website and Contact Now Today.

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...