网络编程——聊天程序实现

1、UDP实现

服务器端:

cpp 复制代码
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h> 
#include <time.h>
#include <unistd.h>
#include<pthread.h>
typedef struct sockaddr *(SA);
//th函数是一个线程函数,负责持续接收来自客户端的消息
void* th(void* arg)
{
    int fd=*(int*)arg;
    char buf[512];
    while (1)
    {
        recvfrom(fd, buf, sizeof(buf), 0, NULL,NULL);
        if(strcmp(buf, "#quit")==0)
        {
            printf("client has left\n");
            exit(0);
        }
        printf("Me:%s\n",buf);
    }
    return NULL;
}
int main()
{
//1创建UDP套接字
    int udpfd=socket(AF_INET, SOCK_DGRAM, 0);
    if(-1==udpfd)
    {
        perror("socket");
        return 1;
    }
//2设置服务器地址
    struct sockaddr_in ser,cli;
    bzero(&ser, sizeof(ser));
    bzero(&cli, sizeof(cli));
    ser.sin_family=AF_INET;
    ser.sin_port=htons(50000);
    ser.sin_addr.s_addr=inet_addr("192.168.31.86");
//3绑定套接字
    int ret=bind(udpfd, (SA)&ser, sizeof(ser));
    if(-1==ret)
    {
        perror("bind");
        return 1;
    }
//4等待客户端连接
    char buf[512];
    socklen_t len=sizeof(cli);
    recvfrom(udpfd, buf, sizeof(buf), 0, (SA)&cli, &len);
//5创建接收线程
    pthread_t tid;
    pthread_create(&tid,NULL,th,&udpfd);
//6主线程循环发送消息给客户端
    printf("You:");
    while (1)
    {
        fgets(buf, sizeof(buf), stdin);
        sendto(udpfd, buf, sizeof(buf), 0, (SA)&cli, len);
        if(0==strcmp("#quit", buf))
        {
            exit(0);
        }
        printf("You:");
    }
//清理工作
    pthread_join(tid1, NULL);
    close(udpfd);
    return 0;
}

客户端:

cpp 复制代码
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h> 
#include <time.h>
#include <unistd.h>
#include<pthread.h>
typedef struct sockaddr *(SA);
//th函数是一个线程函数,负责持续接收来自服务器的消息
void* th1(void* arg)
{
    int fd=*(int*)arg;
    while (1)
    {
        char buf[128]={0};
        recvfrom(fd, buf, sizeof(buf), 0, NULL, NULL);
        if(strcmp(buf, "#quit")==0)
        {
            printf("server has left\n");
            exit(0);
        }
        printf("You:%s\n",buf);
    }
    return NULL;
}

int main()
{
//1创建UDP套接字
    int udpfd=socket(AF_INET, SOCK_DGRAM, 0);
    if(-1==udpfd)
    {
        perror("socket");
        return 1;
    }
//2设置服务器地址
    struct sockaddr_in ser;
    bzero(&ser, sizeof(ser));
    ser.sin_family=AF_INET;
    ser.sin_port=htons(50000);
    ser.sin_addr.s_addr=inet_addr("192.168.31.86");
//3发送消息建立初始链接
    char buf[512]="start";
    sendto(udpfd, buf, strlen(buf), 0, (SA)&ser, sizeof(ser));
//4创建接收线程
    pthread_t tid1,tid2;
    pthread_create(&tid1,NULL,th1,&udpfd);
//5主线程循环发送消息到服务器
    printf("You:");
    while (1)
    {
        char buf[512];
        fgets(buf, sizeof(buf), stdin);
        sendto(udpfd, buf, sizeof(buf), 0, (SA)&ser, sizeof(ser));
        if(0==strcmp("#quit", buf))
        {
            exit(0);
        }
        printf("You:");
    }
//清理工作
    pthread_join(tid1, NULL);
    close(udpfd);
    return 0;
}

2、TCP实现

服务器端:

cpp 复制代码
#include<netinet/in.h>
#include<netinet/ip.h>
#include <pthread.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <strings.h>
#include<sys/socket.h>
#include<sys/types.h>
#include <thread_db.h>
#include<time.h>
#include<unistd.h>
typedef struct sockaddr*(SA);
//th函数是一个线程函数,负责持续接收来自客户端的消息
void *th(void *arg)
{
    int fd=*(int*)arg;
    char buf[512];
    while (1)
    {
        recv(fd, buf, sizeof(buf), 0);
        if(strcmp("#quit\n",buf)==0)
        {
            exit(0);
        }
        printf("You:%s",buf);
    }
    return NULL;
}
int main()
{
//1创建TCP监听套接字
    int listfd=socket(AF_INET, SOCK_STREAM, 0);
    if(-1==listfd)
    {
        perror("socket");
        return 1;
    }
//2设置服务器地址
    struct sockaddr_in ser,cli;
    bzero(&ser, sizeof(ser));
    bzero(&cli, sizeof(cli));
    ser.sin_family=AF_INET;
    ser.sin_port=htons(50000);
    ser.sin_addr.s_addr=INADDR_ANY;
//3绑定套接字
    int ret=bind(listfd, (SA)&ser, sizeof(ser));
    if(-1==ret)
    {
        perror("bind");
        return -1;
    }
//4开始监听
    listen(listfd, 3);//3代表同一时间可以与服务器建立连接的排队数
//5接受客户端连接
    socklen_t len=sizeof(cli);
    int conn=accept(listfd, (SA)&cli, &len);
    if(-1==conn)
    {
        perror("conn");
        return -1;
    }
//6接收初始消息建立连接
    char  buf[512];
    recv(conn, buf, strlen(buf), 0);
//7创建接收线程
    pthread_t tid;
    pthread_create(&tid, NULL, th, &conn);
//8主线程循环发送消息到客户端
    while (1)
    {
        printf("You:");
        fgets(buf, sizeof(buf), stdin);
        // buf[strlen(buf)-1]='\0';
        send(conn, buf, strlen(buf), 0);
        if(0==strcmp("#quit\n", buf))
        {
            exit(0);
        }
    }
//清理工作
    pthread_join(tid, NULL);
    close(listfd);
    close(conn);
    return 0;
}

客户端:

cpp 复制代码
#include <arpa/inet.h>
#include<netinet/in.h>
#include<netinet/ip.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <strings.h>
#include<sys/socket.h>
#include<sys/types.h>
#include <thread_db.h>
#include<time.h>
#include<unistd.h>
#include<pthread.h>
typedef struct sockaddr*(SA);
//th函数是一个线程函数,负责持续接收来自服务器的消息
void *th(void *arg)
{
    int fd=*(int*)arg;
    while (1) 
    {
        char buf[128]={0};
        recv(fd, buf, sizeof(buf), 0);
        if(strcmp("#quit\n",buf)==0)
        {
            exit(0);
        }
        printf("You:%s",buf);
    }
    return NULL;
}
int main()
{
//1创建TCP套接字
    int conn=socket(AF_INET, SOCK_STREAM, 0);
    if(-1==conn)
    {
        perror("socket");
        return 1;
    }
//2设置服务器地址
    struct sockaddr_in ser;
    bzero(&ser, sizeof(ser));
    ser.sin_family=AF_INET;
    ser.sin_port=htons(50000);
    ser.sin_addr.s_addr=inet_addr("192.168.31.86");
//3连接服务器
    int ret=connect(conn, (SA)&ser, sizeof(ser));
    if(-1==ret)
    {
        perror("connect");
        return 1;
    }
//4发送初始消息建立连接
    char buf[512]="start";
    send(conn, buf, strlen(buf), 0);
//5创建接收线程
    pthread_t tid;
    pthread_create(&tid, NULL, th, &conn);
//6主线程循环发送消息到服务器  
    while (1)
    {
        printf("You:");
        char buf[512];
        fgets(buf, sizeof(buf), stdin);
        // buf[strlen(buf)-1]='\0';
        send(conn, buf, strlen(buf), 0);
        if(0==strcmp("#quit\n", buf))
        {
            exit(0);
        }
    }
//清理工作
    pthread_join(tid, NULL);
    close(conn);
    return 0;
}
相关推荐
江团1io03 小时前
深入解析TCP核心机制:连接管理、流量与拥塞控制
服务器·网络·tcp/ip
知白守黑2673 小时前
Ansible角色
运维·服务器·ansible
EkihzniY5 小时前
OCR 证件识别:驱动澳门酒店自助入住智能化
嵌入式硬件·ocr
好家伙VCC6 小时前
数学建模模型 全网最全 数学建模常见算法汇总 含代码分析讲解
大数据·嵌入式硬件·算法·数学建模
伴杯猫6 小时前
【ESP32-IDF】基础外设开发2:系统中断矩阵
c语言·单片机·嵌入式硬件·mcu·物联网·github
搬砖的小码农_Sky6 小时前
常见的显示器接口技术
嵌入式硬件·计算机外设·显示器
茯苓gao6 小时前
STM32G4 速度环开环,电流环闭环 IF模式建模
笔记·stm32·单片机·嵌入式硬件·学习
点灯小铭8 小时前
基于STM32单片机的智能粮仓温湿度检测蓝牙手机APP设计
stm32·单片机·智能手机·毕业设计·课程设计
沐欣工作室_lvyiyi8 小时前
基于单片机的智能路灯(论文+源码)
单片机·嵌入式硬件·毕业设计
Yyq130208696828 小时前
SIT1050 5V 供电,±40V 接口耐压,1Mbps 高速 CAN 总线收发器
单片机·嵌入式硬件