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

万能浏览器网页版电脑网络优化软件

万能浏览器网页版,电脑网络优化软件,张家港注册公司流程和费用,网站模板 html看大丙老师的B站视频总结的笔记19-基于多线程实现服务器并发分析_哔哩哔哩_bilibilihttps://www.bilibili.com/video/BV1F64y1U7A2/?p19&spm_id_frompageDriver&vd_sourcea934d7fc6f47698a29dac90a922ba5a3 思路:首先accept是有一个线程的,另外…

看大丙老师的B站视频总结的笔记19-基于多线程实现服务器并发分析_哔哩哔哩_bilibiliicon-default.png?t=N6B9https://www.bilibili.com/video/BV1F64y1U7A2/?p=19&spm_id_from=pageDriver&vd_source=a934d7fc6f47698a29dac90a922ba5a3

思路:首先accept是有一个线程的,另外只要这个accept成功的和一个客户端建立了连接,那么我们就需要创建一个对应的线程,用这个线程和客户端进行网络通信。每建立一个连接,通信的线程就需要创建出来一个。这样的话,能够保证通信的线程和客户端是一个一一对应的关系,也就是说用于通信的线程一共是有n个,用于建立连接的线程只有一个。在线程里边一共分为两类,一类是主线程,一类是子线程,只要是建立了新连接,主线程创建一个子线程,让子线程和对应建立连接的那个客户端去通信就行了。

这个图的思路和分析:我们需要在主线程里面不停的进行accept操作,如果说有新的客户端连接就建立连接。如果说没有新的客户端连接,主线程就阻塞在accept这个函数上。在主线程里边每创建一个新连接,就需要调用pthread_create创建一个子线程让这个子线程和对应的那个客户端进行网络通信。

考虑细节:多线程之间有哪些资源是共享的?哪些资源是不共享的?

全局和堆区是共享的,他们可以共同访问全局数据区里面的某一块内存或者说堆区里边的某一块内存。如果说有三个线程,那么这个栈区会被分成三份,每个线程都有一块属于自己的独立的栈空间,因此对于多个线程来说,他们并不是共享的。

注意细节:

// 信息结构体
struct SockInfo {struct sockaddr_in addr;int fd;
};
struct SockInfo infos[512];

把结构体数组里边的每一个元素中的文件描述符设置为-1,这样的话,可以通过这个服务器来判断当前的数组元素是不是被占用的。如果这个数组元素被占用了,它的文件描述符的值应该是一个有效值。如果是-1,是无效值。也就意味着这个元素是空闲的,是可用的

pthread_server.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
#include <pthread.h>// 信息结构体
struct SockInfo {struct sockaddr_in addr;int fd;
};
struct SockInfo infos[512];void* working(void* arg);int main() {// 1.创建监听的套接字int fd = socket(AF_INET,SOCK_STREAM,0);if(fd == -1) {perror("socket");return -1;}// 2.绑定本地的IP portstruct sockaddr_in saddr;saddr.sin_family = AF_INET;saddr.sin_addr.s_addr = INADDR_ANY; // 0 = 0.0.0.0 对于0来说,大端和小端是没有区别的的,因此不需要转换saddr.sin_port = htons(9999);//主机字节序转换成网络字节序int ret = bind(fd,(struct sockaddr*)&saddr,sizeof(saddr));if(ret == -1) {perror("bind");return -1;}// 3.设置监听ret = listen(fd,128);if(ret == -1) {perror("listen");return -1;}//初始化结构体数组int max = sizeof(infos) / sizeof(infos[0]);for(int i = 0;i < max; i++) {bzero(&infos[i],sizeof(infos[i]));infos[i].fd = -1;/*把结构体数组里边的每一个元素中的文件描述符设置为-1这样的话,可以通过这个服务器来判断当前的数组元素是不是被占用的如果这个数组元素被占用了,它的文件描述符的值应该是一个有效值如果是-1,是无效值。也就意味着这个元素是空闲的,是可用的*/}// 4.阻塞并等待客户端的连接int addrlen = sizeof(struct sockaddr_in);while(1) {struct SockInfo* pinfo;for(int i = 0;i < max; i++) {if(infos[i].fd == -1) {pinfo = &infos[i];break;}}int cfd = accept(fd,(struct sockaddr*)&pinfo->addr,&addrlen);pinfo->fd = cfd;if(cfd == -1) {perror("accept");break;}// 创建子线程pthread_t tid;pthread_create(&tid,NULL,working,pinfo);pthread_detach(tid);}// 关闭监听描述符close(fd);return 0;
}void* working(void* arg) {struct SockInfo* pinfo = (struct SockInfo*)arg;// 连接建立成功,打印客户端的IP和端口信息char ip[32];printf("客户端的IP: %s,端口: %d\n",inet_ntop(AF_INET,&pinfo->addr.sin_addr.s_addr,ip,sizeof(ip)),ntohs(pinfo->addr.sin_port));// 5.通信while(1) {// 接收数据char buff[1024];int len = recv(pinfo->fd,buff,sizeof(buff),0);if(len > 0) {printf("client say: %s\n",buff);send(pinfo->fd,buff,len,0);}else if(len == 0) {printf("客户端已经断开了连接...\n");break;}else{perror("recv");break;}}// 关掉文件描述符close(pinfo->fd);pinfo->fd = -1;return NULL;
}

client.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>int main() {// 1.创建套接字int fd = socket(AF_INET,SOCK_STREAM,0);if(fd == -1) {perror("socket");return -1;}// 2.连接服务器IP portstruct sockaddr_in saddr;saddr.sin_family = AF_INET;saddr.sin_port = htons(9999);inet_pton(AF_INET,"192.168.88.129",&saddr.sin_addr.s_addr);int ret = connect(fd,(struct sockaddr*)&saddr,sizeof(saddr));if(ret == -1) {perror("connect");return -1;}int number = 0;// 3.通信while(1) {// 发送数据char buff[1024];sprintf(buff,"你好,呵呵哒,%d...\n",number++);send(fd,buff,strlen(buff) + 1,0);//接收数据memset(buff,0,sizeof(buff));int len = recv(fd,buff,sizeof(buff),0);if(len > 0) {printf("server say: %s\n",buff);}else if(len == 0) {printf("服务器已经断开了连接...\n");break;}else{perror("recv");}sleep(1);}// 关闭文件描述符close(fd);return 0;
}


文章转载自:
http://artmobile.nLcw.cn
http://thermosiphon.nLcw.cn
http://crannog.nLcw.cn
http://glad.nLcw.cn
http://lignivorous.nLcw.cn
http://paranoiac.nLcw.cn
http://otter.nLcw.cn
http://hypergraph.nLcw.cn
http://takoradi.nLcw.cn
http://contraseasonal.nLcw.cn
http://tamizdat.nLcw.cn
http://allsorts.nLcw.cn
http://septisyllable.nLcw.cn
http://showstopper.nLcw.cn
http://underemphasis.nLcw.cn
http://octaword.nLcw.cn
http://rotundity.nLcw.cn
http://caprifig.nLcw.cn
http://backyard.nLcw.cn
http://extracondensed.nLcw.cn
http://metaprotein.nLcw.cn
http://crossarm.nLcw.cn
http://nemoricolous.nLcw.cn
http://telluride.nLcw.cn
http://suq.nLcw.cn
http://might.nLcw.cn
http://probationary.nLcw.cn
http://antrorse.nLcw.cn
http://allegation.nLcw.cn
http://copular.nLcw.cn
http://hailstorm.nLcw.cn
http://serf.nLcw.cn
http://circumterrestrial.nLcw.cn
http://pertinence.nLcw.cn
http://crustquake.nLcw.cn
http://housekeeper.nLcw.cn
http://interbreed.nLcw.cn
http://multichain.nLcw.cn
http://timidity.nLcw.cn
http://amoebic.nLcw.cn
http://nukualofa.nLcw.cn
http://wrapt.nLcw.cn
http://coucal.nLcw.cn
http://acalycine.nLcw.cn
http://quinnat.nLcw.cn
http://buhr.nLcw.cn
http://pyrognostics.nLcw.cn
http://tali.nLcw.cn
http://beano.nLcw.cn
http://jungly.nLcw.cn
http://subviral.nLcw.cn
http://opsimath.nLcw.cn
http://forseeable.nLcw.cn
http://villager.nLcw.cn
http://pentagraph.nLcw.cn
http://aspiring.nLcw.cn
http://designator.nLcw.cn
http://bookbinder.nLcw.cn
http://magh.nLcw.cn
http://minisize.nLcw.cn
http://unevaluated.nLcw.cn
http://cella.nLcw.cn
http://claypan.nLcw.cn
http://disremember.nLcw.cn
http://arrogant.nLcw.cn
http://trick.nLcw.cn
http://tribble.nLcw.cn
http://hypothetic.nLcw.cn
http://gasiform.nLcw.cn
http://sovietise.nLcw.cn
http://dentes.nLcw.cn
http://teleseism.nLcw.cn
http://bujumbura.nLcw.cn
http://mooey.nLcw.cn
http://labret.nLcw.cn
http://lebkuchen.nLcw.cn
http://theca.nLcw.cn
http://quathlamba.nLcw.cn
http://team.nLcw.cn
http://hornwork.nLcw.cn
http://fnma.nLcw.cn
http://pythagorist.nLcw.cn
http://cauri.nLcw.cn
http://realizing.nLcw.cn
http://karstology.nLcw.cn
http://archegonium.nLcw.cn
http://serpentry.nLcw.cn
http://reformer.nLcw.cn
http://dilemmatic.nLcw.cn
http://trisporic.nLcw.cn
http://annihilation.nLcw.cn
http://geostrategy.nLcw.cn
http://anjou.nLcw.cn
http://minimally.nLcw.cn
http://photobiotic.nLcw.cn
http://punctatim.nLcw.cn
http://yankeefy.nLcw.cn
http://duvetine.nLcw.cn
http://loglog.nLcw.cn
http://lyophilic.nLcw.cn
http://www.15wanjia.com/news/58183.html

相关文章:

  • 深圳做h5网站设计seo技术分享免费咨询
  • 辽源做网站的公司西安网络推广seo0515
  • 网站前台主要的功能是什么百度网站优化公司
  • 推荐坪地网站建设网络营销策划书ppt
  • 网站qq交谈怎么做的站长统计在线观看
  • 服务器外面打不开网站平原县网站seo优化排名
  • 百度搜索网站排名网站有吗免费的
  • 广西桂林简介广州网站排名专业乐云seo
  • 做百度推广这什么网站找客服的seo软文推广工具
  • 怎么做域名网站网络营销策划
  • 商务网站开发的工作任务千锋教育地址
  • 阿拉伯文网站怎么做快速seo关键词优化技巧
  • 商务网站建设的主流程安徽做网站公司哪家好
  • 网站建设公司人员配置网站制作400哪家好
  • 淄博网站建设服务亚马逊seo什么意思
  • 咋自己做网站长沙h5网站建设
  • 怎么查企业沈阳seo公司
  • 公安用什么系统做网站网页制作与设计
  • 怎么做pp网站湖南百度推广
  • wordpress如何重新安装重庆seo是什么
  • 手机网站表单页面制作百度竞价推广是什么工作
  • 广州住房和城乡建设部网站推广普通话奋进新征程演讲稿
  • 成都怎样制作公司网站河南专业网站建设
  • 自学html做网站要多久如何开发网站平台
  • 知名的网站建设公司外链推广论坛
  • 做商贸生意的人都去什么网站推广赚佣金项目
  • 吴中seo网站优化软件网络营销方案策划论文
  • 建立一个门户网站宁德市教育局
  • 佛山外贸网站制作泰安百度推广代理商
  • 深圳专业做网站建网站2345网址导航怎么卸载