评论

收藏

[C++] c tcp sockt server client -- demo

编程语言 编程语言 发布于:2021-08-07 11:49 | 阅读数:217 | 评论:0

server.c
//
// Created by gxf on 2020/2/6.
//
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#define BUFFSIZE 1024
void dealClientConnection(int clientSocketFd);
void sendMsg2Client(int clientSocketFd);
int main(int argc, char **argv) {
  int serverSocketFd, clientSocketFd, addrLength, readByteCount, i;
  struct sockaddr_in serverAddress;
  struct sockaddr_in clientAddress;
  char recvBuff[BUFFSIZE];
  serverAddress.sin_family = AF_INET;
  serverAddress.sin_port = htons(8000);
  serverAddress.sin_addr.s_addr = INADDR_ANY;
  bzero(&(serverAddress.sin_zero), 8);
  addrLength = sizeof(struct sockaddr_in);
  serverSocketFd = socket(AF_INET, SOCK_STREAM, 0);
  while((bind(serverSocketFd, (struct sockaddr *)&serverAddress, addrLength)) == -1);
  printf("Bind port 8000 success\n");
  while(listen(serverSocketFd, 10) == -1);
  printf("Listen port 8000 success\n");
  clientSocketFd = accept(serverSocketFd, (struct sockaddr *)&clientAddress, &addrLength);
  printf("Client connecting...\n");
//  readByteCount = send(clientSocketFd, "hello guanxianseng", 21, 0);
  dealClientConnection(clientSocketFd);
  while ((readByteCount = recv(clientSocketFd, recvBuff, BUFFSIZE, 0)) > 0) {
    recvBuff[readByteCount] = '\0';
    printf("receive from client:%s\n", recvBuff);
    if (send(clientSocketFd, recvBuff, readByteCount, 0) < 0) {
      perror("Write fail");
      return 1;
    }
  }
  close(serverSocketFd);
  close(clientSocketFd);
  return 0;
}
void dealClientConnection(int clientSocketFd) {
  pthread_t  thread;
  pthread_create(&thread, NULL,  sendMsg2Client, clientSocketFd);
}
void sendMsg2Client(int clientSocketFd) {
  send(clientSocketFd, "hello guanxianseng", 21, 0);
  printf("send msg2client\n");
}
client.c
//
// Created by gxf on 2020/2/6.
//
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#define BUFFSIZE 1024

int main(int argc, char **argv) {
  int clientSocketFd, sendByteCount;
  char receiveBuff[BUFFSIZE];
  struct sockaddr_in serverAddress;
  while ((clientSocketFd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    perror("Socket fail");
    sleep(1);
  }
  serverAddress.sin_family = AF_INET;
  serverAddress.sin_port = htons(8000);
  serverAddress.sin_addr.s_addr = inet_addr("127.0.0.1");
  bzero(&(serverAddress.sin_zero), 8);
  while (connect(clientSocketFd, (struct sockaddr *)&serverAddress, sizeof(struct sockaddr)) == -1) {
    perror("Connect fail");
    sleep(1);
  }
  printf("Connected server]\n");
  sendByteCount = recv(clientSocketFd, receiveBuff, BUFFSIZE, 0);
  receiveBuff[sendByteCount] = '\0';
  printf("receive from server:%s\n", receiveBuff);
  while (1) {
    printf("Enter something:\n");
    scanf("%s", receiveBuff);
    send(clientSocketFd, receiveBuff, strlen(receiveBuff), 0);
    sendByteCount = recv(clientSocketFd, receiveBuff, BUFFSIZE, 0);
    receiveBuff[sendByteCount] = '\0';
    printf("Receive from server:%s\n", receiveBuff);
  }
  close(clientSocketFd);
  return 0;
}


关注下面的标签,发现更多相似文章