APUE Learning Example Source Code
Guo Wenxue
2018-11-15 8d13b7c3d12e05ce4620e43901a554d80d5c7b23
update multple thread socket server sample code
1 files modified
13 ■■■■ changed files
ch4_thread/socket_server_thread.c 13 ●●●● patch | view | raw | blame | history
ch4_thread/socket_server_thread.c
@@ -9,13 +9,11 @@
#include <getopt.h>
#include <pthread.h>
#define MSG_STR "Hello LingYun IoT Studio Client\n"
typedef void *(THREAD_BODY) (void *thread_arg);
void *thread_worker(void *ctx);
int thread_start(pthread_t * thread_id, THREAD_BODY * thread_workbody, void *thread_arg);
void print_usage(char *progname)
{
@@ -106,7 +104,8 @@
        printf("Accept new client[%s:%d] successfully\n", inet_ntoa(cliaddr.sin_addr), ntohs(cliaddr.sin_port));
        thread_start(&tid, thread_worker, &clifd);
        /* ???? Can not pass &clifd to child process, U can think about the reason  ???? */
        thread_start(&tid, thread_worker, (void *)clifd);
    }
@@ -166,7 +165,7 @@
        pthread_exit(NULL);
    }
    clifd = *(int *)ctx;
    clifd = (int)ctx;
    
    printf("Child thread start to commuicate with socket client...\n");
    
@@ -176,13 +175,13 @@
               rv=read(clifd, buf, sizeof(buf));
               if( rv < 0)
               {
                   printf("Read data from client sockfd[%d] failure: %s\n", clifd, strerror(errno));
                   printf("Read data from client sockfd[%d] failure: %s and thread will exit\n", clifd, strerror(errno));
                   close(clifd);
                   pthread_exit(NULL);
               }
        else if( rv == 0)
               { 
            printf("Socket[%d] get disconnected\n", clifd);
            printf("Socket[%d] get disconnected and thread will exit.\n", clifd);
                   close(clifd);
                   pthread_exit(NULL);
               } 
@@ -194,7 +193,7 @@
        rv=write(clifd, buf, rv);
               if(rv < 0)
               {
                   printf("Write to client by sockfd[%d] failure: %s\n", clifd, strerror(errno));
                   printf("Write to client by sockfd[%d] failure: %s and thread will exit\n", clifd, strerror(errno));
                   close(clifd);
                   pthread_exit(NULL);
               }