SHARE
TWEET

Untitled

a guest Mar 25th, 2019 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*****************************************************************
  2.         Module d'utilisation d'un socket UDP sur Unix
  3.  
  4.         Stevens Gagnon
  5.         Departement Informatique
  6.         College Shawinigan
  7.  
  8. ******************************************************************/
  9.  
  10. #define MAX_BUFFER 100
  11.  
  12. // ---- Parametres specifique au serveur
  13.  
  14. #define ADR_SERVER  "172.16.83.129"     // "INADDR_ANY" ou "192.168.2.2"
  15. #define PORT_NET  2083              /* Port sur lequel le serveur attend */
  16.  
  17. //----------------------------------------------------------------
  18.  
  19. #include "sg_udp.h"
  20.  
  21. //---------------------------------------------------------------- main
  22.  
  23.  
  24. int main(void)
  25. {
  26.     int serveur;
  27.     int lg_rx, lg_out, lg_tx;
  28.     socklen_t paq_lg;
  29.  
  30.     struct sockaddr_in server, repondant;
  31.  
  32.     char    buffer_in[MAX_BUFFER + 1],
  33.         buffer_out[MAX_BUFFER + 1];
  34.  
  35.  
  36.     client = setup_udp_client();
  37.     server = find_udp_server(ADR_SERVER, PORT_NET);
  38.  
  39.     if (serveur != -1)
  40.     {
  41.         buffer_out[0] = 'A';
  42.         if ((lg_tx = sendto(serveur, buffer_out, lg_out, 0, (struct sockaddr *)&client, paq_lg)) < 0)
  43.         {
  44.             perror("ERREUR sendto");
  45.             exit(EXIT_FAILURE);
  46.         }
  47.         printf("Connecter");
  48.         paq_lg = sizeof(struct sockaddr_in);
  49.         bzero(buffer_in, MAX_BUFFER);
  50.  
  51.         if ((lg_rx = recvfrom(serveur, buffer_in, MAX_BUFFER, 0, (struct sockaddr *)&client, &paq_lg)) < 0)
  52.         {
  53.             perror("ERREUR recvfrom");
  54.             exit(EXIT_FAILURE);
  55.         }
  56.         else
  57.         {
  58.             printf("\n--------------------------------------------------------------------------\n\n");
  59.  
  60.             FILE *fichier;
  61.             if ((fichier = fopen("prog", "w")) == NULL)
  62.             {
  63.                 printf("--------------------- Probleme de fichier\n");
  64.             }
  65.             else
  66.             {
  67.                 fprintf(fichier, "%1c", lg_rx);
  68.             }
  69.  
  70.  
  71.             buffer_in[lg_rx] = 0;
  72.             printf("Le serveur a recu %i bytes ==>>%s<<==\n\n", lg_rx, buffer_in);
  73.  
  74.             bzero(buffer_out, MAX_BUFFER);
  75.             snprintf(buffer_out, MAX_BUFFER, ")%s(", buffer_in);
  76.             lg_out = strlen(buffer_out);
  77.  
  78.             printf("Le serveur va transmettre %i bytes ==>>%s<<==\n\n", lg_out, buffer_out);
  79.  
  80.             printf("\n--------------------------------------------------------------------------\n\n");
  81.         }
  82.     }
  83. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top