pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

C pastebin - collaborative debugging tool View Help


Posted by sirboderafael on Sun 1 Mar 19:35
report abuse | download | new post

  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. }

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


Remember me so that I can delete my post