您当前的位置:首页 > 计算机 > 编程开发 > C语言

linux下C语言TCP实现聊天

时间:10-01来源:作者:点击数:

一、代码

  • 服务端
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
       
int main(int argc, char const *argv[])
{   
    time_t timer;//time_t就是long int 类型
    struct tm *tblock;

    //1.创建socket套接字
    int fd = socket(AF_INET,SOCK_STREAM,0);

    //2.绑定IP和端口号
    struct sockaddr_in saddr;
    saddr.sin_family = AF_INET;
    saddr.sin_addr.s_addr = INADDR_ANY;//0.0.0.0
    saddr.sin_port = htons(9999);
    bind(fd,(struct sockaddr*)&saddr,sizeof(saddr));

    //3.监听
    listen(fd,8);

    //4.接收客户端连接
    struct sockaddr_in clientaddr;
    socklen_t len = sizeof(clientaddr);
    int cfd = accept(fd,(struct sockaddr*)&clientaddr,&len);//阻塞

    //输出客户端信息
    char clientIP[16]="";
    inet_ntop(AF_INET,&clientaddr.sin_addr.s_addr,clientIP,sizeof(clientIP));
    unsigned short clientport = ntohs(clientaddr.sin_port);
    printf("client IP:%s,port:%d\n",clientIP,clientport);


    //读写
    char buf[1024]={0};
    while(1)
    {
        //发送数据
        fgets(buf,sizeof(buf),stdin);
        timer = time(NULL); 
        tblock = localtime(&timer);
        write(cfd,buf,sizeof(buf));
        printf("%sserver says:%s\n",asctime(tblock),buf);
        memset(buf,0,sizeof(buf));

        //接收数
        timer = time(NULL); 
        tblock = localtime(&timer);
        int len= read(cfd,buf,sizeof(buf));
        if(len==-1)
        {
            perror("read");
            exit(-1);
        }
        else if(len==0)
        {
            printf("client close\n");
            exit(0);
        }
        else
        {
            printf("%sclient says:%s\n",asctime(tblock),buf);
        }
        memset(buf,0,sizeof(buf));
    }

    close(cfd);
    close(fd);
    return 0;
}

  • 客户端
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <time.h>

       
int main(int argc, char const *argv[])
{
    time_t timer;//time_t就是long int 类型
    struct tm *tblock;

    //1.创建socket套接字
    int fd = socket(AF_INET,SOCK_STREAM,0);

    //2.连接服务器
    struct sockaddr_in serveraddr;
    serveraddr.sin_family = AF_INET;
    inet_pton(AF_INET,"192.168.88.111",&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);
    }
    //输出服务端信息
    char serverIP[16]="";
    inet_ntop(AF_INET,&serveraddr.sin_addr.s_addr,serverIP,sizeof(serverIP));
    unsigned short serverport = ntohs(serveraddr.sin_port);
    printf("server IP:%s,port:%d\n",serverIP,serverport);


    //读写
        //读写
    char buf[1024]={0};
    while(1)
    {

        timer = time(NULL); 
        tblock = localtime(&timer);
        int len = read(fd,buf,sizeof(buf));
        if(len==-1)
        {
            perror("read");
            exit(-1);
        }
        else if(len==0)
        {
            printf("server close\n");
            exit(0);
        }
        else
        {
            printf("%sserver says:%s\n",asctime(tblock),buf);
        }
        
        memset(buf,0,sizeof(buf));
        //发送数据
        fgets(buf,sizeof(buf),stdin);
        timer = time(NULL); 
        tblock = localtime(&timer);
        write(fd,buf,sizeof(buf));
        printf("%sclient says:%s\n",asctime(tblock),buf);
        memset(buf,0,sizeof(buf));

    }

    close(fd);
    return 0;
}

二、效果

在这里插入图片描述
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
上一篇:从文件中读取yuv和h264数据 下一篇:很抱歉没有了
推荐内容
相关内容
栏目更新
栏目热门
本栏推荐