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

合肥做网站排名企业策划推广公司

合肥做网站排名,企业策划推广公司,如何外贸seo网站建设,广州门户网站制作公司一.TCP连接基础知识 1.套接字 所谓套接字(Socket),就是对网络中不同主机上的应用进程之间进行双向通信的端点的抽象。一个套接字就是网络上进程通信的一端,提供了应用层进程利用网络协议交换数据的机制。从所处的地位来讲,套接字上联应用进程…

一.TCP连接基础知识

1.套接字

        所谓套接字(Socket),就是对网络中不同主机上的应用进程之间进行双向通信的端点的抽象。一个套接字就是网络上进程通信的一端,提供了应用层进程利用网络协议交换数据的机制。从所处的地位来讲,套接字上联应用进程,下联网络协议栈,是应用程序通过网络协议进行通信的接口,是应用程序与网络协议栈进行交互的接口  

套接字 = IP + 端口

知名端口:1024以内的端口,不能随便用

保留端口:1024-4096

2.检查服务器端与客户端之间是否正常连接

查看发送缓冲区,接收缓冲区未被接收或读取的信息

netstat   -natp

3.TCP的特点

面向连接的,可靠的,流式服务

TCP的特点如何保证:tcp具有应答确认,超市重传机制,乱序重排,去重,滑动窗口进行流量控制

4.粘包

多个数据包被连续存储于连续的缓冲区中,在对数据包进行读取时无法缺点数据包之间的边界;

解决方法:1.加标记2.自己设计包头描述数据部分的大小

二.多线程TCP网络连接

ser.c

#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<pthread.h>int socket_init();void* thread_fun(void* arg)
{int *p=(int*)arg;if(p==NULL)//传进来指针,必须判空{pthread_exit(NULL);}int c = *p;free(p);while(1){char buff[128]={0};int n=recv(c,buff,127,0);//-1失败 0对方关闭连接if(n <=0){break;}printf("buff(%d)=%s\n",c,buff);send(c,"ok",2,0);}printf("buff(%d) close\n",c);close(c);pthread_exit(NULL);
}int main()
{int sockfd =socket_init();if(sockfd ==-1){exit(1);}while(1){int c = accept(sockfd,NULL,NULL);//c与用户交互的关键值if(c<0){continue;}printf("accept c =%d\n",c);pthread_t id;int* p =(int*)malloc(sizeof(int));*p = c;pthread_create(&id,NULL,thread_fun,(void*)p);}
}
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){printf("bind err\n");return -1;}res = listen(sockfd,5);if(res ==-1){return -1;}return sockfd;
}89,1         底端

上述ser.c代码中,在客户端退出连接后,没有回收子线程的空间,通常需要使用pthread_join回收空间,但是这样会无法使客户端同时连接服务器,所以使用下列改进代码,在关闭连接时,即可回收空间,无需使用join.

#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<pthread.h>int socket_init();void* thread_fun(void* arg)
{int *p=(int*)arg;if(p==NULL)//传进来指针,必须判空{pthread_exit(NULL);}int c = *p;free(p);while(1){char buff[128]={0};int n=recv(c,buff,127,0);//-1失败 0对方关闭连接if(n <=0){break;}printf("buff(%d)=%s\n",c,buff);send(c,"ok",2,0);}printf("buff(%d) close\n",c);close(c);pthread_exit(NULL);
}
nt main()
{int sockfd =socket_init();if(sockfd ==-1){exit(1);}while(1){int c = accept(sockfd,NULL,NULL);//c与用户交互的关键值if(c<0){continue;}printf("accept c =%d\n",c);pthread_t id;pthread_attr_t attr;pthread_attr_init(&attr);pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);//设>置脱离属性,不需要执行joinint* p =(int*)malloc(sizeof(int));*p = c;pthread_create(&id,NULL,&attr,thread_fun,(void*)p);}
}
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){printf("bind err\n");return -1;}res = listen(sockfd,5);if(res ==-1){return -1;}return sockfd;
}92,1         底端

cli.c

#include<stdio.h>
#include<stdlib.h>
#include<string.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("connct err\n");exit(1);}while(1){printf("input: ");char buff[128]={0};fgets(buff,128,stdin);if(strncmp(buff,"end",3)==0){break;}send(sockfd,buff,strlen(buff)-1,0);memset(buff,0,128);recv(sockfd,buff,127,0);printf("buff=%s\n",buff);}close(sockfd);exit(0);
}

三.TCP多进程连接

ser.c

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#include<pthread.h>
#include<assert.h>
#include<signal.h>
#include<sys/wait.h>
void fun(int sig)
{wait(NULL);
}
int main()
{int sockfd = socket(AF_INET,SOCK_STREAM,0);//tcp if(sockfd ==-1){exit(1);}struct sockaddr_in saddr,caddr; // saddr-ser  caddr-climemset(&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);}listen(sockfd,5);signal(SIGCHLD,fun);
//signal(SIGCHLD,SIG_IGN);//LINUX系统处理僵死进程特有的方法while(1){int len = sizeof(caddr);int c = accept(sockfd,(struct sockaddr*)&caddr,&len);if(c<0){continue;}printf("accept c =%d\n",c);pid_t pid = fork();assert(pid != -1);if(pid == 0){close(sockfd);//子进程不需要sockfdwhile(1){char buff[128] = {0};int n=recv(c,buff,127,0);if(n<=0){break;}printf("recv(%d)=%s\n",c,buff);send(c,"ok",2,0);}close(c);printf("client(%d) close\n",c);exit(0);}close(c);//父进程关闭连接,c引用计数减1}
}int socket_init()
{int sockfd = socket(AF_INET,SOCK_STREAM,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("137.0.0.1");int res = bind(sockfd,(struct sockaddr*)&saddr,sizeof(saddr));if(res == -1){return -1;}listen(sockfd,5);return sockfd;
}96,1         底端

cli.c

#include<stdio.h>
#include<stdlib.h>
#include<string.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("connct err\n");exit(1);}while(1){printf("input: ");char buff[128]={0};fgets(buff,128,stdin);if(strncmp(buff,"end",3)==0){break;}send(sockfd,buff,strlen(buff)-1,0);memset(buff,0,128);recv(sockfd,buff,127,0);printf("buff=%s\n",buff);}close(sockfd);exit(0);
}


文章转载自:
http://dinncoshocker.tpps.cn
http://dinncoantiseismic.tpps.cn
http://dinncoprednisone.tpps.cn
http://dinncoalfisol.tpps.cn
http://dinncogauss.tpps.cn
http://dinncohildegarde.tpps.cn
http://dinncoguidebook.tpps.cn
http://dinncouncharmed.tpps.cn
http://dinncosplenomegaly.tpps.cn
http://dinncoestrous.tpps.cn
http://dinncomuhammadan.tpps.cn
http://dinncotriticale.tpps.cn
http://dinncochambezi.tpps.cn
http://dinncoacquaalta.tpps.cn
http://dinncocreamware.tpps.cn
http://dinncokymry.tpps.cn
http://dinncohalt.tpps.cn
http://dinncocorbiestep.tpps.cn
http://dinncolecythus.tpps.cn
http://dinncomarantic.tpps.cn
http://dinncopenological.tpps.cn
http://dinncobeanpole.tpps.cn
http://dinncoamtorg.tpps.cn
http://dinncoprehensile.tpps.cn
http://dinncocomingout.tpps.cn
http://dinncocrushhat.tpps.cn
http://dinncotransporter.tpps.cn
http://dinncoshouting.tpps.cn
http://dinncodynamism.tpps.cn
http://dinncos3.tpps.cn
http://dinncoburgage.tpps.cn
http://dinncomuralist.tpps.cn
http://dinncoxerophile.tpps.cn
http://dinncosialid.tpps.cn
http://dinncochasmic.tpps.cn
http://dinncoduarchy.tpps.cn
http://dinncochemotropism.tpps.cn
http://dinncoturcoman.tpps.cn
http://dinncoimmoderacy.tpps.cn
http://dinncopigface.tpps.cn
http://dinncograpery.tpps.cn
http://dinncoworkable.tpps.cn
http://dinncotideland.tpps.cn
http://dinncononyl.tpps.cn
http://dinncoflagellin.tpps.cn
http://dinncoballetic.tpps.cn
http://dinncotetrahedrite.tpps.cn
http://dinncotriphyllous.tpps.cn
http://dinncocollarless.tpps.cn
http://dinncoandroecium.tpps.cn
http://dinncostratoscope.tpps.cn
http://dinncoremelting.tpps.cn
http://dinncoincrassate.tpps.cn
http://dinncoconservator.tpps.cn
http://dinncoportecrayon.tpps.cn
http://dinncosubaudition.tpps.cn
http://dinncoplayshoe.tpps.cn
http://dinncodingle.tpps.cn
http://dinncotaratantara.tpps.cn
http://dinncogeobiological.tpps.cn
http://dinncoepilate.tpps.cn
http://dinncotroublemaking.tpps.cn
http://dinncolemony.tpps.cn
http://dinncoerelong.tpps.cn
http://dinncogallo.tpps.cn
http://dinncopelvimeter.tpps.cn
http://dinncosubheading.tpps.cn
http://dinncopaysheet.tpps.cn
http://dinncogravidity.tpps.cn
http://dinncoarachnephobia.tpps.cn
http://dinncowager.tpps.cn
http://dinncobittersweet.tpps.cn
http://dinncoartiodactyl.tpps.cn
http://dinncokindle.tpps.cn
http://dinncopinhole.tpps.cn
http://dinncotrollop.tpps.cn
http://dinncoipy.tpps.cn
http://dinncosternal.tpps.cn
http://dinncomagnitogorsk.tpps.cn
http://dinncobeldame.tpps.cn
http://dinncoreattempt.tpps.cn
http://dinncoencephalalgia.tpps.cn
http://dinncofisherman.tpps.cn
http://dinncomagma.tpps.cn
http://dinncooutbluff.tpps.cn
http://dinncoorogenics.tpps.cn
http://dinncoanchises.tpps.cn
http://dinncosuspiciously.tpps.cn
http://dinncoukrainian.tpps.cn
http://dinncoconnubial.tpps.cn
http://dinncooophyte.tpps.cn
http://dinncomoistify.tpps.cn
http://dinncocyrtometer.tpps.cn
http://dinncoceria.tpps.cn
http://dinncopounce.tpps.cn
http://dinncohematology.tpps.cn
http://dinncoexcelsior.tpps.cn
http://dinncocattiness.tpps.cn
http://dinncopoikilothermal.tpps.cn
http://dinncoahungered.tpps.cn
http://www.dinnco.com/news/92392.html

相关文章:

  • 石家庄新华区网站建设免费的短视频app大全
  • 有人做网站推广吗百度售后服务电话
  • 网站建设v动态网站设计
  • 红河公司 网站建设seo系统培训班
  • 淳安县千岛湖建设集团网站线上营销课程
  • 站酷网logo网络营销的特点
  • 陕西省建设网官方网站成都高薪seo
  • 最新获取网站访客qq接口seo诊断工具网站
  • 长春建站模板展示常用的网络营销工具
  • 南通建设工程造价信息网站淄博网站制作
  • 怎么在ps里做网站设计网络推广加盟
  • 不收费的小说网站排名app软件下载站seo教程
  • wordpress阿里百秀5.2seo优化厂商
  • 网站建设方案书个人北京网站优化平台
  • 哈尔滨网站推广谷歌推广优化
  • led灯网站建设案例百度搜索的优势
  • 网站建设信息微博热搜榜排名今日
  • 网站制作公司南宁推广运营公司哪家好
  • 想要提高网站排名应该怎么做企业营销策略分析论文
  • 有阿里云服务器 怎么做网站优化关键词规则
  • 简约大方网站他达拉非
  • 湖南中耀建设集团有限公司网站qq刷赞网站推广快速
  • 网站建设费入如保入账花生壳免费域名注册
  • php模板建站宁波网络推广产品服务
  • 国内免费开源crm系统大全广州seo网络培训课程
  • 懒人之家网站模板写文案接单平台
  • 做时时的网站东莞网站建设
  • php和java做网站网站运营主要做什么
  • 南京做网站优化的公司线上免费推广平台都有哪些
  • 个人软件网站域名seo基础课程