最新要闻

广告

手机

iphone11大小尺寸是多少?苹果iPhone11和iPhone13的区别是什么?

iphone11大小尺寸是多少?苹果iPhone11和iPhone13的区别是什么?

警方通报辅警执法直播中被撞飞:犯罪嫌疑人已投案

警方通报辅警执法直播中被撞飞:犯罪嫌疑人已投案

家电

新动态:TCP通信聊天服务端和客户端(C/C++语言开发)附完整源码

来源:博客园


(资料图片)

距离上次学Python写的Python实现简单聊天室已经过去好久了,现在学c++又写了一遍,其实过程差不多,无非是语法的变化,目前仅实现最简单的一对一的通信,然后改就是了,接下来应该是多线程了,话不多说直接贴上源码;

一、服务端源码

#include #include // #include // #include #include #include #include #include int main(){    //1、socket    int sfd = socket(AF_INET, SOCK_STREAM, 0);    if (sfd == -1){        perror("socket");        exit(-1);    }        // 2、bind ip and port    struct sockaddr_in saddr;   //专用socket地址    saddr.sin_family = PF_INET;//协议族,ipv4的AF_INET也可以    // inet_pton(AF_INET, "192.168.193.128", saddr.sin_addr.s_addr);    saddr.sin_addr.s_addr = INADDR_ANY; // 监听所有外网接口, INADDR_ANY = 0    saddr.sin_port = htons(9999);//绑定的服务端口9999    int ret = bind(sfd, (struct sockaddr *)&saddr, sizeof(saddr));    if (ret == -1){        perror("bind");        exit(-1);    }    // 3、listen     ret =  listen(sfd, 128);    if (ret == -1){        perror("listen");        exit(-1);    }    // 4、accept    struct sockaddr_in  client_addr;     socklen_t addrlen = sizeof(client_addr);//客户端socket地址的大小    int newfd = accept(sfd, (struct sockaddr *)&client_addr, &addrlen);    if (newfd == -1){        perror("accept");        exit(-1);    }    // output connect message    char clientIP[16];    inet_ntop(AF_INET, &client_addr.sin_addr.s_addr, clientIP, sizeof(clientIP));    unsigned short clientPort = ntohs(client_addr.sin_port);    printf("client %s:%d is connect. \n", clientIP, clientPort);    // 5、recv and send    char recvBuf[1024] = {0};    while(1){        memset(recvBuf, 0, sizeof(recvBuf)); //clear buffer        int num = read(newfd, recvBuf, sizeof(recvBuf));//read datas        if (num == -1){            perror("read");            exit(-1);        } else if (num > 0){            printf("client message : %s\n", recvBuf);        } else if (num == 0){            printf("server close.");            break;        }                char respondData[] = "Your message is receive,thanks!";        write(newfd, respondData, sizeof(respondData));    }    // 6、close    close(sfd);    close(newfd);    return 0;}

二、客户端源码

#include #include #include #include #include int main() {    // 1.创建套接字    int fd = socket(AF_INET, SOCK_STREAM, 0);    if(fd == -1) {        perror("socket");        exit(-1);    }    // 2.连接服务器端    struct sockaddr_in serveraddr;    serveraddr.sin_family = AF_INET;    inet_pton(AF_INET, "192.168.223.128", &serveraddr.sin_addr.s_addr);    serveraddr.sin_port = htons(9999);    int ret = connect(fd, (struct sockaddr *)&serveraddr, sizeof(serveraddr));    if(ret == -1) {        perror("connect");        exit(-1);    }        // 3. 通信    char recvBuf[1024] = {0};    char client_data[1024];// 从键盘输入,给客户端发送数据    while(1) {        memset(client_data, 0, sizeof(client_data));        printf("请输入发送数据:");        scanf("%s", client_data);        write(fd, client_data, strlen(client_data));        sleep(1);                int ret = read(fd, recvBuf, sizeof(recvBuf));        if(ret == -1) {            perror("read");            exit(-1);        } else if(ret > 0) {            printf("server message : %s\n", recvBuf);        } else if(ret == 0) {            printf("server closed...\n");            break;        }    }    // 关闭连接    close(fd);    return 0;}

三、效果

服务端:客户端:

关键词: 发送数据 服务器端 关闭连接