server.c 5.5 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 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

#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;
  int type;  // 0: tcp, 1: udo, default: 0
} info;

static void *bindPort(void *sarg) {
  info *pinfo = (info *)sarg;
  int   port = pinfo->port;
  int   type = pinfo->type;
  int   serverSocket;

  struct sockaddr_in server_addr;
  struct sockaddr_in clientAddr;
  int                addr_len = sizeof(clientAddr);
  int                client;
  char               buffer[BUFFER_SIZE];
  int                iDataNum;

  if ((serverSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 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;
  }

  if (listen(serverSocket, 5) < 0) {
    perror("listen");
    return NULL;
  }

  printf("Bind port: %d success\n", port);
  while (1) {
    client = accept(serverSocket, (struct sockaddr *)&clientAddr, (socklen_t *)&addr_len);
    if (client < 0) {
      perror("accept");
      continue;
    }
    printf("=================================\n");

    printf("Client ip is %s, Server port is %d\n", inet_ntoa(clientAddr.sin_addr), port);
    while (1) {
      buffer[0] = '\0';
      iDataNum = recv(client, buffer, BUFFER_SIZE, 0);

      if (iDataNum < 0) {
        perror("recv null");
        continue;
      }
      if (iDataNum > 0) {
        buffer[iDataNum] = '\0';
        printf("read msg:%s\n", buffer);
        if (strcmp(buffer, "quit") == 0) break;
        buffer[0] = '\0';

        sprintf(buffer, "ack port_%d", port);
        printf("send ack msg:%s\n", buffer);

        send(client, buffer, strlen(buffer), 0);
        break;
      }
    }
    printf("=================================\n");
  }
  close(serverSocket);
  return NULL;
}

static void *bindUPort(void *sarg) {
  info *pinfo = (info *)sarg;
  int   port = pinfo->port;
  int   type = pinfo->type;
  int   serverSocket;

  struct sockaddr_in server_addr;
  struct sockaddr_in clientAddr;
  int                addr_len = sizeof(clientAddr);
  int                client;
  char               buffer[BUFFER_SIZE];
  int                iDataNum;

  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;
  printf("Bind port: %d success\n", port);

  while (1) {
    buffer[0] = '\0';

    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) {
      printf("=================================\n");

      printf("Client ip is %s, Server port is %d\n", inet_ntoa(clientAddr.sin_addr), port);
      buffer[iDataNum] = '\0';
      printf("Read msg from udp:%s\n", buffer);
      if (strcmp(buffer, "quit") == 0) break;
      buffer[0] = '\0';

      sprintf(buffer, "ack port_%d by udp", port);
      printf("Send ack msg by udp:%s\n", buffer);

      sendto(serverSocket, buffer, strlen(buffer), 0, (struct sockaddr *)&clientAddr, (int)sin_size);

      send(client, buffer, strlen(buffer), 0);
      printf("=================================\n");
    }
  }

  close(serverSocket);
  return NULL;
}


int main() {
  int        port = 6020;
S
Shuaiqiang Chang 已提交
175 176 177
  pthread_t *pids = malloc(60 * sizeof(pthread_t));
  info *     infos = malloc(30 * sizeof(info));
  info *     uinfos = malloc(30 * sizeof(info));
S
Shuaiqiang Chang 已提交
178

S
Shuaiqiang Chang 已提交
179
  for (size_t i = 0; i < 30; i++) {
S
Shuaiqiang Chang 已提交
180 181 182 183 184 185 186 187 188 189 190 191 192 193
    port++;

    info *pinfo = infos++;
    pinfo->port = port;

    if (pthread_create(pids + i, NULL, bindPort, pinfo) != 0)  //创建线程
    {                                                          //创建线程失败
      printf("创建线程失败: %d.\n", port);
      exit(0);
    }

    info *uinfo = uinfos++;
    uinfo->port = port;
    uinfo->type = 1;
S
Shuaiqiang Chang 已提交
194
    if (pthread_create(pids + 30 + i, NULL, bindUPort, uinfo) != 0)  //创建线程
S
Shuaiqiang Chang 已提交
195 196 197 198 199
    {                                                                //创建线程失败
      printf("创建线程失败: %d.\n", port);
      exit(0);
    }
  }
S
Shuaiqiang Chang 已提交
200
  for (int i = 0; i < 30; i++) {
S
Shuaiqiang Chang 已提交
201 202 203 204
    pthread_join(pids[i], NULL);
    pthread_join(pids[(10 + i)], NULL);
  }
}