• 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[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
    ");
    
        while(listen(serverSocketFd, 10) == -1);
        printf("Listen port 8000 success
    ");
    
        clientSocketFd = accept(serverSocketFd, (struct sockaddr *)&clientAddress, &addrLength);
        printf("Client connecting...
    ");
    
    //    readByteCount = send(clientSocketFd, "hello guanxianseng", 21, 0);
        dealClientConnection(clientSocketFd);
        while ((readByteCount = recv(clientSocketFd, recvBuff, BUFFSIZE, 0)) > 0) {
            recvBuff[readByteCount] = '';
            printf("receive from client:%s
    ", 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
    ");
    }
    

      

      

    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]
    ");
    
        sendByteCount = recv(clientSocketFd, receiveBuff, BUFFSIZE, 0);
        receiveBuff[sendByteCount] = '';
        printf("receive from server:%s
    ", receiveBuff);
    
        while (1) {
            printf("Enter something:
    ");
            scanf("%s", receiveBuff);
            send(clientSocketFd, receiveBuff, strlen(receiveBuff), 0);
            sendByteCount = recv(clientSocketFd, receiveBuff, BUFFSIZE, 0);
            receiveBuff[sendByteCount] = '';
            printf("Receive from server:%s
    ", receiveBuff);
        }
    
        close(clientSocketFd);
        return 0;
    }
    

      

  • 相关阅读:
    标准模板库中的链表(list)
    C++接口
    qsort
    C++异常
    标准模板库中的向量(vector)
    后缀表达式/逆波兰表达式
    静态数据成员
    c++存储区域
    #define 和 const
    Git 的下载与安装
  • 原文地址:https://www.cnblogs.com/luckygxf/p/12271559.html
Copyright © 2020-2023  润新知