client.c 4.7 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 31 32 33 34 35 36 37
#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>

#define BUFFER_SIZE 200

typedef struct {
  int   port;
  char *host[15];
} info;

S
Shuaiqiang Chang 已提交
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
typedef struct Arguments {
  char *   host;
  uint16_t port;
  uint16_t max_port;
} SArguments;

static struct argp_option options[] = {
    {0, 'h', "host", 0, "The host to connect to TDEngine. Default is localhost.", 0},
    {0, 'p', "port", 0, "The TCP or UDP port number to use for the connection. Default is 6020.", 1},
    {0, 'm', "max port", 0, "The max TCP or UDP port number to use for the connection. Default is 6050.", 2}};

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;
  }
  return 0;
}

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

S
Shuaiqiang Chang 已提交
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
void *checkPort(void *sarg) {
  info *pinfo = (info *)sarg;
  int   port = pinfo->port;
  char *host = *pinfo->host;
  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) {
    perror("socket");
    return NULL;
  }
  serverAddr.sin_family = AF_INET;
  serverAddr.sin_port = htons(port);

  serverAddr.sin_addr.s_addr = inet_addr(host);

  printf("=================================\n");
  if (connect(clientSocket, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) < 0) {
    perror("connect");
    return NULL;
  }
  printf("Connect to: %s:%d...success\n", host, port);

  sprintf(sendbuf, "send port_%d", port);
  send(clientSocket, sendbuf, strlen(sendbuf), 0);
  printf("Send msg_%d: %s\n", port, sendbuf);

  recvbuf[0] = '\0';
  iDataNum = recv(clientSocket, recvbuf, BUFFER_SIZE, 0);
  recvbuf[iDataNum] = '\0';
  printf("Read ack msg_%d: %s\n", port, recvbuf);

  printf("=================================\n");
  close(clientSocket);
  return NULL;
}

void *checkUPort(void *sarg) {
  info *pinfo = (info *)sarg;
  int   port = pinfo->port;
  char *host = *pinfo->host;
  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");
    return NULL;
  }
  serverAddr.sin_family = AF_INET;
  serverAddr.sin_port = htons(port);

  serverAddr.sin_addr.s_addr = inet_addr(host);

  printf("=================================\n");

  sprintf(sendbuf, "send msg port_%d by udp", port);

S
Shuaiqiang Chang 已提交
131
  socklen_t sin_size = sizeof(*(struct sockaddr *)&serverAddr);
S
Shuaiqiang Chang 已提交
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146

  sendto(clientSocket, sendbuf, strlen(sendbuf), 0, (struct sockaddr *)&serverAddr, (int)sin_size);

  printf("Send msg_%d by udp: %s\n", port, sendbuf);

  recvbuf[0] = '\0';
  iDataNum = recvfrom(clientSocket, recvbuf, BUFFER_SIZE, 0, (struct sockaddr *)&serverAddr, &sin_size);
  recvbuf[iDataNum] = '\0';
  printf("Read ack msg_%d from udp: %s\n", port, recvbuf);

  printf("=================================\n");
  close(clientSocket);
  return NULL;
}

S
Shuaiqiang Chang 已提交
147 148 149 150 151 152 153 154 155
int main(int argc, char *argv[]) {
  SArguments arguments = {"127.0.0.1", 6020, 6050};

  argp_parse(&argp, argc, argv, 0, 0, &arguments);

  printf("host: %s\tport: %d\tmax_port: %d\n", arguments.host, arguments.port, arguments.max_port);

  int   port = arguments.port;
  char *host = arguments.host;
S
Shuaiqiang Chang 已提交
156 157
  info *tinfo = malloc(sizeof(info));
  info *uinfo = malloc(sizeof(info));
S
Shuaiqiang Chang 已提交
158

S
Shuaiqiang Chang 已提交
159
  for (; port < arguments.max_port; port++) {
S
Shuaiqiang Chang 已提交
160 161
    printf("For test: %s:%d\n", host, port);

S
Shuaiqiang Chang 已提交
162 163 164
    *tinfo->host = host;
    tinfo->port = port;
    checkPort(tinfo);
S
Shuaiqiang Chang 已提交
165 166 167 168 169

    *uinfo->host = host;
    uinfo->port = port;
    checkUPort(uinfo);
  }
S
Shuaiqiang Chang 已提交
170 171
  free(tinfo);
  free(uinfo);
S
Shuaiqiang Chang 已提交
172
}