Client - Serveur UDP


Ces deux programmes ne font presque rien mais ils mettent en place les mechanismes de dialogue inter-process.

Serveur


#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <netdb.h>
#include <errno.h>
#include <arpa/inet.h>

#define MSGSIZE 500

main(int argc, char **argv)
{
  int sockfd, cc, addr_in_size;
  u_short portnum = 12345;
  struct sockaddr_in *my_addr, *from;
  char *msg;
  u_long fromaddr;

  addr_in_size = sizeof(struct sockaddr_in);

  msg = (char *)malloc(MSGSIZE);
  from = (struct sockaddr_in *)malloc(addr_in_size);
  my_addr = (struct sockaddr_in *)malloc(addr_in_size);

  memset((char *)my_addr,(char)0,addr_in_size);
  my_addr->sin_family = AF_INET;
  my_addr->sin_addr.s_addr = htonl(INADDR_ANY);
  my_addr->sin_port = portnum;

  if((sockfd = socket (PF_INET, SOCK_DGRAM, 0)) < 0){
    fprintf(stderr,"Error %d in socket: %s\n",errno,sys_errlist[errno]);
    exit(errno);
  };

  if(bind(sockfd, (struct sockaddr *)my_addr, addr_in_size) < 0){
    fprintf(stderr,"Error %d in bind: %s\n",errno,sys_errlist[errno]);
    if(errno != EADDRINUSE) exit(errno);
  };

  fprintf(stdout,"Ready to receive\n");

  while(1){
    if((cc = recvfrom (sockfd,msg,MSGSIZE,0,(struct sockaddr *)from,
           &addr_in_size)) == -1){
      fprintf(stderr,"Error %d in recvfrom: %s\n",
        errno,sys_errlist[errno]);
      exit(errno);
    };
    fromaddr = from->sin_addr.s_addr;
    msg[cc] = '\0';
    fprintf(stdout,"From %s port %d: %s\n",
      (gethostbyaddr((char *)&fromaddr,
         sizeof(fromaddr),
         AF_INET))->h_name,
       from->sin_port,msg);

  }
}

Client

#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>

#define MSGSIZE 500

main(int argc,char **argv)
{
  int sockfd, ok, addr_in_size;
  u_short portnum = 12345;
  struct sockaddr_in *to;
  struct hostent *toinfo;
  char *msg, *htoname = "127.0.0.1";
  u_long toaddr;

  msg = (char *)malloc(MSGSIZE);
  to = (struct sockaddr_in *)malloc(sizeof(struct sockaddr_in));

  if((toinfo = gethostbyname(htoname)) == NULL){
    fprintf(stderr,"Error %d in gethostbyname: %s\n",
      errno,sys_errlist[errno]);
    exit(errno);
  };
  toaddr = *(u_long *)toinfo->h_addr_list[0];

  addr_in_size = sizeof(struct sockaddr_in);
  memset((char *)to,(char)0,addr_in_size);

  to->sin_family = AF_INET;
  to->sin_addr.s_addr = toaddr;
  to->sin_port = /*(htons)*/portnum;

  if((sockfd = socket (PF_INET, SOCK_DGRAM, 0)) == -1){
    fprintf(stderr,"Error %d in socket: %s\n",errno,sys_errlist[errno]);
    exit(errno);
  };

  ok = 1;
  while(ok){
    fprintf(stdout,"Message: ");
    scanf("%s",msg);
    fprintf(stdout,"\n");
    ok = (msg[0] != '0');

    if(sendto(sockfd,msg,strlen(msg),0,(struct sockaddr *)to,
        addr_in_size) == -1){
      fprintf(stderr,"Error %d in sendto: %s\n",errno,sys_errlist[errno]);
      exit(errno);
    }
  }
}