matheus__serpa

Socket

Mar 29th, 2015
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.51 KB | None | 0 0
  1. /* http://www.linuxhowtos.org/C_C++/socket.htm */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>    /* atoi */  
  5. #include <unistd.h>
  6.  
  7. #include <strings.h>   /* bzero */
  8. #include <arpa/inet.h> /* htons */
  9.  
  10. #include <sys/types.h>
  11. #include <sys/socket.h>
  12. #include <netinet/in.h>
  13.  
  14. #define BUFFER_SIZE 256
  15. #define BACKLOG 5 /* defines the maximum length to which the queue of pending connections. */
  16.  
  17. int main(int argc, char **argv){
  18.    
  19.     if(argc != 3){
  20.         fprintf(stderr, "Usage: %s <PORT> <FILE_TO_SEND> \n", argv[0]);
  21.         exit(EXIT_FAILURE);
  22.     }
  23.  
  24.     int socketFD, newsocketFD; /* file descriptors. */
  25.     int port = atoi(argv[1]); /* port number. */
  26.     socklen_t clientSize /* size of the address of the client. */;
  27.     int n; /* number of characters read or written. */
  28.     char buffer[BUFFER_SIZE]; /* server reads from the socket into this buffer. */
  29.     struct sockaddr_in serv_addr, cli_addr;
  30.  
  31.     socketFD = socket(AF_INET, SOCK_STREAM, 0); /* create an endpoint for communication. */
  32.     if(socketFD < 0){
  33.         fprintf(stderr, "Socket doesn't opening. \n");
  34.         exit(EXIT_FAILURE);
  35.     }
  36.  
  37.     bzero((char *) &serv_addr, sizeof(serv_addr)); /* sets all values in serv_addr to zero. */
  38.  
  39.     serv_addr.sin_family = AF_INET; /* code for the address family. */
  40.     serv_addr.sin_port = htons(port); /* convert the port to network byte order.*/
  41.     serv_addr.sin_addr.s_addr = INADDR_ANY; /* IP address of the host. */
  42.  
  43.     if(bind(socketFD, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0){ /* binds a socket to an address. */
  44.         fprintf(stderr, "Socket doesn't binds to an address. \n");
  45.         exit(EXIT_FAILURE);
  46.     }
  47.  
  48.     listen(socketFD, BACKLOG); /* listen for connections on a socket. */
  49.  
  50.  
  51.     clientSize = sizeof(cli_addr);
  52.     newsocketFD = accept(socketFD, (struct sockaddr *) &cli_addr, &clilen); /* accept a connection on a socket. */
  53.     if (newsocketFD < 0){
  54.         fprintf(stderr, "Socket doesn't accept a new connection. \n");
  55.         exit(EXIT_FAILURE);
  56.     }
  57.  
  58.     bzero(buffer, BUFFER_SIZE); /* sets all values in buffer to zero. */
  59.  
  60.     n = read(newsocketFD, buffer, BUFFER_SIZE - 1); /* read from a file descriptor. */
  61.  
  62.     if(n < 0){
  63.         fprintf(stderr, "Socket doesn't read to the client socket. \n");
  64.         exit(EXIT_FAILURE);
  65.     }
  66.  
  67.     printf("Here is the message: %s\n", buffer);
  68.  
  69.     n = write(newsocketFD, "I got your message", 18); /* write to a file descriptor. */
  70.  
  71.     if(n < 0){
  72.         fprintf(stderr, "Socket doesn't write to the client socket. \n");
  73.         exit(EXIT_FAILURE);
  74.     }
  75.  
  76.     /* close a file descriptor. */
  77.     close(newsocketFD);
  78.     close(socketFD);
  79.  
  80.     return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment