SHARE
TWEET

sirboderafael

a guest Mar 1st, 2009 887 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <sys/types.h>
  2. #include <sys/socket.h>
  3. #include <netinet/in.h>
  4. #include <arpa/inet.h>
  5. #include <stdio.h>
  6. #include <unistd.h>
  7. #include <errno.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10.  
  11. int main()
  12. {
  13.     int sock;
  14.     int addr_len, bytes_read;
  15.     char recv_data[1024];
  16.     struct sockaddr_in server_addr , client_addr;
  17.  
  18.     if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
  19.         perror("Erro: Socket");
  20.         exit(1);
  21.     }
  22.  
  23.     server_addr.sin_family = AF_INET;
  24.     server_addr.sin_port = htons(25000);
  25.     server_addr.sin_addr.s_addr = INADDR_ANY;
  26.     bzero(&(server_addr.sin_zero),8);
  27.  
  28.     if (bind(sock,(struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) {
  29.         perror("Erro: Bind");
  30.         exit(1);
  31.     }
  32.  
  33.     addr_len = sizeof(struct sockaddr);
  34.  
  35.     printf("\n/*Servidor UDP esperando por clientes na porta 25000...*/");
  36.     fflush(stdout);
  37.  
  38.     while (1) {
  39.  
  40.         bytes_read = recvfrom(
  41.             sock,
  42.             recv_data,
  43.             1024,
  44.             0,
  45.             (struct sockaddr *)&client_addr,
  46.             &addr_len
  47.         );
  48.  
  49.         recv_data[bytes_read] = '\0';
  50.  
  51.         printf(
  52.             "\n%s : %d : %s",
  53.             inet_ntoa(client_addr.sin_addr),
  54.             ntohs(client_addr.sin_port),
  55.             recv_data
  56.         );
  57.  
  58.         fflush(stdout);
  59.     }
  60.  
  61.     return 0;
  62. }
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
 
Top