/********************************************************************************* * Copyright: (C) 2025 LingYun IoT System Studio * All rights reserved. * * Filename: socketd.c * Description: This file is TCP socket server example program. * * Version: 1.0.0(10/20/2025) * Author: Guo Wenxue * ChangeLog: 1, Release initial version on "10/20/2025 09:48:51 PM" * ********************************************************************************/ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define BACKLOG 13 int g_stop = 0; /* program stop or not */ void *workbody(void *ctx); void sig_handler(int signum) { switch(signum) { case SIGTERM: printf("SIGTERM signal detected(kill/killall)\n"); break; case SIGSEGV: printf("SIGSEGV signal detected(segmentation fault)\n"); break; case SIGPIPE: printf("SIGPIPE signal detected(socket error)\n"); break; case SIGINT: printf("SIGINT signal detected(Ctrl+C)\n"); break; default: break; } g_stop = 1; } void print_usage(char *progname) { printf("%s usage: \n", progname); printf(" -p(--port): sepcify 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; int port = 10086; struct sockaddr_in serv_addr; struct sockaddr_in cli_addr; socklen_t addr_len = sizeof(cli_addr); int reuse = 1; pthread_t tid; int debug = 0; struct rlimit limit; pid_t pid; int ch; struct option opts[] = { {"debug", no_argument, NULL, 'd'}, {"port", required_argument, NULL, 'p'}, {"help", no_argument, NULL, 'h'}, {NULL, 0, NULL, 0} }; /* parser command line input arguments */ while( (ch=getopt_long(argc, argv, "dp:h", opts, NULL)) != -1 ) { switch(ch) { case 'd': debug = 1; break; case 'p': port=atoi(optarg); break; case 'h': print_usage(argv[0]); return 0; } } /* set Max. fd limit */ getrlimit(RLIMIT_NOFILE, &limit); limit.rlim_cur = limit.rlim_max; setrlimit(RLIMIT_NOFILE, &limit); if( !debug ) { daemon(0, 0); } /* install signal */ //signal(SIGTERM, sig_handler); /* capture kill/killall signal */ //signal(SIGINT, sig_handler); /* capture Ctrl+C signal */ signal(SIGSEGV, sig_handler); /* capture segmentation fault signal */ signal(SIGPIPE, sig_handler); /* capture socket error signal */ /* 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; } /* resolve the "Address already in use" error when using bind() */ 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( !g_stop ) { /* 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); /* create new thread to handle the client */ pthread_create(&tid, NULL, workbody, (void *)(intptr_t)client_fd); } close(listen_fd); return 0; } void *workbody(void *ctx) { int clifd; int rv; char buf[1024]; int i; if( !ctx ) { printf("Invalid input arguments in %s()\n", __FUNCTION__); pthread_exit(NULL); } clifd = (int)(intptr_t)ctx; pthread_detach(pthread_self()); printf("New thread start to commuicate with socket client...\n"); while(1) { /* receive data from socket client */ memset(buf, 0, sizeof(buf)); rv=read(clifd, buf, sizeof(buf)); if( rv < 0) { 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 and thread will exit.\n", clifd); close(clifd); pthread_exit(NULL); } else if( rv > 0 ) { printf("Read %d bytes data from Server: %s\n", rv, buf); } /* convert letter from lowercase to uppercase */ for(i=0; i