Guest User

Untitled

a guest
Jan 22nd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.69 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. #include <netdb.h>      // gethostbyname
  9.  
  10. // Useful constants
  11. #define SERVER_HOSTNAME "localhost"
  12. #define SERVER_PORT 4567
  13. #define DATA_BUFFER_SIZE 256
  14.  
  15. int main() {
  16.   printf("Starting TCP Client\n\n");
  17.  
  18.   // Create the socket on which we will communicate PF_INET -> Internet Protocol
  19.   // Family, SOCK_STREAM -> TCP, Last arg is for socket flags, which we do not
  20.   // set.
  21.   // A socket is an abstract communication device within the kernel and,
  22.   // much like a file, the integer descriptor used by the process is simply an
  23.   // index to the kernel's socket object that is associated with this process
  24.   // as state in the Process Control Block.
  25.   int socket_descriptor = socket(PF_INET, SOCK_STREAM, 0);
  26.     if(socket_descriptor < 0){
  27.         perror("Socket: ");
  28.         exit(0);
  29.     }
  30.  
  31.   // This will hold the socket address of the server to which we will connect.
  32.   // This will include the IP address and port, as defined in the IP version 4.
  33.   struct sockaddr_in server_address;
  34.  
  35.   // Set up the internet address structure.
  36.   server_address.sin_family = AF_INET;   // Internet (IPv4) address family
  37.  
  38.   // Since the network protocol does not necessarily use the same byte endianess
  39.   // as the host CPU, htons() ensures it is converted correctly.
  40.   server_address.sin_port = htons(SERVER_PORT); // host-to-network-short
  41.  
  42.   // Resolve the hostname to an IP address, ultimately via a DNS look-up over
  43.   // the network if it cannot be satisfied from the local cache.
  44.   struct hostent *hostinfo;
  45.   hostinfo = gethostbyname(SERVER_HOSTNAME);
  46.   if (hostinfo == NULL) {
  47.     fprintf(stderr, "Unknown host %s.\n", SERVER_HOSTNAME);
  48.     exit(1);
  49.   }
  50.   // Set the address to that returned from the lookup.  Note the pointer
  51.   // manipulation that is happening here: we cast the h_addr pointer to a
  52.   // pointer to an in_addr (for holding IPv4 addresses), then we get the value
  53.   // pointed to and store it in sin_addr, so the address has been copied.
  54.   server_address.sin_addr = *(struct in_addr *) hostinfo->h_addr;
  55.  
  56.   // Connect the socket to the destination host.
  57.   int result = connect(
  58.     socket_descriptor, // The socket we are using to communicate with
  59.     (struct sockaddr*) &server_address, // The server's IPv4 address cast to
  60.                                         // a generic address.
  61.     sizeof(struct sockaddr_in));        // The size of our address.
  62.  
  63.   // Check if connect failed.
  64.   if (result != 0) {
  65.     perror("Could not connect socket.\n");
  66.     exit(1);
  67.   }
  68.  
  69.   // Send some data.
  70.   char* message = "Hello from the Client";
  71.   int no_bytes_sent = send(
  72.     socket_descriptor,
  73.     message,
  74.     (size_t) strlen(message)+1, // Plus one to include terminaing zero.
  75.     0);                         // We don't use the flags.
  76.  
  77.   // Check that we sent what we expeected to send.
  78.   if (no_bytes_sent != strlen(message)+1) {
  79.     perror("ERROR: Failed to send data\n");
  80.     exit(1);
  81.   }
  82.  
  83.   // Read the server's result.
  84.   char incoming_data[DATA_BUFFER_SIZE];
  85.   int no_incoming_bytes = recv(
  86.     socket_descriptor,
  87.     &incoming_data,
  88.     DATA_BUFFER_SIZE,
  89.     0);                        // Again, we don't use the flags.
  90.  
  91.   // Check we received >= 0 bytes.
  92.   if (no_incoming_bytes < 0) {
  93.     perror("Receive error\n");
  94.     exit(1);
  95.   }
  96.  
  97.   printf("Received: '%s'\n", incoming_data);
  98.  
  99.   // close the Socket.
  100.   close(socket_descriptor);
  101.  
  102.   // Did we forget to free anything?
  103.   //  - I'll leave that as an exercise ;)
  104.  
  105.   return 0;
  106. }
Add Comment
Please, Sign In to add comment