From 14bbb45d375b611d19494874996e94aad64cf912 Mon Sep 17 00:00:00 2001 From: guowenxue <guowenxue@gmail.com> Date: Mon, 06 Nov 2023 17:15:05 +0800 Subject: [PATCH] add socket argments sample --- /dev/null | 139 ----------------- ch2_socket/sockargs_server.c | 152 +++++++++++++++++++ ch2_socket/sockargs_client.c | 131 ++++++++++++++++ 3 files changed, 283 insertions(+), 139 deletions(-) diff --git a/ch2_socket/sockargs_client.c b/ch2_socket/sockargs_client.c new file mode 100644 index 0000000..e3ffe64 --- /dev/null +++ b/ch2_socket/sockargs_client.c @@ -0,0 +1,131 @@ +/********************************************************************************* + * Copyright: (C) 2018 LingYun IoT Studio + * All rights reserved. + * + * Filename: socktcp_client.c + * Description: This file is TCP socket client example source code. + * + * Version: 1.0.0(10/23/2018) + * Author: Guo Wenxue <guowenxue@gmail.com> + * ChangeLog: 1, Release initial version on "2018-10-23 01:38:08 PM" + * + ********************************************************************************/ +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <string.h> +#include <unistd.h> +#include <math.h> +#include <sys/types.h> +#include <sys/socket.h> +#include <netinet/in.h> +#include <arpa/inet.h> +#include <getopt.h> + +void print_usage(char *progname) +{ + printf("%s usage: \n", progname); + printf("-i(--ipaddr): sepcify server IP address\n"); + printf("-p(--port): sepcify server port.\n"); + printf("-h(--Help): print this help information.\n"); + + return ; +} + +int main(int argc, char **argv) +{ + int conn_fd = -1; + int rv = -1; + char buf[1024]; + char *servip = "127.0.0.1"; + int port = 8889; + struct sockaddr_in serv_addr; + int ch; + struct option opts[] = { + {"ipaddr", required_argument, NULL, 'i'}, + {"port", required_argument, NULL, 'p'}, + {"help", no_argument, NULL, 'h'}, + {NULL, 0, NULL, 0} + }; + + while( (ch=getopt_long(argc, argv, "i:p:h", opts, NULL)) != -1 ) + { + switch(ch) + { + case 'i': + servip=optarg; + break; + + case 'p': + port=atoi(optarg); + break; + + case 'h': + print_usage(argv[0]); + return 0; + } + + } + + if( !servip || !port ) + { + print_usage(argv[0]); + return 0; + } + + /* create TCP socket */ + conn_fd = socket(AF_INET, SOCK_STREAM, 0); + if(conn_fd < 0) + { + printf("create socket failure: %s\n", strerror(errno)); + return -1; + } + + /* set socket server information */ + memset(&serv_addr, 0, sizeof(serv_addr)); + serv_addr.sin_family = AF_INET; + serv_addr.sin_port = htons(port); + inet_aton( servip, &serv_addr.sin_addr ); + + /* connect to socket server */ + if( connect(conn_fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) + { + printf("connect to server [%s:%d] failure: %s\n", servip, port, strerror(errno)); + return 0; + } + + while(1) + { + /* send message to socket server */ + strncpy(buf, "Hello, LingYun IoT socket server!", sizeof(buf)); + if( write(conn_fd, buf, strlen(buf)) < 0 ) + { + printf("Write data to server [%s:%d] failure: %s\n", servip, port, strerror(errno)); + goto cleanup; + } + printf("Send message to socket server okay\n"); + + /* receive message from socket server */ + memset(buf, 0, sizeof(buf)); + printf("Wating reply from socket server...\n"); + rv = read(conn_fd, buf, sizeof(buf)); + if(rv < 0) + { + printf("Read data from server failure: %s\n", strerror(errno)); + goto cleanup; + } + else if( 0 == rv ) + { + printf("Client connect to server get disconnected\n"); + goto cleanup; + } + printf("Receive [%d] bytes data from server: %s", rv, buf); + printf("\n"); + + sleep(3); + } + +cleanup: + close(conn_fd); + return 0; +} diff --git a/ch2_socket/sockargs_server.c b/ch2_socket/sockargs_server.c new file mode 100644 index 0000000..be9bf37 --- /dev/null +++ b/ch2_socket/sockargs_server.c @@ -0,0 +1,152 @@ +/********************************************************************************* + * Copyright: (C) 2018 LingYun IoT Studio + * All rights reserved. + * + * Filename: socket_server.c + * Description: This file is TCP socket server example source code. + * + * Version: 1.0.0(10/23/2018) + * Author: Guo Wenxue <guowenxue@gmail.com> + * ChangeLog: 1, Release initial version on "2017-10-23 01:41:05 PM" + * + ********************************************************************************/ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <errno.h> +#include <unistd.h> +#include <sys/types.h> +#include <sys/socket.h> +#include <netinet/in.h> +#include <arpa/inet.h> +#include <getopt.h> + +#define BACKLOG 13 + +void print_usage(char *progname) +{ + printf("%s usage: \n", progname); + printf("-p(--port): sepcify server listen port.\n"); + printf("-h(--Help): print this help information.\n"); + + return ; +} + +int main(int argc, char **argv) +{ + int rv = -1; + int listen_fd, client_fd = -1; + struct sockaddr_in serv_addr; + struct sockaddr_in cli_addr; + int port = 8889; + socklen_t addr_len = sizeof(cli_addr); + char buf[1024]; + int reuse = 1; + + int ch; + struct option opts[] = { + {"port", required_argument, NULL, 'p'}, + {"help", no_argument, NULL, 'h'}, + {NULL, 0, NULL, 0} + }; + + while( (ch=getopt_long(argc, argv, "p:h", opts, NULL)) != -1 ) + { + switch(ch) + { + case 'p': + port=atoi(optarg); + break; + + case 'h': + print_usage(argv[0]); + return 0; + } + + } + + if( !port ) + { + print_usage(argv[0]); + return 0; + } + + + + /* create TCP socket */ + listen_fd = socket(AF_INET, SOCK_STREAM, 0); + if(listen_fd < 0 ) + { + printf("create socket failure: %s\n", strerror(errno)); + return -1; + } + printf("socket create fd[%d]\n", listen_fd); + + /* Fix port can not be used when stop server program and run it again immediately. */ + setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)); + + /* bind socket server information */ + memset(&serv_addr, 0, sizeof(serv_addr)); + serv_addr.sin_family = AF_INET; + serv_addr.sin_port = htons(port); + serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); + if( bind(listen_fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0 ) + { + printf("create socket failure: %s\n", strerror(errno)); + return -2; + } + printf("socket[%d] bind on port[%d] for all IP address ok\n", listen_fd, port); + + /* start listen socket */ + listen(listen_fd, BACKLOG); + + while(1) + { + /* accept new incoming socket client */ + printf("\nStart waiting and accept new client connect...\n"); + client_fd = accept(listen_fd, (struct sockaddr*)&cli_addr, &addr_len); + if(client_fd < 0) + { + printf("accept new socket failure: %s\n", strerror(errno)); + return -2; + } + printf("Accept new client[%s:%d] with fd [%d]\n", inet_ntoa(cli_addr.sin_addr), ntohs(cli_addr.sin_port), client_fd); + + while(1) + { + /* receive message from socket client */ + memset(buf, 0, sizeof(buf)); + if( (rv=read(client_fd, buf, sizeof(buf))) < 0) + { + printf("Read data from client socket[%d] failure: %s\n", client_fd, strerror(errno)); + close(client_fd); + break; + } + else if( rv == 0 ) + { + printf("client socket[%d] disconnected\n", client_fd); + close(client_fd); + break; + } + + printf("Read [%d] bytes data from socket client[%d]: %s\n", rv, client_fd, buf); + + /* send message to socket client */ + printf("Reply back to socket client...\n"); + snprintf(buf, sizeof(buf), "Hello, socket client [%s:%d]\n", inet_ntoa(cli_addr.sin_addr), ntohs(cli_addr.sin_port)); + if( write(client_fd, buf, strlen(buf)) < 0 ) + { + printf("Reply back to socket client[%d] failure: %s\n", client_fd, strerror(errno)); + close(client_fd); + } + printf("\n"); + } + + close(client_fd); + } + + close(listen_fd); + return 0; +} + diff --git a/ch2_socket_sample/socket_client.c b/ch2_socket_sample/socket_client.c deleted file mode 100644 index 642de2d..0000000 --- a/ch2_socket_sample/socket_client.c +++ /dev/null @@ -1,119 +0,0 @@ -#include <stdio.h> -#include <errno.h> -#include <string.h> -#include <unistd.h> -#include <sys/types.h> -#include <sys/socket.h> -#include <arpa/inet.h> -#include <stdlib.h> -#include <getopt.h> - -#define MSG_STR "Hello LingYun IoT Studio\n" - -void print_usage(char *progname) -{ - printf("%s usage: \n", progname); - printf("-i(--ipaddr): sepcify server IP address\n"); - printf("-p(--port): sepcify server port.\n"); - printf("-h(--Help): print this help information.\n"); - - return ; -} - -int main(int argc, char **argv) -{ - int sockfd = -1; - int rv = -1; - struct sockaddr_in servaddr; - char *servip = NULL; - int port = 0; - char buf[1024]; - int ch; - struct option opts[] = { - {"ipaddr", required_argument, NULL, 'i'}, - {"port", required_argument, NULL, 'p'}, - {"help", no_argument, NULL, 'h'}, - {NULL, 0, NULL, 0} - }; - - while( (ch=getopt_long(argc, argv, "i:p:h", opts, NULL)) != -1 ) - { - switch(ch) - { - case 'i': - servip=optarg; - break; - - case 'p': - port=atoi(optarg); - break; - - case 'h': - print_usage(argv[0]); - return 0; - } - - } - - if( !servip || !port ) - { - print_usage(argv[0]); - return 0; - } - - - sockfd=socket(AF_INET, SOCK_STREAM, 0); - if(sockfd < 0) - { - printf("Create socket failure: %s\n", strerror(errno)); - return -1; - } - printf("Create socket[%d] successfully!\n", sockfd); - - memset(&servaddr, 0, sizeof(servaddr)); - servaddr.sin_family=AF_INET; - servaddr.sin_port = htons(port); - inet_aton(servip, &servaddr.sin_addr); - rv=connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)); - if(rv < 0) - { - printf("Connect to server[%s:%d] failure: %s\n", - servip, port, strerror(errno)); - return -2; - } - - printf("Connect to server[%s:%d] successfully!\n", servip, port); - - while(1) - { - rv=write(sockfd, MSG_STR, strlen(MSG_STR)); - if(rv < 0) - { - printf("Write to server by sockfd[%d] failure: %s\n", - sockfd, strerror(errno)); - break; - } - - memset(buf, 0, sizeof(buf)); - rv=read(sockfd, buf, sizeof(buf)); - if( rv < 0) - { - printf("Read data from server by sockfd[%d] failure: %s\n", - sockfd, strerror(errno)); - break; - } - else if( rv == 0) - { - printf("Socket[%d] get disconnected\n", sockfd); - break; - } - else if( rv > 0 ) - { - printf("Read %d bytes data from Server: %s\n", - rv, buf); - } - } - - - close(sockfd); -} diff --git a/ch2_socket_sample/socket_server.c b/ch2_socket_sample/socket_server.c deleted file mode 100644 index 4b373a4..0000000 --- a/ch2_socket_sample/socket_server.c +++ /dev/null @@ -1,139 +0,0 @@ -#include <stdio.h> -#include <errno.h> -#include <string.h> -#include <unistd.h> -#include <sys/types.h> -#include <sys/socket.h> -#include <arpa/inet.h> -#include <stdlib.h> -#include <getopt.h> - -#define MSG_STR "Hello LingYun IoT Studio Client\n" - -void print_usage(char *progname) -{ - printf("%s usage: \n", progname); - printf("-p(--port): sepcify server listen port.\n"); - printf("-h(--Help): print this help information.\n"); - - return ; -} - -int main(int argc, char **argv) -{ - int sockfd = -1; - int rv = -1; - struct sockaddr_in servaddr; - struct sockaddr_in cliaddr; - socklen_t len; - int port = 0; - char buf[1024]; - int clifd; - int ch; - int on = 1; - - struct option opts[] = { - {"port", required_argument, NULL, 'p'}, - {"help", no_argument, NULL, 'h'}, - {NULL, 0, NULL, 0} - }; - - while( (ch=getopt_long(argc, argv, "p:h", opts, NULL)) != -1 ) - { - switch(ch) - { - case 'p': - port=atoi(optarg); - break; - - case 'h': - print_usage(argv[0]); - return 0; - } - - } - - if( !port ) - { - print_usage(argv[0]); - return 0; - } - - sockfd=socket(AF_INET, SOCK_STREAM, 0); - if(sockfd < 0) - { - printf("Create socket failure: %s\n", strerror(errno)); - return -1; - } - printf("Create socket[%d] successfully!\n", sockfd); - - setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); - - 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); - - rv=bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)); - if(rv < 0) - { - printf("Socket[%d] bind on port[%d] failure: %s\n", - sockfd, port, strerror(errno)); - return -2; - } - - listen(sockfd, 13); - printf("Start to listen on port [%d]\n", port); - - while(1) - { - printf("Start accept new client incoming...\n"); - - clifd=accept(sockfd, (struct sockaddr *)&cliaddr, &len); - if(clifd < 0) - { - printf("Accept new client failure: %s\n", strerror(errno)); - continue; - } - - printf("Accept new client[%s:%d] successfully\n", - inet_ntoa(cliaddr.sin_addr), ntohs(cliaddr.sin_port)); - - 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); - continue; - } - else if( rv == 0) - { - printf("Socket[%d] get disconnected\n", clifd); - close(clifd); - continue; - } - else if( rv > 0 ) - { - 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", - sockfd, strerror(errno)); - close(clifd); - continue; - } - - printf("Close client socket[%d]\n", clifd); - close(clifd); - } - - - close(sockfd); -} -- Gitblit v1.9.1