server.c 6.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
#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 已提交
30
#include <pthread.h>
S
Shuaiqiang Chang 已提交
31

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

typedef struct {
  int port;
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
    {0, 'p', "port", 0, "The TCP or UDP port number to use for the connection. Default is 6041.", 1},
H
Hui Li 已提交
50 51
    {0, 'm', "max port", 0, "The max TCP or UDP port number to use for the connection. Default is 6060.", 2},
    {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
static void *bindTcpPort(void *sarg) {
  info_s *pinfo = (info_s *)sarg;
S
Shuaiqiang Chang 已提交
80 81 82 83 84 85 86 87
  int   port = pinfo->port;
  int   serverSocket;

  struct sockaddr_in server_addr;
  struct sockaddr_in clientAddr;
  int                addr_len = sizeof(clientAddr);
  int                client;
  char               buffer[BUFFER_SIZE];
H
Hui Li 已提交
88
  int                iDataNum = 0;
S
Shuaiqiang Chang 已提交
89 90

  if ((serverSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
H
Hui Li 已提交
91
    printf("socket() fail: %s", strerror(errno));
S
Shuaiqiang Chang 已提交
92 93 94 95 96 97 98 99 100
    return NULL;
  }

  bzero(&server_addr, sizeof(server_addr));
  server_addr.sin_family = AF_INET;
  server_addr.sin_port = htons(port);
  server_addr.sin_addr.s_addr = htonl(INADDR_ANY);

  if (bind(serverSocket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
H
Hui Li 已提交
101
    printf("port:%d bind() fail: %s", port, strerror(errno));
S
Shuaiqiang Chang 已提交
102 103 104 105
    return NULL;
  }

  if (listen(serverSocket, 5) < 0) {
H
Hui Li 已提交
106
    printf("listen() fail: %s", strerror(errno));
S
Shuaiqiang Chang 已提交
107 108 109
    return NULL;
  }

H
Hui Li 已提交
110
  //printf("Bind port: %d success\n", port);
S
Shuaiqiang Chang 已提交
111 112 113
  while (1) {
    client = accept(serverSocket, (struct sockaddr *)&clientAddr, (socklen_t *)&addr_len);
    if (client < 0) {
H
Hui Li 已提交
114
      printf("accept() fail: %s", strerror(errno));
S
Shuaiqiang Chang 已提交
115 116
      continue;
    }
H
Hui Li 已提交
117 118 119 120 121 122

    memset(buffer, 0, BUFFER_SIZE);
    int   nleft, nread;
    char *ptr = buffer;
    nleft = pinfo->pktLen;
    while (nleft > 0) {
H
Hui Li 已提交
123
      nread = recv(client, ptr, BUFFER_SIZE, 0);
H
Hui Li 已提交
124 125

      if (nread == 0) {
S
Shuaiqiang Chang 已提交
126
        break;
H
Hui Li 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
      } else if (nread < 0) {
        if (errno == EINTR) {
          continue;
        } else {
          printf("recv Client: %s pkg from TCP port: %d fail:%s.\n", inet_ntoa(clientAddr.sin_addr), port, strerror(errno));
          close(serverSocket);
          return NULL;
        }
      } else {
        nleft -= nread;
        ptr += nread;
        iDataNum += nread;
      }      
    }
    
    printf("recv Client: %s pkg from TCP port: %d, pkg len: %d\n", inet_ntoa(clientAddr.sin_addr), port, iDataNum);
    if (iDataNum > 0) {
      send(client, buffer, iDataNum, 0);
S
Shuaiqiang Chang 已提交
145 146
    }
  }
H
Hui Li 已提交
147
  
S
Shuaiqiang Chang 已提交
148 149 150 151
  close(serverSocket);
  return NULL;
}

H
Hui Li 已提交
152 153
static void *bindUdpPort(void *sarg) {
  info_s *pinfo = (info_s *)sarg;
S
Shuaiqiang Chang 已提交
154 155 156 157 158 159 160
  int   port = pinfo->port;
  int   serverSocket;

  struct sockaddr_in server_addr;
  struct sockaddr_in clientAddr;
  char               buffer[BUFFER_SIZE];
  int                iDataNum;
H
Hui Li 已提交
161
    
S
Shuaiqiang Chang 已提交
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
  if ((serverSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
    perror("socket");
    return NULL;
  }

  bzero(&server_addr, sizeof(server_addr));
  server_addr.sin_family = AF_INET;
  server_addr.sin_port = htons(port);
  server_addr.sin_addr.s_addr = htonl(INADDR_ANY);

  if (bind(serverSocket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
    perror("connect");
    return NULL;
  }

  socklen_t sin_size;

  while (1) {
H
Hui Li 已提交
180
    memset(buffer, 0, BUFFER_SIZE);
S
Shuaiqiang Chang 已提交
181 182 183 184 185 186 187 188 189 190

    sin_size = sizeof(*(struct sockaddr *)&server_addr);

    iDataNum = recvfrom(serverSocket, buffer, BUFFER_SIZE, 0, (struct sockaddr *)&clientAddr, &sin_size);

    if (iDataNum < 0) {
      perror("recvfrom null");
      continue;
    }
    if (iDataNum > 0) {
H
Hui Li 已提交
191 192
      printf("recv Client: %s pkg from UDP port: %d, pkg len: %d\n", inet_ntoa(clientAddr.sin_addr), port, iDataNum);
      //printf("Read msg from udp:%s ... %s\n", buffer, buffer+iDataNum-16);
S
Shuaiqiang Chang 已提交
193

H
Hui Li 已提交
194
      sendto(serverSocket, buffer, iDataNum, 0, (struct sockaddr *)&clientAddr, (int)sin_size);
S
Shuaiqiang Chang 已提交
195 196 197 198 199 200 201 202
    }
  }

  close(serverSocket);
  return NULL;
}


S
Shuaiqiang Chang 已提交
203
int main(int argc, char *argv[]) {
H
Hui Li 已提交
204
  SArguments arguments = {"127.0.0.1", 6030, 6042, 1000};
S
Shuaiqiang Chang 已提交
205
  argp_parse(&argp, argc, argv, 0, 0, &arguments);
H
Hui Li 已提交
206 207 208 209 210
  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 已提交
211
  int port = arguments.port;
S
Shuaiqiang Chang 已提交
212

H
Hui Li 已提交
213
  int num = arguments.max_port - arguments.port + 1;
S
Shuaiqiang Chang 已提交
214 215 216 217 218

  if (num < 0) {
    num = 1;
  }
  pthread_t *pids = malloc(2 * num * sizeof(pthread_t));
H
Hui Li 已提交
219 220
  info_s *     tinfos = malloc(num * sizeof(info_s));
  info_s *     uinfos = malloc(num * sizeof(info_s));
S
Shuaiqiang Chang 已提交
221

S
Shuaiqiang Chang 已提交
222
  for (size_t i = 0; i < num; i++) {
H
Hui Li 已提交
223 224
    info_s *tcpInfo = tinfos + i;
    tcpInfo->port = port + i;
H
Hui Li 已提交
225
    tcpInfo->pktLen = arguments.pktLen;
S
Shuaiqiang Chang 已提交
226

H
Hui Li 已提交
227 228 229 230
    if (pthread_create(pids + i, NULL, bindTcpPort, tcpInfo) != 0) 
    {
      printf("create thread fail, port:%d.\n", port);
      exit(-1);
S
Shuaiqiang Chang 已提交
231 232
    }

H
Hui Li 已提交
233 234 235 236 237 238
    info_s *udpInfo = uinfos + i;
    udpInfo->port = port + i;
    if (pthread_create(pids + num + i, NULL, bindUdpPort, udpInfo) != 0)
    {                          
      printf("create thread fail, port:%d.\n", port);
      exit(-1);
S
Shuaiqiang Chang 已提交
239 240
    }
  }
H
Hui Li 已提交
241
  
S
Shuaiqiang Chang 已提交
242
  for (int i = 0; i < num; i++) {
S
Shuaiqiang Chang 已提交
243
    pthread_join(pids[i], NULL);
S
Shuaiqiang Chang 已提交
244
    pthread_join(pids[(num + i)], NULL);
S
Shuaiqiang Chang 已提交
245 246
  }
}