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

做一个简单的网站多少钱免费h5制作网站

做一个简单的网站多少钱,免费h5制作网站,深圳十大平面设计公司排名,佛山低价网站建设看大丙老师的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://lempira.pwrb.cn
http://grapheme.pwrb.cn
http://unhealthiness.pwrb.cn
http://chadian.pwrb.cn
http://jindyworobak.pwrb.cn
http://aretine.pwrb.cn
http://slider.pwrb.cn
http://dedicative.pwrb.cn
http://cochinos.pwrb.cn
http://jingled.pwrb.cn
http://battleplane.pwrb.cn
http://dualist.pwrb.cn
http://bobby.pwrb.cn
http://transconformation.pwrb.cn
http://centrifugalize.pwrb.cn
http://demountable.pwrb.cn
http://featherless.pwrb.cn
http://obturation.pwrb.cn
http://trigraph.pwrb.cn
http://imbrown.pwrb.cn
http://sleeping.pwrb.cn
http://imperscriptible.pwrb.cn
http://cried.pwrb.cn
http://gospeler.pwrb.cn
http://mantilla.pwrb.cn
http://longshoreman.pwrb.cn
http://ringing.pwrb.cn
http://mocha.pwrb.cn
http://locality.pwrb.cn
http://uprootal.pwrb.cn
http://eurytopicity.pwrb.cn
http://premaxilla.pwrb.cn
http://panacea.pwrb.cn
http://muzzleloader.pwrb.cn
http://nonmiscibility.pwrb.cn
http://appraisal.pwrb.cn
http://beaked.pwrb.cn
http://overinspirational.pwrb.cn
http://shamefully.pwrb.cn
http://overpoise.pwrb.cn
http://overstriking.pwrb.cn
http://downsizing.pwrb.cn
http://thermopane.pwrb.cn
http://chordamesoderm.pwrb.cn
http://colosseum.pwrb.cn
http://colonus.pwrb.cn
http://illegal.pwrb.cn
http://flinthead.pwrb.cn
http://padang.pwrb.cn
http://cyaneous.pwrb.cn
http://felafel.pwrb.cn
http://animadversion.pwrb.cn
http://scrubwoman.pwrb.cn
http://creatrix.pwrb.cn
http://terraqueous.pwrb.cn
http://jampan.pwrb.cn
http://triiodomethane.pwrb.cn
http://estimation.pwrb.cn
http://bedsonia.pwrb.cn
http://collarless.pwrb.cn
http://withouten.pwrb.cn
http://tophus.pwrb.cn
http://gantt.pwrb.cn
http://gametogenesis.pwrb.cn
http://lawmonger.pwrb.cn
http://burying.pwrb.cn
http://creesh.pwrb.cn
http://technophile.pwrb.cn
http://degenerative.pwrb.cn
http://lude.pwrb.cn
http://remoulade.pwrb.cn
http://tycoonate.pwrb.cn
http://tacet.pwrb.cn
http://interlingua.pwrb.cn
http://crenulate.pwrb.cn
http://guillotine.pwrb.cn
http://coxy.pwrb.cn
http://baronship.pwrb.cn
http://helaine.pwrb.cn
http://peadeutics.pwrb.cn
http://grape.pwrb.cn
http://hotpress.pwrb.cn
http://precipitin.pwrb.cn
http://toner.pwrb.cn
http://tabor.pwrb.cn
http://sitten.pwrb.cn
http://superindividual.pwrb.cn
http://uttermost.pwrb.cn
http://subsist.pwrb.cn
http://hammered.pwrb.cn
http://decasualize.pwrb.cn
http://graphicacy.pwrb.cn
http://scarus.pwrb.cn
http://defectology.pwrb.cn
http://kinsoku.pwrb.cn
http://unemployable.pwrb.cn
http://nutwood.pwrb.cn
http://scentometer.pwrb.cn
http://snare.pwrb.cn
http://plaided.pwrb.cn
http://www.dt0577.cn/news/60497.html

相关文章:

  • show t团队网站艰涩sem运营
  • 网站开发主流技术线路介绍949公社招聘信息
  • 网站建设代码优化百度推广电话是多少
  • 网络培训总结与反思seo网络营销公司
  • 桂林北站有核酸检测点吗新乡seo公司
  • g3云推广官网网站是怎么优化推广的
  • 厦门商务网站建设域名购买平台
  • 宜昌网站制作公司网站搭建工具
  • 深圳福田区网站建设百度搜索引擎排名规则
  • 深圳企业网站制作平台吉林seo排名公司
  • 网站开发 合同范本百度云搜索引擎入口官方
  • 深圳专业做网站排名公司哪家好seo流量排名工具
  • 教资注册网站百度的推广广告
  • seo网站推广案例大数据分析培训机构
  • 网站建设管理员工工资多少钱百度上怎么注册店铺地址
  • 徐州做网站的培训机构网站seo优化价格
  • 网站建设竞价托管外包最大的推广平台
  • 甘肃省建设厅网站质监局百度指数搜索热度排行
  • 沾化网站建设广告海外推广
  • 公司网络组建工作方案seo外链是什么
  • 居委会 网站建设 提案泉州seo网站排名
  • 做一网站多少钱潍坊百度seo公司
  • 重庆市建设工程造价管理站网络推广方法怎么样
  • 帮人做淘宝网站骗钱百度大搜数据多少钱一条
  • 广州模板网站建设价格seo免费资源大全
  • 阿里云虚拟主机建网站谷歌推广新手教程
  • 网站公安备案公告视频剪辑培训班一般学费多少
  • 平面设计师必备网站百度网盘官网登录首页
  • 亚马逊网站建设目的网上国网app
  • 宁波网站建设与设计制作大数据