当前位置: 首页 > news >正文

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

1、UDP实现

服务器端:

#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;
}

客户端:

#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实现

服务器端:

#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;
}

客户端:

#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;
}

http://www.lryc.cn/news/598034.html

相关文章:

  • 嵌入式通信知识串讲:从同步 / 异步传输到 UART 协议 STM32F103 硬件解析
  • 换热站可视化:藏在数字里的城市温暖密码
  • 【jupyter 使用多进程方案】
  • 数据库底层索引讲解-排序和数据结构
  • 根据字符串数组的顺序重新排序 List顺序
  • 使用全局变量访问 Qt UI 组件的方法文档
  • WebRTC指纹——深度分析(中篇)
  • 5种最佳方法将iPhone语音备忘录传输到Mac
  • pycharm配conda环境
  • 阿里视频直播解决方案VS(MediaMTX + WebRTC) 流媒体解决方案
  • 基于python django的农业可视化系统,以奶牛牧场为例
  • WebRTC指纹——技术背景(上篇)
  • Apache POI 实战应用:企业级文档处理解决方案
  • 解决VSCode中“#include错误,请更新includePath“问题
  • es 和 lucene 的区别
  • 【Practical Business English Oral Scene Interpretation】入职面试No.5~7
  • 基于三维点云的智能焊缝识别系统设计与实现
  • 噪声环境下的数据驱动预测控制:提升抗测量噪声干扰能力
  • C++的虚基类?
  • Visual Studio 2010-.Net Framework 4.0项目-NPOI安装
  • 【智能协同云图库】智能协同云图库第六弹:空间模块开发
  • 2025年“创新杯”(原钉钉杯) B题 详细建模思路
  • 钉钉DingTalk完整版下载离线安装包2025
  • Webpack/Vite 终极指南:前端开发的“涡轮增压引擎“
  • 2025创新杯(钉钉杯)数学建模 AB赛题已出
  • 设置后轻松将 iPhone 转移到 iPhone
  • vue3 + vite || Vue3 + Webpack创建项目
  • 脑电分析——EEGLAB的使用与代码的解读
  • 系统配置修改安全指南
  • 硬件基础 -- PLL锁相环