home *** CD-ROM | disk | FTP | other *** search
/ ftp.disi.unige.it / 2015-02-11.ftp.disi.unige.it.tar / ftp.disi.unige.it / pub / .person / GianuzziV / SO1 / WinUDPsend.c < prev    next >
C/C++ Source or Header  |  2005-03-28  |  2KB  |  97 lines

  1. /***********************************************************
  2. *  UDPsend.cpp   (in coppia con UDPrecv)                   *
  3. *                                                          *
  4. *  Chiamato con 2 parametri:                               *
  5. *    - messaggio da inviare                                *
  6. *    - porta                                               *
  7. *  Invia il messaggio (fino a 10 caratteri) alla porta     *
  8. *  indicata, su localhost                                  *
  9. *  Compilato con MS Visual C++ 6.0                         *
  10. *                                                          *
  11. ************************************************************/
  12.  
  13. #include <stdio.h> 
  14. #include <stdlib.h>
  15. #include "winsock2.h"
  16.  
  17. #define HOSTNAME "localhost"   // IP del receiver
  18.  
  19. int main(int argc,char **argv) 
  20.     SOCKET s;
  21.     struct sockaddr_in destination_addr; 
  22.     WSADATA WSAData;  
  23.     PHOSTENT phostent = NULL;   
  24.  
  25.     // Initiate Windows Sockets. 
  26.     if (WSAStartup (MAKEWORD(1,1), &WSAData) != 0) 
  27.          {
  28.             printf("WSAStartup failed. Error: %d\n",
  29.               WSAGetLastError ());
  30.             exit(0);
  31.          }
  32.  
  33.  
  34.     s=INVALID_SOCKET; 
  35.     if(argc != 3) 
  36.     { 
  37.         printf("usage: %s message port\n",argv[0]); 
  38.         WSACleanup ();
  39.         return 0; 
  40.     }
  41.  
  42.  
  43. // inizializzo il record contenente l'indirizzo del destinatario
  44.  
  45.     destination_addr.sin_family = AF_INET;
  46.     destination_addr.sin_port = htons((unsigned short) atoi(argv[2]));  
  47.     if ((phostent = gethostbyname (HOSTNAME)) == NULL) 
  48.     {
  49.         printf ("Unable to get the host name. Error: %d\n", 
  50.             WSAGetLastError ());
  51.         WSACleanup ();
  52.         exit(1);
  53.     }
  54.  
  55. // Assegno l'indirizzo IP alla struttura.
  56.     memcpy ((char FAR *)&(destination_addr.sin_addr), 
  57.         phostent->h_addr, phostent->h_length);
  58.  
  59. // creo socket  
  60.     s = socket(AF_INET, SOCK_DGRAM, 0); 
  61.     if(s==INVALID_SOCKET) 
  62.     { 
  63.         printf("socket %d\n", s);
  64.         perror("Can't create socket"); 
  65.         return 1; 
  66.     }
  67.  
  68.  
  69. // send data 
  70. // limito il messaggio a 10 caratteri
  71.     if (strlen(argv[1])>10) argv[1][10]='\0';
  72.     if(sendto(s, argv[1], strlen(argv[1]), 0, (struct sockaddr *) 
  73.         &destination_addr, sizeof(struct sockaddr_in))==-1) 
  74.     { 
  75.         printf("Can't send message\n"); 
  76.         closesocket(s); 
  77.         WSACleanup ();
  78.         return 3; 
  79.     } 
  80.  
  81. // chiudo il canale
  82.     if(closesocket(s)) 
  83.     { 
  84.         printf("Error on closing socket\n"); 
  85.         WSACleanup ();
  86.         return 4; 
  87.     } 
  88.  
  89.     WSACleanup ();
  90.     return 0; 
  91.  
  92.  
  93.  
  94.  
  95.