From 8a383b8715fc07145fdc3f3e3dbe6ecd93f72989 Mon Sep 17 00:00:00 2001 From: guowenxue <guowenxue@gmail.com> Date: Sun, 07 Jun 2020 00:06:57 +0800 Subject: [PATCH] update tlv_server program, add TLV parser support --- ch4_thread/socket_server_thread.c | 25 ++++++++++++++++--------- 1 files changed, 16 insertions(+), 9 deletions(-) diff --git a/ch4_thread/socket_server_thread.c b/ch4_thread/socket_server_thread.c index 7b194af..8e4ecee 100644 --- a/ch4_thread/socket_server_thread.c +++ b/ch4_thread/socket_server_thread.c @@ -8,14 +8,13 @@ #include <stdlib.h> #include <getopt.h> #include <pthread.h> +#include <ctype.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) { @@ -80,8 +79,8 @@ memset(&servaddr, 0, sizeof(servaddr)); servaddr.sin_family=AF_INET; servaddr.sin_port = htons(port); - servaddr.sin_addr.s_addr = htonl(INADDR_ANY); - //inet_aton("192.168.0.16", &servaddr.sin_addr); + servaddr.sin_addr.s_addr = htonl(INADDR_ANY); /* listen all the IP address on this host */ + //inet_aton("192.168.0.16", &servaddr.sin_addr); /* Only listen specify IP address on this host */ rv=bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)); if(rv < 0) @@ -106,7 +105,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); } @@ -159,6 +159,7 @@ int clifd; int rv; char buf[1024]; + int i; if( !ctx ) { @@ -166,7 +167,7 @@ pthread_exit(NULL); } - clifd = *(int *)ctx; + clifd = (int)ctx; printf("Child thread start to commuicate with socket client...\n"); @@ -176,13 +177,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); } @@ -190,11 +191,17 @@ { printf("Read %d bytes data from Server: %s\n", rv, buf); } + + /* convert letter from lowercase to uppercase */ + for(i=0; i<rv; i++) + { + buf[i]=toupper(buf[i]); + } 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); } -- Gitblit v1.9.1