上山打老虎 发表于 2021-8-7 11:49:23

c tcp sockt server client -- demo

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;

    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 = '\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_tthread;
    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;
    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 = '\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 = '\0';
      printf("Receive from server:%s\n", receiveBuff);
    }

    close(clientSocketFd);
    return 0;
}



文档来源:51CTO技术博客https://blog.51cto.com/u_2498536/3304162
页: [1]
查看完整版本: c tcp sockt server client -- demo