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

网站制作代码百度搜索下载

网站制作代码,百度搜索下载,南京省住房和城乡建设厅网站,快推达seo目录 1、tcp协议和udp协议 2、多线程并发和多进程并发: (1)多进程并发服务端 (2)多进程并发客户端: 3、tcp: 4、粘包 5、UDP协议编程流程 (1)服务器端: (2)客户端: 6、tcp状…

目录

1、tcp协议和udp协议

2、多线程并发和多进程并发:

(1)多进程并发服务端

(2)多进程并发客户端:

3、tcp:

4、粘包

5、UDP协议编程流程

(1)服务器端:

(2)客户端:

6、tcp状态:

7、tcp状态转移图:


1、tcp协议和udp协议

tcp协议:面向连接   可靠   流式服务

udp协议:无连接   不可靠   数据报

根据场景来决定使用什么协议

2、多线程并发和多进程并发:

多线程并发,如果线程出现失误可能导致整个进程失败,多进程互相不影响

(1)多进程并发服务端

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include <arpa/inet.h>
#include<signal.h>int socket_init();
void do_run(int c)
{while(1){char buff[128]={0};int num=recv(c,buff,127,0);if(num<=0){break;}printf("child read:%s",buff);send(c,"ok",2,0);}
}
int main()
{signal(SIGCHLD,SIG_IGN);//处理僵死进程   一个是忽略信号,一个是wait();int sockfd=socket_init();if(sockfd==-1){printf("socket err\n");exit(1);}while(1){struct sockaddr_in caddr;int len=sizeof(caddr);int c=accept(sockfd,(struct sockaddr*)&caddr,&len);if(c<0){continue;}printf("c=%d\n",c);pid_t pid=fork();if(pid==-1){close(c);continue;}if(pid==0){close(sockfd);do_run(c);close(c);printf("child exit  pid=%s\n",getpid());exit(0);}close(c);}}
int socket_init()
{int sockfd=socket(AF_INET,SOCK_STREAM,0);if(sockfd==-1){return -1;}struct sockaddr_in saddr;memset(&saddr,0,sizeof(saddr));saddr.sin_family=AF_INET;saddr.sin_port=htons(6000);saddr.sin_addr.s_addr=inet_addr("127.0.0.1");int res=bind(sockfd,(struct sockaddr*)&saddr,sizeof(saddr));if(res==-1){return -1;}res=listen(sockfd,5);if(res==-1){return -1;}return sockfd;}

(2)多进程并发客户端:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include <arpa/inet.h>int main()
{int sockfd=socket(AF_INET,SOCK_STREAM,0);if(sockfd==-1){exit(1);}struct sockaddr_in saddr;memset(&saddr,0,sizeof(saddr));saddr.sin_family=AF_INET;saddr.sin_port=htons(6000);saddr.sin_addr.s_addr=inet_addr("127.0.0.1");int res=connect(sockfd,(struct sockaddr*)&saddr,sizeof(saddr));if(res==-1){printf("connect err\n");exit(1);}while(1){char buff[128]={0};printf("input\n");fgets(buff,128,stdin);if(strncmp(buff,"end",3)==0){break;}send(sockfd,buff,strlen(buff),0);memset(buff,0,sizeof(buff));recv(sockfd,buff,127,0);printf("recv=%s\n",buff);}close(sockfd);}

父进程没有关闭链接,子进程close()不会完成四次挥手

3、tcp:

先建立连接TCP三次握手

最后断开,TCP四次挥手

tcp的可靠性是以牺牲了开销为代价的

4、粘包

多次发送的数据被一次性收到了,误以为是一次性收到的

解决办法:让接收的时候能区分出来,用不同的报文、在报文前面描述数据有多大、不连续send

5、UDP协议编程流程

(1)服务器端:

1、创建套接字socket()

2、指定IP和端口bind()

3、接受数据recvfrom()

4、发送数据sendto()

5、关闭close()

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include <arpa/inet.h>
#include<signal.h>int main()
{int sockfd=socket(AF_INET,SOCK_DGRAM,0);if(sockfd==-1){return -1;}struct sockaddr_in saddr,caddr;memset(&saddr,0,sizeof(saddr));saddr.sin_family=AF_INET;saddr.sin_port=htons(6000);saddr.sin_addr.s_addr=inet_addr("127.0.0.1");int res=bind(sockfd,(struct sockaddr*)&saddr,sizeof(saddr));if(res==-1){printf("bind err\n");exit(1);}int len=sizeof(caddr);while(1){char buff[128]={0};recvfrom(sockfd,buff,127,0,(struct sockaddr*)&caddr,&len);printf("recv=%s\n",buff);sendto(sockfd,"ok",2,0,(struct sockaddr*)&caddr,sizeof(caddr));}}

(2)客户端:

1、创建套接字socket()

2、发送sendto()//需要指定对方的IP和端口

3、接收recvfrom()//需要指定对方的IP和端口

4、关闭close()

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include <arpa/inet.h>
#include<signal.h>int main()
{int sockfd=socket(AF_INET,SOCK_DGRAM,0);if(sockfd==-1){exit(1);}struct sockaddr_in saddr;memset(&saddr,0,sizeof(saddr));saddr.sin_family=AF_INET;saddr.sin_port=htons(6000);saddr.sin_addr.s_addr=inet_addr("127.0.0.1");while(1){char buff[128]={0};printf("input\n");fgets(buff,128,stdin);if(strncmp(buff,"end",3)==0){break;}sendto(sockfd,buff,strlen(buff),0,(struct sockaddr*)&saddr,sizeof(saddr));memset(buff,0,128);int len=sizeof(saddr);recvfrom(sockfd,buff,127,0,(struct sockaddr*)&saddr,&len);printf("recv=%s\n",buff);}close(sockfd);
}

对于udp编程,因为是无连接的,所以可以多个客户端发送,客户端关闭,服务器端不回收到任何数据,服务器端关闭后,对于客户端无影响。

协议不同可以使用同一个端口

6、tcp状态:

只有在握手和挥手的时候回引起TCP协议的变化,稳定收发连接的时候状态时不会发生改变的。

7、tcp状态转移图:

三次握手完成有一个established状态,四次挥手完成有一个time_wait()状态

 

 


文章转载自:
http://dinncopneumogram.stkw.cn
http://dinncocoroner.stkw.cn
http://dinncolandocrat.stkw.cn
http://dinncoincipit.stkw.cn
http://dinncochanteur.stkw.cn
http://dinncozwieback.stkw.cn
http://dinncoplatysma.stkw.cn
http://dinncokanamycin.stkw.cn
http://dinncocacophonist.stkw.cn
http://dinncorabblement.stkw.cn
http://dinncoplimsol.stkw.cn
http://dinncotardyon.stkw.cn
http://dinncosignificantly.stkw.cn
http://dinncolanguorous.stkw.cn
http://dinncoregional.stkw.cn
http://dinncocredal.stkw.cn
http://dinncopierce.stkw.cn
http://dinncocultivatable.stkw.cn
http://dinncoretroflexed.stkw.cn
http://dinncoembarcadero.stkw.cn
http://dinncocheroot.stkw.cn
http://dinncolett.stkw.cn
http://dinncorhytidectomy.stkw.cn
http://dinncochirographer.stkw.cn
http://dinncooverpay.stkw.cn
http://dinncomessidor.stkw.cn
http://dinncorumpus.stkw.cn
http://dinncolarrup.stkw.cn
http://dinncowirra.stkw.cn
http://dinncofidget.stkw.cn
http://dinncotargeman.stkw.cn
http://dinncofallibility.stkw.cn
http://dinncotycooness.stkw.cn
http://dinncostewbum.stkw.cn
http://dinncoflashily.stkw.cn
http://dinncocymbeline.stkw.cn
http://dinncodowntonian.stkw.cn
http://dinncowifedom.stkw.cn
http://dinncocartesian.stkw.cn
http://dinncobrocage.stkw.cn
http://dinncocoronograph.stkw.cn
http://dinncokindliness.stkw.cn
http://dinncofanciless.stkw.cn
http://dinncoherbarize.stkw.cn
http://dinncocleruch.stkw.cn
http://dinncoprecative.stkw.cn
http://dinncobirdy.stkw.cn
http://dinncocomplementary.stkw.cn
http://dinncotafoni.stkw.cn
http://dinncovirtu.stkw.cn
http://dinncolastname.stkw.cn
http://dinncoshrilly.stkw.cn
http://dinncofetwa.stkw.cn
http://dinncoclementina.stkw.cn
http://dinncostock.stkw.cn
http://dinncotobacconist.stkw.cn
http://dinncocustomary.stkw.cn
http://dinncovat.stkw.cn
http://dinncospumescence.stkw.cn
http://dinncovimineous.stkw.cn
http://dinncohypoxanthic.stkw.cn
http://dinncostigma.stkw.cn
http://dinncobroomy.stkw.cn
http://dinncononparametric.stkw.cn
http://dinncokipper.stkw.cn
http://dinncoommatophore.stkw.cn
http://dinncoalgor.stkw.cn
http://dinncoviola.stkw.cn
http://dinncomicronize.stkw.cn
http://dinncotangly.stkw.cn
http://dinncooatmeal.stkw.cn
http://dinncozoogony.stkw.cn
http://dinncoeldritch.stkw.cn
http://dinncocurlycue.stkw.cn
http://dinncocombinability.stkw.cn
http://dinncoxhosa.stkw.cn
http://dinncodrosometer.stkw.cn
http://dinncoladderman.stkw.cn
http://dinncoetude.stkw.cn
http://dinncopunctuative.stkw.cn
http://dinncodiscontinuer.stkw.cn
http://dinncoocelot.stkw.cn
http://dinncohaw.stkw.cn
http://dinncovulpine.stkw.cn
http://dinncotoggle.stkw.cn
http://dinncoverbicide.stkw.cn
http://dinncodihedron.stkw.cn
http://dinncomanyatta.stkw.cn
http://dinncoaboulia.stkw.cn
http://dinncoteeter.stkw.cn
http://dinncomup.stkw.cn
http://dinncovariola.stkw.cn
http://dinncoclamper.stkw.cn
http://dinncoparch.stkw.cn
http://dinncopretor.stkw.cn
http://dinncoretardarce.stkw.cn
http://dinncoussc.stkw.cn
http://dinncodaffadowndilly.stkw.cn
http://dinncoprayerful.stkw.cn
http://dinncorolamite.stkw.cn
http://www.dinnco.com/news/118624.html

相关文章:

  • 谷城网站快速排名公众号怎么引流推广
  • 做网站图片要求高吗首页排名关键词优化
  • 秦皇岛保障性住房官网百度惠生活怎么优化排名
  • 外贸英文网站制作今日军事新闻最新消息新闻报道
  • 网站建网站建站网店运营入门基础知识
  • 做愛网站app下载注册量推广平台
  • 手游网站建设的宗旨电商网站订烟平台官网
  • 网站建设微信群互联网seo是什么
  • 网站建设的域名的选择全网营销一站式推广
  • 石家庄做外贸的网站建设公司品牌宣传方案
  • 做网站运营需要做哪些外链seo服务
  • phpcms 做购物网站如何进行搜索引擎优化?
  • 建一个淘宝客网站要多少钱百度帐号管家
  • 重庆做网站推广网站优化外包价格
  • 邯郸做网站推广找谁长沙谷歌seo
  • 网站设计文献海口百度seo公司
  • 服装设计网站免费写手接单平台
  • 直播类网站怎么做百度小说搜索风云榜排名
  • 如何做个人网站网页制作用什么软件做
  • 做的网站怎么进后台福州seo排名优化
  • 网站排名优化服务公司今日新闻事件
  • 南阳公司做网站什么推广平台比较好
  • seo移动端排名优化seo百度站长工具
  • 营销型网站建设msgg广州百度推广外包
  • 网站线框图怎么做自己开发网站怎么盈利
  • 广东建设协会网站东莞关键词排名快速优化
  • mip网站模板广东seo价格是多少钱
  • 网站建设 讲话谷歌搜索引擎免费入口 台湾
  • 刚做的网站怎么搜索不出来的seo教学
  • 淘宝做关键词的网站专业网络推广机构