SHARE
TWEET

Untitled

a guest Nov 19th, 2019 76 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <errno.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include <arpa/inet.h>  // htons() and inet_addr()
  7. #include <netinet/in.h> // struct sockaddr_in
  8. #include <sys/socket.h>
  9.  
  10. #include "common.h"
  11.  
  12. int main(int argc, char* argv[]) {
  13.     int ret,bytes_sent,recv_bytes;
  14.  
  15.     // variables for handling a socket
  16.     int socket_desc;
  17.     struct sockaddr_in server_addr = {0}; // some fields are required to be filled with 0
  18.  
  19.     /**
  20.      *  TODO: create a socket for contacting the server
  21.      *
  22.      * Suggestions:
  23.      * - protocollo AF_INET
  24.      * - tipo SOCK_STREAM
  25.      */
  26.  
  27.     socket_desc = socket(AF_INET, SOCK_STREAM, 0);
  28.  
  29.     if (socket_desc == -1) {
  30.         perror("Errore nella creazione della socket");
  31.         exit(EXIT_FAILURE);
  32.     }
  33.  
  34.     if (DEBUG) fprintf(stderr, "Socket created...\n");
  35.  
  36.     /**
  37.      *  TODO: set up parameters for the connection and initiate a connection to the server
  38.      *
  39.      * Suggestions:
  40.      * - set the 3 fields of server:
  41.      * - - server_addr.sin_addr.s_addr: we must specify the server address
  42.      * - - server_addr.sin_family,
  43.      * - - server_addr.sin_port (using htons() method)
  44.      * - initiate a connection to the server
  45.      * - - attention to the bind method:
  46.      * - - it requires as second field struct sockaddr* addr, but our address is a struct sockaddr_in, hence we must cast it (struct sockaddr*) &server_addr
  47.      */
  48.  
  49.     server_addr.sin_addr.s_addr = SERVER_ADDRESS;
  50.     server_addr.sin_family = AF_INET;
  51.     server_addr.sin_port = htons(SERVER_PORT);
  52.  
  53.     struct sockaddr *indirizzo_server = (struct sockaddr*) &server_addr;
  54.  
  55.     int connessione = connect(socket_desc, indirizzo_server, sizeof(*indirizzo_server));
  56.  
  57.     if (connessione == -1) {
  58.         perror("Errore nella connect del client");
  59.         exit(EXIT_FAILURE);
  60.     }
  61.  
  62.     if (DEBUG) fprintf(stderr, "Connection established!\n");
  63.  
  64.     char buf[1024];
  65.     size_t buf_len = sizeof(buf);
  66.     int msg_len;
  67.     memset(buf,0,buf_len);
  68.  
  69.     /**
  70.      *  TODO: receive and display the welcome message from server
  71.      *
  72.      * Suggestions:
  73.      * - set the 3 fields of server:
  74.      * - - server_addr.sin_addr.s_addr: we must specify the server address
  75.      * - - server_addr.sin_family,
  76.      * - - server_addr.sin_port (using htons() method)
  77.      * - initiate a connection to the server
  78.      * - - attention to the bind method:
  79.      * - - it requires as second field struct sockaddr* addr, but our address is a struct sockaddr_in, hence we must cast it (struct sockaddr*) &server_addr
  80.      */
  81.  
  82.     recv_bytes = 0;
  83.     ret = 0;
  84.     int bytes_to_read = buf_len;
  85.  
  86.     bytes_sent = 0;
  87.     int bytes_to_send;
  88.  
  89.     while(bytes_to_read > 0) {
  90.         ret = recv(socket_desc, buf+ret, buf_len, 0);
  91.        
  92.         if (ret = 0) break;
  93.  
  94.         if (ret == -1) {
  95.             if (errno == EINTR) continue;
  96.             else {
  97.                 perror("Errore in ricezione");
  98.                 exit(EXIT_FAILURE);
  99.             }
  100.         }
  101.  
  102.         recv_bytes += ret;
  103.         bytes_to_read -= ret;
  104.     }
  105.  
  106.     if (DEBUG) fprintf(stderr, "Received message of %d bytes...\n",recv_bytes);
  107.  
  108.     printf("Ho ricevuto questo messaggio: %s \n", buf);
  109.  
  110.     // main loop
  111.     while (1) {
  112.         char* quit_command = SERVER_COMMAND;
  113.         size_t quit_command_len = strlen(quit_command);
  114.  
  115.         printf("Insert your message: ");
  116.  
  117.         /* Read a line from stdin
  118.          *
  119.          * fgets() reads up to sizeof(buf)-1 bytes and on success
  120.          * returns the first argument passed to it. */
  121.         if (fgets(buf, sizeof(buf), stdin) != (char*)buf) {
  122.             fprintf(stderr, "Error while reading from stdin, exiting...\n");
  123.             exit(EXIT_FAILURE);
  124.         }
  125.  
  126.         msg_len = strlen(buf);
  127. //        buf[--msg_len] = '\0'; // remove '\n' from the end of the message
  128.  
  129.         /**
  130.          *  TODO: send message to server
  131.          *
  132.          * Suggestions:
  133.          * - send() with flags = 0 is equivalent to write() to a descriptor
  134.          * - the number of bytes to send is msg_len as we have discarded '\n'
  135.          * - deal with partially sent messages
  136.          * - message size IS NOT buf size
  137.          */
  138.  
  139.         bytes_to_send = msg_len;
  140.         ret = 0;
  141.         bytes_sent = 0;
  142.  
  143.         while(bytes_to_send > 0) {
  144.             ret = send(socket_desc, buf+bytes_sent, msg_len, 0);
  145.        
  146.             if (ret == 0) break;
  147.  
  148.             if (ret == -1) {
  149.                 if (errno == EINTR) continue;
  150.                 else {
  151.                     perror("Errore nella send del client");
  152.                     exit(EXIT_FAILURE);
  153.                 }
  154.             }
  155.  
  156.             bytes_sent += ret;
  157.             bytes_to_send -= ret;
  158.         }
  159.  
  160.         if (DEBUG) fprintf(stderr, "Sent message of %d bytes...\n", bytes_sent);
  161.  
  162.  
  163.         /**
  164.          *  TODO: After a quit command we won't receive any more data from
  165.          *  the server, thus we must exit the main loop.
  166.          *
  167.          * Suggestions:
  168.          * - compare the number of bytes sent with the length of the
  169.          *   quit command that tells the server to close the connection
  170.          *   (see SERVER_COMMAND macro in common.h)
  171.          * - perform a byte-to-byte comparsion when required using
  172.          *   memcmp(const void *ptr1, const void *ptr2, size_t num)
  173.          * - exit from the cycle when there is nothing left to receive
  174.          */
  175.  
  176.         char ginio[6] = SERVER_COMMAND;
  177.  
  178.         if (memcmp(ginio, buf, msg_len)) break;
  179.         /**
  180.          *  TODO: read message from server
  181.          * Suggestions:
  182.          * - recv() with flags = 0 is equivalent to read() from a descriptor
  183.          * - for sockets, we get a 0 return value only when the peer closes
  184.          *   the connection: if there are no bytes to read and we invoke
  185.          *   recv() we will get stuck, because the call is blocking!
  186.          * - deal with partially sent messages (we do not know the message size)
  187.          */
  188.  
  189.         while(bytes_to_read > 0) {
  190.             ret = recv(socket_desc, buf+ret, buf_len, 0);
  191.        
  192.             if (ret = 0) break;
  193.  
  194.             if (ret == -1) {
  195.                 if (errno == EINTR) continue;
  196.                 else {
  197.                     perror("Errore in ricezione");
  198.                     exit(EXIT_FAILURE);
  199.                 }
  200.             }
  201.  
  202.             recv_bytes += ret;
  203.             bytes_to_read -= ret;
  204.         }
  205.  
  206.         if (DEBUG) fprintf(stderr, "Received answer of %d bytes...\n",recv_bytes);
  207.  
  208.         printf("Server response: %s\n", buf);
  209.     }
  210.  
  211.  
  212.     /**
  213.      *  TODO: close socket and release unused resources
  214.      */
  215.  
  216.     if (DEBUG) fprintf(stderr, "Socket closed...\n");
  217.  
  218.     if (DEBUG) fprintf(stderr, "Exiting...\n");
  219.  
  220.     exit(EXIT_SUCCESS);
  221. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top