multithread server/client implementation in C

前端 未结 2 1421
广开言路
广开言路 2020-12-13 16:34

I have just started learning basic networking concepts.I am trying to implement a multithread server-client prog in C.but the problem is instead of running multiple windows/

相关标签:
2条回答
  • 2020-12-13 17:19

    Firstly, if you fork(), you will be creating additional processes, not additional threads. To create additional threads, you want to use pthread_create.

    Secondly, as you are a student, the canonical answer here is 'read Stephens'. Not only is this an invaluable tool even for those of us experienced in writing socket I/O routines, but also it contains examples of non-threaded non-forking async I/O, and various ways to add threads and forking to them. I believe the one you want is: http://www.amazon.com/Programming-Environment-Addison-Wesley-Professional-Computing/dp/0321637739 (chapter 14 if memory serves). This should be in your college library.

    0 讨论(0)
  • 2020-12-13 17:21

    You can create multiple clients using thread. Create a separate thread for each client and then from thread handler connect to the server. I am not sure if it is a good way or not.

    Code:

    #include <stdio.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    #define MAX_SIZE 50
    #define NUM_CLIENT 5
    void *connection_handler(void *socket_desc);
    int main()
    {
        int socket_desc , new_socket , c , *new_sock, i;
        pthread_t sniffer_thread;
        for (i=1; i<=NUM_CLIENT; i++) {
            if( pthread_create( &sniffer_thread , NULL ,  connection_handler , (void*) i) < 0)
            {
                perror("could not create thread");
                return 1;
            }
            sleep(3);
        }
        pthread_exit(NULL);
        return 0;
    }
    
    void *connection_handler(void *threadid)
    {
        int threadnum = (int)threadid;
        int sock_desc;
        struct sockaddr_in serv_addr;
        char sbuff[MAX_SIZE],rbuff[MAX_SIZE];
    
        if((sock_desc = socket(AF_INET, SOCK_STREAM, 0)) < 0)
            printf("Failed creating socket\n");
    
        bzero((char *) &serv_addr, sizeof (serv_addr));
    
        serv_addr.sin_family = AF_INET;
        serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
        serv_addr.sin_port = htons(8888);
    
        if (connect(sock_desc, (struct sockaddr *) &serv_addr, sizeof (serv_addr)) < 0) {
            printf("Failed to connect to server\n");
        }
    
        printf("Connected successfully client:%d\n", threadnum);
        while(1)
        {
            printf("For thread : %d\n", threadnum);
            fgets(sbuff, MAX_SIZE , stdin);
            send(sock_desc,sbuff,strlen(sbuff),0);
    
            if(recv(sock_desc,rbuff,MAX_SIZE,0)==0)
                printf("Error");
            else
               fputs(rbuff,stdout);
    
            bzero(rbuff,MAX_SIZE);
            sleep(2);
        }
        close(sock_desc);
        return 0;
    }
    

    For understanding purpose, i used sleep.

    REF:

    http://www.amazon.com/UNIX-Network-Programming-Richard-Stevens/dp/0139498761

    http://beej.us/guide/bgnet/

    https://computing.llnl.gov/tutorials/pthreads/

    0 讨论(0)
提交回复
热议问题