SHARE
TWEET

Untitled

Sbovvo Feb 19th, 2019 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <sys/socket.h>
  6. #include <pthread.h>
  7. #include <arpa/inet.h>
  8.  
  9. #define TEXT_SIZE 1000
  10.  
  11. typedef struct{
  12.     unsigned int lunghezza;
  13.     char testo[TEXT_SIZE];
  14. }info;
  15.  
  16. void *connection_handler(void *socket_desc){
  17.     int socket = *(int*)socket_desc;
  18.     int ripeti = 1;
  19.     info message;
  20.    
  21.     while(ripeti){
  22.         read(socket, &message, sizeof(info));
  23.         if(!strncmp(message.testo,"end",3)){
  24.             ripeti=0;
  25.             strcpy(message.testo,"by by");
  26.         }else{
  27.             message.lunghezza=strlen(message.testo);
  28.             strcpy(message.testo,"La lunghezza che ho calcolato e' ");
  29.         }
  30.         write(socket, &message, sizeof(info));
  31.     }
  32.     close(socket);
  33.     free(socket_desc);
  34.     pthread_exit(NULL);
  35. }
  36.  
  37. int main(void){
  38.     int server_sockfd, client_sockfd;
  39.     int server_len, client_len;
  40.     struct sockaddr_in server_address, client_address;
  41.     int *new_sock;
  42.     int res;
  43.     pthread_t tid;
  44.    
  45.     server_sockfd = socket(AF_INET , SOCK_STREAM , 0);
  46.     if (server_sockfd == -1) {
  47.         perror("ERRORE ! Socket non creata\n");
  48.         exit(EXIT_FAILURE);
  49.     }
  50.     printf("Socket creato\n");
  51.      
  52.     server_address.sin_family=AF_INET;
  53.     server_address.sin_addr.s_addr=inet_addr("127.0.0.1");
  54.     server_address.sin_port=htons(9734);
  55.     server_len=sizeof(server_address);
  56.    
  57.     if( bind(server_sockfd,(struct sockaddr *)&server_address, server_len) < 0){
  58.         perror("ERRORE ! Bind non eseguita\n");
  59.     exit(EXIT_FAILURE);
  60.     }
  61.     printf("Bind eseguita\n");
  62.    
  63.     listen(server_sockfd, 5);
  64.     printf("Attendo connessioni...\n");
  65.    
  66.     while(1){
  67.         client_len=sizeof(client_address);
  68.         client_sockfd=accept(server_sockfd, (struct sockaddr *)&client_address, &client_len);
  69.         if(client_sockfd < 0){
  70.             perror("ERRORE ! Socket non creato\n");
  71.             exit(EXIT_FAILURE);
  72.         }
  73.         printf("Connessione accettata\n");
  74.        
  75.         new_sock= (int *)malloc(sizeof(int));
  76.         *new_sock=client_sockfd;
  77.        
  78.         res=pthread_create(&tid, NULL, connection_handler,(void *)new_sock);
  79.         if(res!=0){
  80.             perror("ERRORE ! Creazione del thread fallita\n");
  81.             exit(EXIT_FAILURE);
  82.         }
  83.         pthread_detach(tid);
  84.     }
  85.   int server_sockfd, client_sockfd;
  86.   int server_len, client_len;
  87.   struct sockaddr_in server_address, client_address;
  88.   int *new_sock;
  89.   int res;
  90.   pthread_t tid;
  91.  
  92.   server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
  93.   if(server_sockfd == -1){
  94.       perror("ERRORE ! Socket non creata\n");
  95.       exit(EXIT_FAILURE);
  96.  }
  97.  printf("Socket creata\n");
  98.  
  99. server_address.sin_family=AF_INET;
  100. server_address.sin_addr.s_addr=inet_addr("127.0.0.1");
  101. server_address.sin_port=htons(9734);
  102. server_len=sizeof(server_address);
  103. }
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