taosnetwork_client.c 1.8 KB
Newer Older
S
Shuaiqiang Chang 已提交
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
/*
 * 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/>.
 */

#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#define SERVER_PORT 8000
#define SIZE 200

int main() {
  struct sockaddr_in servaddr, cliaddr;
  socklen_t          cliaddr_len;
  int                client_sockfd;
  char               buf[SIZE];
  char               recvbuf[SIZE];

  int i, n, flag = 0;

  int len, iDataNum;

  client_sockfd = socket(AF_INET, SOCK_STREAM, 0);
  bzero(&servaddr, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  servaddr.sin_port = htons(SERVER_PORT);

  if (connect(client_sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
    printf("Connected error..\n");
    return 0;
  }
  printf("Connected to server..\n");

  /*循环的发送接收信息并打印接收信息(可以按需发送)--recv返回接收到的字节数,send返回发送的字节数*/
  while (1) {
    printf("Enter string to send:");
    scanf("%s", buf);
    if (!strcmp(buf, "quit")) {
      break;
    }
    len = (sizeof buf);

    recvbuf[0] = '\0';

    iDataNum = recv(client_sockfd, recvbuf, SIZE, 0);

    recvbuf[iDataNum] = '\0';

    printf("%s\n", recvbuf);
  }
  return 0;
}