guowenxue
2020-08-21 02f4d9518378031c63df7a36c49d8b2eabdaab90
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
/*********************************************************************************
 *      Copyright:  (C) 2018 LingYun I.o.T Studio  <www.iot-yun.com>
 *                  All rights reserved.
 *
 *       Filename:  socket_client.c
 *    Description:  This is a sample socket client demo program.
 *                  Usage: socket_client -i 192.168.2.18 -p 6666
 *                 
 *        Version:  1.0.0(06/08/2012~)
 *         Author:  Guo Wenxue <guowenxue@gmail.com>
 *      ChangeLog:  1, Release initial version on "06/08/2012 02:50:51 PM"
 *                 
 ********************************************************************************/
 
/* Some Unix Program Standard head file  */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <getopt.h> /* getopt_long */
#include <libgen.h> /* getopt_long */
 
/* Socket Program head file */
#include <sys/types.h>   
#include <sys/socket.h>
#include <netinet/in.h> /* sockaddr_in{} and other Internet define */
 
#include <errno.h>      /* strerror(), perror(), errno head file*/
#include <arpa/inet.h>  /* For inet_pton() */
 
#define MAX_BUF_SIZE                       1024
 
#define LOGON_INFO                         "lingyun:www.iot-yun.com"
 
#define MAJOR            1
#define MINOR            0
#define REVER            0
 
void print_version(char *progname)
{
    printf("%s Version 1.0.0\n", progname);
    printf("Copyright (C) 2012 Guo Wenxue<guowenxue@gmail.com>.\n");
    return;
}
 
void print_usage(char *progname)
{
    print_version(progname); 
    
    printf("Usage: %s [OPTION]...\n", progname);
    printf(" %s is a socket client test program, which used to logon server and send string to server\n", progname);
 
    printf("\nMandatory arguments to long options are mandatory for short options too:\n");
 
    printf(" -i[ipaddr  ]  Socket server IP address\n");
    printf(" -p[port    ]  Socket server port address\n");
    printf(" -h[help    ]  Display this help information\n");
    printf(" -v[version ]  Display the program version\n");
 
    printf("\nExample: %s -i 192.168.2.18 -p 8900\n", progname);
    return ;
}
 
 
/* Argc is the program linux running command arguments count, and argv is 
 * the arguments string value. All of the arguments take as string.
 */
int main(int argc, char **argv)
{
    int                       sockfd, len, rv;
    int                       opt;
    char                     *progname = NULL;
    char                     *server_ip = NULL;
    unsigned short            server_port = 0;
    char                      buf[MAX_BUF_SIZE];
    struct sockaddr_in        servaddr;
 
    struct option             long_options[] = 
    { 
        {"ipaddr", required_argument, NULL, 'i'},
        {"port", required_argument, NULL, 'p'},
        {"version", no_argument, NULL, 'v'},
        {"help", no_argument, NULL, 'h'},
        {NULL, 0, NULL, 0}
    };
 
    progname = basename(argv[0]);
 
    /*  Parser the command line parameters */
    while ((opt = getopt_long(argc, argv, "i:p:vh", long_options, NULL)) != -1)
    {
        switch (opt)
        {
            case 'p':
                server_port = atoi(optarg);
                break;
 
            case 'i':
                server_ip = optarg;
                break;
 
            case 'v':
                print_version(progname); /*  Defined in version.h */
                return EXIT_SUCCESS;
 
            case 'h':  /*  Get help information */
                print_usage(progname);
                return EXIT_SUCCESS;
 
            default:
                break;
        }
    }
 
    if(NULL==server_ip || 0==server_port) 
    {
        print_usage(progname);
        return -1;
    }
 
    /* Open an IPV4(AF_INET) TCP(SOCK_STREAM) Socket File Description, UDP socket should 
     * use SOCK_DGRAM,We can use linux command "man socket" to see this function manual
     */
    if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
        /* strerror() is the 1st way to display the failure reason, argument 
         * errno is a globle variable defined in <errno.h>, we can use linux command 
         * "man strerror" to see this function manual*/
        printf("Use socket() to create a TCP socket failure: %s\n", strerror(errno));
        return -1;
    }
 
    /* Now we set the Server Information, include IPV4 or IPV6, Server Port, Server IP address  */
    memset(&servaddr, 0, sizeof(servaddr));
    servaddr.sin_family = AF_INET;   /* Set it as IPV4 protocal */
    servaddr.sin_port = htons(server_port); /*Server port*/ 
 
    /* argv[1] we take as Server IP address, it's the second arguments in running command  */
    if (inet_pton(AF_INET, server_ip, &servaddr.sin_addr) <= 0) 
    {
        printf("Use inet_pton() to set the Server IP address failure.\n");
        rv = -2;
        goto CleanUp;
    }
 
    /* Now call connect() function to connect to the server, we can use linux command "man connect" 
     * to see this function manual */
    if (connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0)
    {
        printf("Connect to the server [%s:%d] failure: %s\n", server_ip, server_port, strerror(errno));
        rv = -3;
        goto CleanUp;
    }
    printf("Connect to Server [%s:%d] ok\n", server_ip, server_port);
 
#ifdef CONFIG_VERIFY_USER
    if( write(sockfd, LOGON_INFO, strlen(LOGON_INFO)) < 0)
    {
        printf("Write logon information to server failure: %s\n", strerror(errno));
        goto CleanUp;
    }
 
    /* Once we connect to the server successfully, we can receive the data from the socket socekt */
    memset(buf, 0, sizeof(buf));
    if( (len = read(sockfd, buf, MAX_BUF_SIZE)) < 0) 
    {
        printf("socket read data from server[%s] failure:%s\n", server_ip, strerror(errno));
        goto CleanUp;
    }
    else if( 0 == len ) 
    {
        printf("logon to server[%s] failure and socket disconnected\n", server_ip);
        goto CleanUp;
    }
 
    /* socket read data from server */
    if( strncasecmp(buf, "passed", 6) )
    {
        printf("Logon to server[%s] failure: %s\n", server_ip, buf);
        goto CleanUp;
    }
    printf("Logon to server successfully!\n");
#endif
 
    while(1)
    {
        printf("\nplease input string to send to server:\n==> ");
 
        memset(buf, 0, sizeof(buf));
        fgets(buf, sizeof(buf), stdin);
        write(sockfd, buf, strlen(buf));
 
        memset(buf, 0, sizeof(buf));
        rv=read(sockfd, buf, sizeof(buf));
        if(rv <= 0)
        {
            printf("ERROR:socket disconnected or get error\n");
            goto CleanUp;
        }
 
        printf("<== %s\n", buf);
    }
 
 
CleanUp:
    close(sockfd);  /* We must close socket File Description when program exit*/
    return 0;
}