Advertisement
Guest User

Untitled

a guest
Oct 21st, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.17 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <sys/socket.h>
  3. #include <stdio.h>
  4. #include <netinet/in.h>
  5. #include <netinet/ip.h>
  6. #include <string.h>
  7. #include <unistd.h>
  8. #include <arpa/inet.h>
  9.  
  10. int main(void) {
  11.     // The socket we will use.
  12.     int used_socket;
  13.     // Here we require just one sockaddr_in, for the server.
  14.     struct sockaddr_in server;
  15.  
  16.     // Attempt to create the socket, handle failure.
  17.     used_socket = socket(AF_INET, SOCK_STREAM, 0);
  18.     if (used_socket < 0) {
  19.         printf("Could not create the socket.\n");
  20.         return 1;
  21.     }
  22.  
  23.     // Clear the server sockaddr_in data and set some values.
  24.     memset(&server, 0, sizeof(server));
  25.     server.sin_port = htons(8080);
  26.     server.sin_family = AF_INET;
  27.     server.sin_addr.s_addr = inet_addr("127.0.0.1");
  28.  
  29.     // Attempt to connect to the server, handle failure.
  30.     if (connect(used_socket, (struct sockaddr *) &server, sizeof(server)) < 0) {
  31.         printf("Error connecting to the server.\n");
  32.         return 2;
  33.     }
  34.  
  35.     printf("Client started up successfully.\n");
  36.     // Data related to the problem to be solved.
  37.     uint16_t tosend;
  38.     uint16_t toreceive;
  39.  
  40.     // Note that the sent and received information has to be converted.
  41.     // Conversion between network byte to host byte.
  42.     // https://linux.die.net/man/3/ntohs
  43.     // ntohs = network to host (short / uint16_t)
  44.     // htons = host to network (short / uint16_t)
  45.  
  46.     tosend = htons( 45 );
  47.  
  48.     // Send the data.
  49.     send(used_socket, &tosend, sizeof(tosend), 0);
  50.     // Receive the response.
  51.     recv(used_socket, &toreceive, sizeof(toreceive), 0);
  52.  
  53.     toreceive = ntohs( toreceive );
  54.     printf("Received response: %d.\n", toreceive);
  55.  
  56.     // Close the connection to the server.
  57.     close(used_socket);
  58.  
  59.     return 0;
  60. }
  61. #include <sys/types.h>
  62. #include <sys/socket.h>
  63. #include <stdio.h>
  64. #include <netinet/in.h>
  65. #include <netinet/ip.h>
  66. #include <string.h>
  67. #include <unistd.h>
  68.  
  69. int main(void) {
  70.     // The socket we will use.
  71.     int used_socket;
  72.     // Here we need two sockaddr_in structures, for client as well.
  73.     struct sockaddr_in server, client;
  74.  
  75.     // Create the socket, handle case when it can't be created.
  76.     used_socket = socket(AF_INET, SOCK_STREAM, 0);
  77.     if (used_socket < 0) {
  78.         printf("Error creating the socket.\n");
  79.         return 1;
  80.     }
  81.  
  82.     // Empty the sockaddr_in and set some values.
  83.     memset(&server, 0, sizeof(server));
  84.     server.sin_port = htons(8080);
  85.     server.sin_family = AF_INET;
  86.     server.sin_addr.s_addr = INADDR_ANY;
  87.  
  88.     // Attempt to bind the socket to the used sockaddr_in, handle failure.
  89.     if (bind(used_socket, (struct sockaddr *) &server, sizeof(server)) < 0) {
  90.         printf("Bind error.\n");
  91.         return 1;
  92.     }
  93.  
  94.     // Start listening, store client size and clear the client sockaddr_in information.
  95.     listen(used_socket, 5);
  96.     int client_size = sizeof(client);
  97.     memset(&client, 0, sizeof(client));
  98.  
  99.     printf("Server started up successfully.\n");
  100.     // Keep running.
  101.     while (1) {
  102.         // Accept a connection.
  103.         int ret_addr = accept(used_socket, (struct sockaddr *) &client, &client_size);
  104.         printf("Accepted a new connection.\n");
  105.  
  106.         // Attempt to fork just after accept, so multiple clients can be served concurrently.
  107.         if ( fork() == 0 ) {
  108.             // Information related to the problem to be solved.
  109.             uint16_t tosend;
  110.             uint16_t toreceive;
  111.  
  112.             // Note that the sent and received information has to be converted.
  113.             // Conversion between network byte to host byte.
  114.             // https://linux.die.net/man/3/ntohs
  115.             // ntohs = network to host (short / uint16_t)
  116.             // htons = host to network (short / uint16_t)
  117.  
  118.             // Receive the data.
  119.             recv(ret_addr, &toreceive, sizeof(toreceive), MSG_WAITALL);
  120.  
  121.             toreceive = ntohs( toreceive );
  122.             tosend = htons( toreceive / 5 );
  123.  
  124.             // Send back the response.
  125.             send(ret_addr, &tosend, sizeof(tosend), 0);
  126.  
  127.             // Close the connection to the client.
  128.             close(ret_addr);
  129.         }
  130.     }
  131.  
  132.     return 0;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement