File was renamed from ch3_fork/socket_server.c |
| | |
| | | #include <arpa/inet.h> |
| | | #include <stdlib.h> |
| | | #include <getopt.h> |
| | | |
| | | #define MSG_STR "Hello LingYun IoT Studio Client\n" |
| | | #include <ctype.h> |
| | | |
| | | void print_usage(char *progname) |
| | | { |
| | |
| | | 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) |
| | |
| | | continue; |
| | | } |
| | | |
| | | printf("Accept new client[%s:%d] successfully\n", |
| | | inet_ntoa(cliaddr.sin_addr), ntohs(cliaddr.sin_port)); |
| | | printf("Accept new client[%s:%d] successfully\n", inet_ntoa(cliaddr.sin_addr), ntohs(cliaddr.sin_port)); |
| | | |
| | | pid = fork(); |
| | | if( pid < 0 ) |
| | |
| | | else if ( 0 == pid ) |
| | | { |
| | | char buf[1024]; |
| | | int i; |
| | | |
| | | /* Child process close the listen socket fd */ |
| | | close(sockfd); |
| | | |
| | | printf("Child process start to commuicate with socket client...\n"); |
| | | memset(buf, 0, sizeof(buf)); |
| | | rv=read(clifd, buf, sizeof(buf)); |
| | | if( rv < 0) |
| | | { |
| | | printf("Read data from client sockfd[%d] failure: %s\n", clifd, strerror(errno)); |
| | | close(clifd); |
| | | exit(0); |
| | | } |
| | | else if( rv == 0) |
| | | { |
| | | printf("Socket[%d] get disconnected\n", clifd); |
| | | close(clifd); |
| | | exit(0); |
| | | } |
| | | else if( rv > 0 ) |
| | | |
| | | close(sockfd); /* Child process close the listen socket fd */ |
| | | |
| | | while(1) |
| | | { |
| | | printf("Read %d bytes data from Server: %s\n", rv, buf); |
| | | } |
| | | |
| | | rv=write(clifd, MSG_STR, strlen(MSG_STR)); |
| | | if(rv < 0) |
| | | { |
| | | printf("Write to client by sockfd[%d] failure: %s\n", clifd, strerror(errno)); |
| | | close(clifd); |
| | | exit(0); |
| | | } |
| | | memset(buf, 0, sizeof(buf)); |
| | | rv=read(clifd, buf, sizeof(buf)); |
| | | if( rv < 0 ) |
| | | { |
| | | printf("Read data from client sockfd[%d] failure: %s\n", clifd, strerror(errno)); |
| | | close(clifd); |
| | | exit(0); |
| | | } |
| | | else if( rv == 0) |
| | | { |
| | | printf("Socket[%d] get disconnected\n", clifd); |
| | | close(clifd); |
| | | exit(0); |
| | | } |
| | | else if( rv > 0 ) |
| | | { |
| | | printf("Read %d bytes data from Server: %s\n", rv, buf); |
| | | } |
| | | |
| | | sleep(1); |
| | | /* convert letter from lowercase to uppercase */ |
| | | for(i=0; i<rv; i++) |
| | | { |
| | | buf[i]=toupper(buf[i]); |
| | | } |
| | | |
| | | printf("close client socket[%d] and child process exit\n", clifd); |
| | | close(clifd); |
| | | exit(0); |
| | | } |
| | | rv=write(clifd, buf, rv); |
| | | if(rv < 0) |
| | | { |
| | | printf("Write to client by sockfd[%d] failure: %s\n", clifd, strerror(errno)); |
| | | close(clifd); |
| | | exit(0); |
| | | } |
| | | |
| | | } /* Child process loop */ |
| | | |
| | | } /* Child process start*/ |
| | | } |
| | | |
| | | |