piffy

UDP Client (C)

Aug 8th, 2015
313
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <sys/socket.h>
  3. #include <netinet/in.h>
  4. #include <string.h>
  5.  
  6. int main(){
  7.   int sock, portNum, nBytes;
  8.   char buffer[1024];
  9.   struct sockaddr_in serverAddr;
  10.   socklen_t addr_size;
  11.  
  12.   /** Crea socket **/
  13.   sock = socket(PF_INET, SOCK_DGRAM, 0);
  14.  
  15.   /** Configurazione dati server **/
  16.   serverAddr.sin_family = AF_INET;
  17.   serverAddr.sin_port = htons(6666);
  18.   serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
  19.   memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);  
  20.   addr_size = sizeof serverAddr;
  21.  
  22.  
  23.     printf("Scrivi la frase da inviare al server: ");
  24.     fgets(buffer,1024,stdin);
  25.     printf("Hai scritto: %s",buffer);
  26.  
  27.     nBytes = strlen(buffer) + 1;
  28.    
  29.     /** Invia messaggio **/
  30.     sendto(sock,buffer,nBytes,0,(struct sockaddr *)&serverAddr,addr_size);
  31.  
  32.   close (sock);
  33.  
  34.   return 0;
  35. }
RAW Paste Data