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

Posted Binge

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了TCP通信聊天服务端和客户端(C/C++语言开发)附完整源码相关的知识,希望对你有一定的参考价值。

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

一、服务端源码

#include <stdio.h>
#include <arpa/inet.h>
// #include <sys/types.h>
// #include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

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 <stdio.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>

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;

三、效果

服务端:

客户端:

以上是关于TCP通信聊天服务端和客户端(C/C++语言开发)附完整源码的主要内容,如果未能解决你的问题,请参考以下文章

基于TCP的聊天系统

基于TCP的聊天系统

PHP的Socket怎么进行服务端与客户端的相互通信?

Java实现一个简单的网络聊天程序

day31(2023.3.31)

c#UDP聊天程序,关于服务端和客户端的功能!急!!!!