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 2.27 KB | None | 0 0
  1. #define closesocket close
  2. #include <sys/socket.h>
  3. #include <arpa/inet.h>
  4. #include <unistd.h>
  5. #include <sys/socket.h>
  6. #include <arpa/inet.h>
  7. #include <unistd.h>
  8. #include <stdio.h>
  9. #include <stdio.h>
  10. #include <stdlib.h> // for atoi()
  11. #include <string.h>
  12. #define PROTOPORT 27015 // default protocol port number
  13. #define QLEN 6 // size of request queue
  14.  
  15. void errorhandler(char *errorMessage) {
  16. printf ("%s", errorMessage);
  17. }
  18.  
  19. void clearwinsock() {
  20.  
  21. }
  22.  
  23. int main(int argc, char *argv[]) {
  24.     int port;
  25.     if (argc > 1) {
  26.         port = atoi(argv[1]); // if argument specified convert argument tobinary
  27.     }
  28.     else
  29.         port = PROTOPORT; // use default port number
  30.     if (port < 0) {
  31.         printf("bad port number %s \n", argv[1]);
  32.         return 0;
  33.     }
  34.  
  35.  
  36.     // CREAZIONE DELLA SOCKET
  37.     int my_socket;
  38.     my_socket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  39.     if (my_socket < 0) {
  40.     errorhandler("socket creation failed.\n");
  41.     return -1;
  42.     }
  43.     // ASSEGNAZIONE DI UN INDIRIZZO ALLA SOCKET
  44.     struct sockaddr_in sad;
  45.     memset(&sad, 0, sizeof(sad)); // ensures that extra bytes contain 0
  46.     sad.sin_family = AF_INET;
  47.     sad.sin_addr.s_addr = inet_addr("127.0.0.1");
  48.     sad.sin_port = htons(PROTOPORT); /* converts values between the host and
  49.     network byte order. Specifically, htons() converts 16-bit quantities
  50.     from host byte order to network byte order. */
  51.     if (bind(my_socket, (struct sockaddr*) &sad, sizeof(sad)) < 0) {
  52.         errorhandler("bind() failed.\n");
  53.         close(my_socket);
  54.         return -1;
  55.     }
  56.  
  57.  
  58.     // SETTAGGIO DELLA SOCKET ALL'ASCOLTO
  59.     if (listen (my_socket, QLEN) < 0) {
  60.         errorhandler("listen() failed.\n");
  61.         close(my_socket);
  62.         return -1;
  63.     }
  64.     printf("1");
  65.  
  66.     // ACCETTARE UNA NUOVA CONNESSIONE
  67.     struct sockaddr_in cad; // structure for the client address
  68.     int client_socket;
  69.  
  70.     // socket descriptor for the client
  71.     int client_len;
  72.  
  73.     // the size of the client address
  74.     printf("Waiting for a client to connect...");
  75.     while (1) { /* oppure for (;;) */
  76.         client_len = sizeof(cad); // set the size of the client address
  77.         if ((client_socket = accept(my_socket, (struct sockaddr*)&cad, &client_len)) < 0) {
  78.             errorhandler("accept() failed.\n");
  79.  
  80.             // CHIUSURA DELLA CONNESSIONE
  81.             close(my_socket);
  82.             return 0;
  83.         }
  84.  
  85.         printf("Handling client %s\n", inet_ntoa(cad.sin_addr));
  86.     } // end-while
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement