client.c 6.4 KB
Newer Older
S
Shuaiqiang Chang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
S
Shuaiqiang Chang 已提交
15

S
Shuaiqiang Chang 已提交
16
#include <argp.h>
S
Shuaiqiang Chang 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <netinet/in.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

H
Hui Li 已提交
31
#define MAX_PKG_LEN (64*1000)
H
Hui Li 已提交
32
#define BUFFER_SIZE (MAX_PKG_LEN + 1024)
S
Shuaiqiang Chang 已提交
33 34 35

typedef struct {
  int   port;
H
Hui Li 已提交
36
  char *host;
H
Hui Li 已提交
37
  uint16_t pktLen;
H
Hui Li 已提交
38
} info_s;
S
Shuaiqiang Chang 已提交
39

S
Shuaiqiang Chang 已提交
40 41 42 43
typedef struct Arguments {
  char *   host;
  uint16_t port;
  uint16_t max_port;
H
Hui Li 已提交
44
  uint16_t pktLen;
S
Shuaiqiang Chang 已提交
45 46 47 48
} SArguments;

static struct argp_option options[] = {
    {0, 'h', "host", 0, "The host to connect to TDEngine. Default is localhost.", 0},
H
Hui Li 已提交
49 50
    {0, 'p', "port", 0, "The TCP or UDP port number to use for the connection. Default is 6030.", 1},
    {0, 'm', "max port", 0, "The max TCP or UDP port number to use for the connection. Default is 6060.", 2},
H
Hui Li 已提交
51
    {0, 'l', "test pkg len", 0, "The len of pkg for test. Default is 1000 Bytes, max not greater than 64k Bytes.\nNotes: This parameter must be consistent between the client and the server.", 3}};
S
Shuaiqiang Chang 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65

static error_t parse_opt(int key, char *arg, struct argp_state *state) {

  SArguments *arguments = state->input;
  switch (key) {
    case 'h':
      arguments->host = arg;
      break;
    case 'p':
      arguments->port = atoi(arg);
      break;
    case 'm':
      arguments->max_port = atoi(arg);
      break;
H
Hui Li 已提交
66 67 68
    case 'l':
      arguments->pktLen = atoi(arg);
      break;
H
Hui Li 已提交
69

H
Hui Li 已提交
70
    default:
H
Hui Li 已提交
71
      return ARGP_ERR_UNKNOWN;
S
Shuaiqiang Chang 已提交
72 73 74 75 76 77
  }
  return 0;
}

static struct argp argp = {options, parse_opt, 0, 0};

H
Hui Li 已提交
78 79 80
int checkTcpPort(info_s *info) {
  int   port = info->port;
  char *host = info->host;
S
Shuaiqiang Chang 已提交
81 82 83 84 85 86 87
  int   clientSocket;

  struct sockaddr_in serverAddr;
  char               sendbuf[BUFFER_SIZE];
  char               recvbuf[BUFFER_SIZE];
  int                iDataNum;
  if ((clientSocket = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
H
Hui Li 已提交
88
    printf("socket() fail: %s\n", strerror(errno));
H
Hui Li 已提交
89
    return -1;
S
Shuaiqiang Chang 已提交
90 91 92 93 94 95
  }
  serverAddr.sin_family = AF_INET;
  serverAddr.sin_port = htons(port);

  serverAddr.sin_addr.s_addr = inet_addr(host);

H
Hui Li 已提交
96
  //printf("=================================\n");
S
Shuaiqiang Chang 已提交
97
  if (connect(clientSocket, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) < 0) {
H
Hui Li 已提交
98
    printf("connect() fail: %s\n", strerror(errno));
H
Hui Li 已提交
99
    return -1;
S
Shuaiqiang Chang 已提交
100
  }
H
Hui Li 已提交
101
  //printf("Connect to: %s:%d...success\n", host, port);
H
Hui Li 已提交
102 103 104 105 106
  memset(sendbuf, 0, BUFFER_SIZE);
  memset(recvbuf, 0, BUFFER_SIZE);

  sprintf(sendbuf, "client send tcp pkg to %s:%d, content: 1122334455", host, port);
  sprintf(sendbuf + info->pktLen - 16, "1122334455667788");
S
Shuaiqiang Chang 已提交
107

H
Hui Li 已提交
108
  send(clientSocket, sendbuf, info->pktLen, 0);
S
Shuaiqiang Chang 已提交
109

H
Hui Li 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
  memset(recvbuf, 0, BUFFER_SIZE);
  int   nleft, nread;
  char *ptr = recvbuf;
  nleft = info->pktLen;
  while (nleft > 0) {
    nread = recv(clientSocket, ptr, BUFFER_SIZE, 0);;
  
    if (nread == 0) {
      break;
    } else if (nread < 0) {
      if (errno == EINTR) {
        continue;
      } else {
        printf("recv ack pkg from TCP port: %d fail:%s.\n", port, strerror(errno));
        close(clientSocket);
        return -1;
      }
    } else {
      nleft -= nread;
      ptr += nread;
      iDataNum += nread;
    }      
  }

H
Hui Li 已提交
134
  if (iDataNum < info->pktLen) {
H
Hui Li 已提交
135
    printf("recv ack pkg len: %d, less than req pkg len: %d from tcp port: %d\n", iDataNum, info->pktLen, port);
H
Hui Li 已提交
136 137 138
    return -1;
  }
  //printf("Read ack pkg len:%d from tcp port: %d, buffer: %s  %s\n", info->pktLen, port, recvbuf, recvbuf+iDataNum-8);
S
Shuaiqiang Chang 已提交
139 140

  close(clientSocket);
H
Hui Li 已提交
141
  return 0;
S
Shuaiqiang Chang 已提交
142 143
}

H
Hui Li 已提交
144
int checkUdpPort(info_s *info) {
H
Hui Li 已提交
145 146
  int   port = info->port;
  char *host = info->host;
S
Shuaiqiang Chang 已提交
147 148 149 150 151 152 153 154
  int   clientSocket;

  struct sockaddr_in serverAddr;
  char               sendbuf[BUFFER_SIZE];
  char               recvbuf[BUFFER_SIZE];
  int                iDataNum;
  if ((clientSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
    perror("socket");
H
Hui Li 已提交
155
    return -1;
S
Shuaiqiang Chang 已提交
156
  }
H
Hui Li 已提交
157
  
S
Shuaiqiang Chang 已提交
158 159 160
  serverAddr.sin_family = AF_INET;
  serverAddr.sin_port = htons(port);
  serverAddr.sin_addr.s_addr = inet_addr(host);
H
Hui Li 已提交
161 162 163
  
  memset(sendbuf, 0, BUFFER_SIZE);
  memset(recvbuf, 0, BUFFER_SIZE);
S
Shuaiqiang Chang 已提交
164

H
Hui Li 已提交
165 166
  sprintf(sendbuf, "client send udp pkg to %s:%d, content: 1122334455", host, port);
  sprintf(sendbuf + info->pktLen - 16, "1122334455667788");
S
Shuaiqiang Chang 已提交
167

S
Shuaiqiang Chang 已提交
168
  socklen_t sin_size = sizeof(*(struct sockaddr *)&serverAddr);
S
Shuaiqiang Chang 已提交
169

H
Hui Li 已提交
170
  int code = sendto(clientSocket, sendbuf, info->pktLen, 0, (struct sockaddr *)&serverAddr, (int)sin_size);
H
Hui Li 已提交
171 172 173 174
  if (code < 0) {
    perror("sendto");
    return -1;
  }
S
Shuaiqiang Chang 已提交
175 176 177

  iDataNum = recvfrom(clientSocket, recvbuf, BUFFER_SIZE, 0, (struct sockaddr *)&serverAddr, &sin_size);

H
Hui Li 已提交
178 179 180 181 182 183
  if (iDataNum < info->pktLen) {
    printf("Read ack pkg len: %d, less than req pkg len: %d from udp port: %d\n", iDataNum, info->pktLen, port);
    return -1;
  }
  
  //printf("Read ack pkg len:%d from udp port: %d, buffer: %s  %s\n", info->pktLen, port, recvbuf, recvbuf+iDataNum-8);
S
Shuaiqiang Chang 已提交
184
  close(clientSocket);
H
Hui Li 已提交
185
  return 0;
S
Shuaiqiang Chang 已提交
186 187
}

S
Shuaiqiang Chang 已提交
188
int main(int argc, char *argv[]) {
H
Hui Li 已提交
189
  SArguments arguments = {"127.0.0.1", 6030, 6060, 1000};
H
Hui Li 已提交
190 191 192
  info_s  info;
  int ret;
  
S
Shuaiqiang Chang 已提交
193
  argp_parse(&argp, argc, argv, 0, 0, &arguments);
H
Hui Li 已提交
194 195 196 197
  if (arguments.pktLen > MAX_PKG_LEN) {
    printf("test pkg len overflow: %d, max len not greater than %d bytes\n", arguments.pktLen, MAX_PKG_LEN);
    exit(0);
  }
S
Shuaiqiang Chang 已提交
198

H
Hui Li 已提交
199
  printf("host: %s\tport: %d\tmax_port: %d\tpkgLen: %d\n", arguments.host, arguments.port, arguments.max_port, arguments.pktLen);
S
Shuaiqiang Chang 已提交
200 201

  int   port = arguments.port;
S
Shuaiqiang Chang 已提交
202

H
Hui Li 已提交
203
  info.host = arguments.host;
H
Hui Li 已提交
204
  info.pktLen = arguments.pktLen;
S
Shuaiqiang Chang 已提交
205

H
Hui Li 已提交
206 207 208
  for (; port <= arguments.max_port; port++) {
    //printf("test: %s:%d\n", info.host, port);
    printf("\n");
H
Hui Li 已提交
209 210 211 212

    info.port = port;
    ret = checkTcpPort(&info);
    if (ret != 0) {
H
Hui Li 已提交
213
      printf("tcp port:%d test fail.\t\t", port);
H
Hui Li 已提交
214
    } else {
H
Hui Li 已提交
215
      printf("tcp port:%d test ok.\t\t", port);
H
Hui Li 已提交
216 217
    }
    
H
Hui Li 已提交
218
    ret = checkUdpPort(&info);
H
Hui Li 已提交
219
    if (ret != 0) {
H
Hui Li 已提交
220
      printf("udp port:%d test fail.\t\t", port);
H
Hui Li 已提交
221
    } else {
H
Hui Li 已提交
222
      printf("udp port:%d test ok.\t\t", port);
H
Hui Li 已提交
223
    }
S
Shuaiqiang Chang 已提交
224
  }
H
Hui Li 已提交
225 226
  printf("\n");
  return 0;
H
Hui Li 已提交
227
}