UDP Client Server Program in C | Socket Programming | CodeTextPro

In this program, you will learn how to connect Client-Server and sent messages from Client to Server.


Socket Programming


UDP Server Client implementation in C / Socket Programming:

udpserver.c

#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<netdb.h>
#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<stdlib.h>
#define LOCAL_SERVER_PORT 1500
#define MAX_MSG 100
 int main(int argc,char *argv[]) {
  int sd, rc, n, cliLen,num[2],i;
  long int tot;
  struct sockaddr_in cliAddr, servAddr;
  char msg[MAX_MSG],name[MAX_MSG]="Ms,";
  
  sd=socket(AF_INET, SOCK_DGRAM, 0);
  if(sd<0) {
   printf("%s: cannot open socket \n",argv[0]);
  exit(1);
  }
  servAddr.sin_family=AF_INET;
  servAddr.sin_addr.s_addr=htonl(INADDR_ANY);
  servAddr.sin_port=htons(LOCAL_SERVER_PORT);
  rc=bind(sd,(struct sockaddr *)&servAddr,sizeof(servAddr));
  if(rc<0) {
     printf("%s: cannot bind port number %d \n",argv[0],LOCAL_SERVER_PORT);
     exit(1);
     }
    printf("%s: waiting for data on port UDP %u\n",argv[0],LOCAL_SERVER_PORT);
     
     for(i=0;i<2;i++)
      {
         memset(msg,0x0,MAX_MSG);
         cliLen=sizeof(cliAddr);
       n=recvfrom(sd, msg, MAX_MSG, 0, (struct sockaddr *) &cliAddr, &cliLen);
       
        if(n<0) {
        printf("%s: cannot receive data \n",argv[0]);
        continue;
     }
  printf("%s: from %s: UDP %u : %s \n",argv[0],inet_ntoa(cliAddr.sin_addr),ntohs(cliAddr.sin_port),msg);
   strcat(name,msg);
   }
  printf("the name is : %s\n",name);
  return 0;
 }



udpclient.c


#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<netdb.h>
#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<stdlib.h>
#include<sys/time.h>
#define REMOTE_SERVER_PORT 1500
#define MAX_MSG 100
  int main(int argc, char *argv[]) {
     int sd, rc, i;
     struct sockaddr_in cliAddr, remoteServAddr;
     struct hostent *h;
    if(argc<3) {
       printf("usage : %s <server> <data1><data2> \n", argv[0]);
      exit(1);
    }
    
   h=gethostbyname(argv[1]);
   if(h==NULL) {
     printf("%s: unknown host '%s' \n", argv[0], argv[1]);
     exit(1);
     }
   
   printf("%s: sending data to '%s' (IP :%s) \n", argv[0], h->h_name,inet_ntoa(*(struct in_addr *)h->h_addr_list[0]));
    remoteServAddr.sin_family=h->h_addrtype;
    memcpy((char *) &remoteServAddr.sin_addr.s_addr,h->h_addr_list[0], h->h_length);
   remoteServAddr.sin_port=htons(REMOTE_SERVER_PORT);
   
   sd= socket(AF_INET,SOCK_DGRAM,0);
     if(sd<0)  {
        printf("%s: cannont open socket \n",argv[0]);
         exit(1);
      }
      cliAddr.sin_family=AF_INET;
      cliAddr.sin_addr.s_addr=htonl(INADDR_ANY);
      cliAddr.sin_port=htons(0);
     

      rc=bind(sd, (struct sockaddr *) &cliAddr, sizeof(cliAddr));


      if(rc<0) {
      printf("%s: cannort bind port\n", argv[0]);
       exit(1);
    }
      for(i=2;i<argc;i++)
       rc=sendto(sd, argv[i], strlen(argv[i])+1,0,(struct sockaddr *) &remoteServAddr,sizeof(remoteServAddr));
    if(rc<0)  {
      printf("%s: cannnot send data %d \n",argv[0],i-1);
      close(sd);
      exit(1);
      }
     
   return 1;
   }






Post a Comment

0 Comments