/*********************************************************************************
|
* 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 <guowenxue@gmail.com>
|
* ChangeLog: 1, Release initial version on "10/20/2025 09:48:51 PM"
|
*
|
********************************************************************************/
|
|
#include <stdio.h>
|
#include <string.h>
|
#include <errno.h>
|
#include <unistd.h>
|
#include <stdlib.h>
|
#include <signal.h>
|
#include <sys/types.h>
|
#include <sys/socket.h>
|
#include <netinet/in.h>
|
#include <arpa/inet.h>
|
#include <getopt.h>
|
#include <ctype.h>
|
#include <sys/time.h>
|
#include <sys/resource.h>
|
#include <sys/types.h>
|
#include <sys/wait.h>
|
|
#define BACKLOG 13
|
|
int g_stop = 0; /* program stop or not */
|
|
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;
|
|
case SIGCHLD:
|
printf("SIGCHLD signal detected(child exit)\n");
|
waitpid(-1, NULL, WNOHANG);
|
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;
|
|
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(SIGSEGV, sig_handler); /* capture segmentation fault signal */
|
signal(SIGPIPE, sig_handler); /* capture socket error signal */
|
signal(SIGINT, sig_handler); /* capture Ctrl+C signal */
|
signal(SIGCHLD, sig_handler); /* capture child exit 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);
|
|
pid = fork();
|
if( pid < 0 ) /* fork() failed */
|
{
|
printf("fork() create child process failure: %s\n", strerror(errno));
|
close(client_fd);
|
continue;
|
}
|
else if( pid > 0 ) /* parent process */
|
{
|
/* close client fd and goes to accept new client again */
|
close(client_fd);
|
continue;
|
}
|
else /* child process */
|
{
|
int i;
|
char buf[1024];
|
|
/* close the listen socket fd, parent will handle it */
|
close(listen_fd);
|
|
printf("Child process start to commuicate with socket client...\n");
|
while(1)
|
{
|
/* receive data from socket client */
|
memset(buf, 0, sizeof(buf));
|
rv=read(client_fd, buf, sizeof(buf));
|
if( rv < 0 )
|
{
|
printf("Read data from client sockfd[%d] failure: %s\n", client_fd, strerror(errno));
|
close(client_fd);
|
exit(0);
|
}
|
else if( rv == 0)
|
{
|
printf("Socket[%d] get disconnected\n", client_fd);
|
close(client_fd);
|
exit(1);
|
}
|
else if( rv > 0 )
|
{
|
printf("Read %d bytes data from client: %s\n", rv, buf);
|
}
|
|
/* convert letter from lowercase to uppercase */
|
for(i=0; i<rv; i++)
|
{
|
buf[i]=toupper(buf[i]);
|
}
|
|
/* write back data to socket client */
|
rv=write(client_fd, buf, rv);
|
if(rv < 0)
|
{
|
printf("Write to client by sockfd[%d] failure: %s\n", client_fd, strerror(errno));
|
close(client_fd);
|
exit(1);
|
}
|
} /* Child process loop */
|
}
|
}
|
|
close(listen_fd);
|
return 0;
|
}
|