APUE course source code
guowenxue
2 days ago 01c119252bae1e252aa9e6bdba99f1b2ee756624
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*********************************************************************************
 *      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;
}