daily pastebin goal
19%
SHARE
TWEET

Untitled

a guest Jun 13th, 2018 46 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<stdio.h>
  2. #include<string.h>    //strlen
  3. #include<stdlib.h>    //strlen
  4. #include<sys/socket.h>
  5. #include<arpa/inet.h> //inet_addr
  6. #include<unistd.h>    //write
  7. #include<pthread.h> //for threading , link with lpthread
  8.  
  9. //the thread function
  10. void *connection_handler(void *);
  11.  
  12. int main(int argc , char *argv[])
  13. {
  14.     int socket_desc , client_sock , c , *new_sock;
  15.     struct sockaddr_in server , client;
  16.      
  17.     //Create socket
  18.     socket_desc = socket(AF_INET , SOCK_STREAM , 0);
  19.     if (socket_desc == -1)
  20.     {
  21.         printf("Could not create socket");
  22.     }
  23.     puts("Socket created");
  24.      
  25.     //Prepare the sockaddr_in structure
  26.     server.sin_family = AF_INET;
  27.     server.sin_addr.s_addr = INADDR_ANY;
  28.     server.sin_port = htons( 8888 );
  29.      
  30.     //Bind
  31.     if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
  32.     {
  33.         //print the error message
  34.         perror("bind failed. Error");
  35.         return 1;
  36.     }
  37.     puts("bind done");
  38.      
  39.     //Listen
  40.     listen(socket_desc , 3);
  41.      
  42.     //Accept and incoming connection
  43.     puts("Waiting for incoming connections...");
  44.     c = sizeof(struct sockaddr_in);
  45.      
  46.    
  47.     //Accept and incoming connection
  48.     puts("Waiting for incoming connections...");
  49.     c = sizeof(struct sockaddr_in);
  50.     while( (client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c)) )
  51.     {
  52.     int var;
  53.         puts("Connection accepted");
  54.         FILE *file;
  55.         file = fopen("log.txt","r");
  56.         fscanf(file, "%d,", &var);
  57.         printf("Data read=%i\n",var);
  58.         fclose(file);
  59.                
  60.  
  61.     //pthread_t sniffer_thread;
  62.     pthread_t thread_id [10];
  63.         new_sock = malloc(1);
  64.         *new_sock = client_sock;
  65.         int id2 = pthread_self();
  66.     printf("Thread nummber: %d \n", id2);
  67.         if( pthread_create( &thread_id[var] , NULL ,  connection_handler , (void*) new_sock) < 0)
  68.         {
  69.             perror("could not create thread");
  70.             return 1;
  71.         }
  72.     //var++;
  73.     //printf("VAR MUDOU=%i\n",var);
  74.     //file = fopen("log.txt","w+");
  75.     //fprintf(file, "%d", var);
  76.     //fclose(file);
  77.         int id = pthread_self();
  78.     printf("Thread nummber: %d \n", id);
  79.         //Now join the thread , so that we dont terminate before the thread
  80.         //pthread_join( sniffer_thread , NULL);
  81.         puts("Handler assigned");
  82.     }
  83.      
  84.     if (client_sock < 0)
  85.     {
  86.         perror("accept failed");
  87.         return 1;
  88.     }
  89.      
  90.     return 0;
  91. }
  92.  
  93. /*
  94.  * This will handle connection for each client
  95.  * */
  96. void *connection_handler(void *socket_desc)
  97. {
  98.     int var;
  99.         FILE *file;
  100.         file = fopen("log.txt","r");
  101.         fscanf(file, "%d,", &var);
  102.         printf("Data read=%i\n",var);
  103.         fclose(file);
  104.                
  105.         var++;
  106.        
  107.     file = fopen("log.txt","w+");
  108.     fprintf(file, "%d", var);
  109.     fclose(file);
  110.     //Get the socket descriptor
  111.     int sock = *(int*)socket_desc;
  112.     int read_size;
  113.     char *message , client_message[2000];
  114.      
  115.     //Send some messages to the client
  116.     message = "Greetings! I am your connection handler\n";
  117.     write(sock , message , strlen(message));
  118.      
  119.     message = "Now type something and i shall repeat what you type \n";
  120.     write(sock , message , strlen(message));
  121.      
  122.     //Receive a message from client
  123.     while( (read_size = recv(sock , client_message , 2000 , 0)) > 0 )
  124.     {
  125.         //Send the message back to client
  126.         write(sock , client_message , strlen(client_message));
  127.     }
  128.      
  129.     if(read_size == 0)
  130.     {
  131.         puts("Client disconnected");
  132.         fflush(stdout);
  133.     }
  134.     else if(read_size == -1)
  135.     {
  136.         perror("recv failed");
  137.     }
  138.          
  139.     //Free the socket pointer
  140.     free(socket_desc);
  141.      
  142.     return 0;
  143. }
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