Guest User

Untitled

a guest
Jan 22nd, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.27 KB | None | 0 0
  1. #include <stdio.h>      // printf
  2. #include <stdlib.h>     // exit
  3. #include <unistd.h>     // close
  4. #include <errno.h>      // errno
  5. #include <string.h>     // strerror
  6. #include <netinet/in.h> // sockaddr_in
  7. #include <arpa/inet.h>  // inet_ntop
  8.  
  9. // Useful constants.
  10. #define SERVER_PORT 4567
  11. #define DATA_BUFFER_SIZE 256
  12.  
  13. int main() {
  14.   printf("Starting TCP Server\n\n");
  15.  
  16.   // Create the socket on which we will communicate
  17.   // PF_INET -> Internet Protocol Family, SOCK_STREAM -> TCP
  18.   // Last arg is for socket flags, which we do not set.
  19.   int socket_descriptor = socket(PF_INET, SOCK_STREAM, 0);
  20.     if(socket_descriptor < 0){
  21.         perror("Can't create socket");
  22.         exit(0);
  23.     }
  24.  
  25.   // This will hold the address of our server
  26.   struct sockaddr_in server_address;
  27.  
  28.   // Set up the internet address structure.
  29.   server_address.sin_family = AF_INET;   // Internet address family
  30.  
  31.   // htons correctly sets endianness of port number for the network protocol
  32.   server_address.sin_port = htons(SERVER_PORT); // host-to-network-short
  33.  
  34.   // Set the actually address to a sort of wildcard, so we will listen on
  35.   // any addresses that this host may have (server machines often have multiple
  36.   // network cards!).
  37.   server_address.sin_addr.s_addr = htonl(INADDR_ANY);
  38.  
  39.   // Now bind the socket to the address, so we can soon tell it to listen for
  40.   // connections.
  41.   int result = bind(
  42.     socket_descriptor,    // Socket to bind
  43.     (struct sockaddr *) &server_address, // Address to bind to.
  44.     sizeof (struct sockaddr_in));        // Size of server_address
  45.  
  46.   // Check the status of bind.
  47.   if (result < 0) {
  48.     perror("Could not bind to socket.");
  49.     exit(1);
  50.   }
  51.  
  52.   printf("Socket now bound to the all host address on port %d\n", SERVER_PORT);
  53.  
  54.   // Now put the socket into listening mode with a maximum queue length
  55.   // of 5 connections, after which new connections will be rejected.
  56.   listen(socket_descriptor, 5);
  57.  
  58.   // We're going to go into a loop soon, so that we can accept connections from
  59.   // clients and then read some data from them, so we need to prepare some
  60.   // variables for storing the sender's address and the incoming data.
  61.   struct sockaddr_in sender_address;
  62.   socklen_t sender_address_length;
  63.   sender_address_length = sizeof(sender_address);
  64.   char incoming_data[DATA_BUFFER_SIZE];
  65.  
  66.   printf("Listening for connections\n");
  67.  
  68.   // Loop for ever, accepting connections and responding to messages.
  69.   while (1) {
  70.     // Wait for then accept a connection, storing the client's address.
  71.     int connected_socket = accept(
  72.       socket_descriptor,
  73.       (struct sockaddr*) &sender_address,
  74.       &sender_address_length);
  75.      
  76.                    
  77.      if (connected_socket < 0) {         
  78.           perror("ERROR on accept");
  79.     exit(1);
  80.      }
  81.     // Note: for a multi-threaded server (we'll look at threads soon) this is
  82.     // where you would likely start a new thread, passing it
  83.     // 'connected_socket' as an argument so that the server can continue to
  84.     // accept new requests.
  85.  
  86.     // Wait for then receive some data from the client.
  87.     int no_incoming_bytes = recv(
  88.       connected_socket,   // Note that we use the connected socket here not ...
  89.       &incoming_data,     // ... our listening socket.
  90.       DATA_BUFFER_SIZE,
  91.       0);
  92.  
  93.     // Check...
  94.     if (no_incoming_bytes < 0) {
  95.       perror("Receive error");
  96.       exit(1);
  97.     }
  98.  
  99.     printf("Received: '%s'\n", incoming_data);
  100.  
  101.     // Get the sender's host and port in a format that we can easily print out.
  102.     char ip_address[256];
  103.     inet_ntop(
  104.       AF_INET,
  105.       &(sender_address.sin_addr), // Pass the address by reference.
  106.       (char *) &ip_address,  // It expects char* rather than char[]
  107.       sizeof(ip_address));
  108.     uint16_t port = ntohs(sender_address.sin_port); // Reverse of htons
  109.     printf("Received some data from %s:%d\n", ip_address, port);
  110.  
  111.     // Reply to the client.
  112.     char* response = "Hello from the Server";
  113.     int no_bytes_writen = write(connected_socket,response,strlen(response)+1);
  114.     if (no_bytes_writen != strlen(response)+1) {
  115.       perror("Send error\n");
  116.       exit(1);
  117.     }
  118.  
  119.     // Close the client socket.
  120.     close(connected_socket);
  121.  
  122.   }
  123.  
  124.   // Close the listening socket
  125.   close(socket_descriptor);
  126.  
  127.   return 0;
  128. }
Add Comment
Please, Sign In to add comment