pickzy.com

C  |  C++  |  Objective-C  |  VC++  |  Win32  |  MFC  |  Java  |  Php  |  Delphi  |  Visual Basic  |  .Net  |  Networking  |  General  |  Games  |  Jobs  |  Javascript  |  




Menu

pickSourcecode.com


        

 




 

C > Programs

 

C UDP socket example Server and Client program


//  Server

/*  Creates  a  datagram  server.    The  port  
     number  is  passed  as  an  argument.    This
     server  runs  forever  */


#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>

#include <arpa/inet.h> 
#include <assert.h> 
#include <errno.h> 
#include <signal.h> 
#include <string.h> 
#include <sys/wait.h> 
#include <netdb.h> 
#include <unistd.h> 

void error(char *msg)
{

    perror(msg);

    exit(0);

}


struct Test {

char name[20];
int  num;
} test_t;


int main(int argc, char *argv[])
{

   
int sock, length, fromlen, n;
   struct sockaddr_in server;

   struct sockaddr_in from;

   
char buf[1024];
   struct Test test_name;


   
if (argc < 2) {
      fprintf(stderr, "ERROR, no port provided\n");

      exit(0);

   }


   sock=socket(AF_INET, SOCK_DGRAM, 0);

   
if (sock < 0) error("Opening socket");
   length = sizeof(server);

   bzero(&server,length);

   server.sin_family=AF_INET;

   server.sin_addr.s_addr=INADDR_ANY;

   server.sin_port=htons(atoi(argv[1]));

   
if (bind(sock,(struct sockaddr *)&server,length)<0) 
       error("binding");

   fromlen = sizeof(struct sockaddr_in);

   while (1) {

       n = recvfrom(sock,&test_name,1024,0,(struct sockaddr *)&from,&fromlen);

       
if (n < 0) error("recvfrom");
       write(1,"Received a datagram: ",21);

       write(1,test_name.name,strlen(test_name.name));

       printf("Received value: %d\n", test_name.num);

       n = sendto(sock,"Got your message\n",17,

                  0,(struct sockaddr *)&from,fromlen);

       
if (n  < 0) error("sendto");
   }

 }



//  Client


/*  UDP  client  in  the  internet  domain  */
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>

#include <arpa/inet.h> 
#include <assert.h> 
#include <errno.h> 
#include <signal.h> 
#include <string.h> 
#include <sys/wait.h> 
#include <netdb.h> 
#include <unistd.h> 

struct Test {

char name[20];
int  num;
} test_t;


void error(char *);
int main(int argc, char *argv[])
{

   
int sock, length, n;
   struct sockaddr_in server, from;

   struct hostent *hp;

   
char buffer[256];
   struct Test test_name;


   test_name.num = 10;


   
if (argc != 3) { printf("Usage: server port\n");
                    exit(1);

   }

   sock= socket(AF_INET, SOCK_DGRAM, 0);

   
if (sock < 0) error("socket");

   server.sin_family = AF_INET;

   hp = gethostbyname(argv[1]);

   
if (hp==0) error("Unknown host");

   bcopy((
char *)hp->h_addr, 
        (
char *)&server.sin_addr,
         hp->h_length);

   server.sin_port = htons(atoi(argv[2]));

   length=sizeof(struct sockaddr_in);

   printf("Please enter the message: ");

   bzero(buffer,256);

   fgets(test_name.name,255,stdin);

   n=sendto(sock,&test_name, 255,0,&server,length);

   
if (n < 0) error("Sendto");
   n = recvfrom(sock,buffer,256,0,&from, &length);

   
if (n < 0) error("recvfrom");
   write(1,"Got an ack: ",12);

   write(1,buffer,n);

}


void error(char *msg)
{

    perror(msg);

    exit(0);

}





 
Privacy Policy | About Us