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

求一个网站网络营销的10个特点

求一个网站,网络营销的10个特点,做微信的网站叫什么名字,临沂网站建设推广思路:主线程(只有一个)建立连接,就创建子线程。子线程开始通信。 共享资源:全局数据区,堆区,内核区描述符。 线程同步不同步需要取决于线程对共享资源区的数据的操作,如果是只读就不…

思路:主线程(只有一个)建立连接,就创建子线程。子线程开始通信。
在这里插入图片描述共享资源:全局数据区,堆区,内核区描述符。
线程同步不同步需要取决于线程对共享资源区的数据的操作,如果是只读就不需要,如果是写就需要了。

多线程布置过程

(1)包含对应的头文件,特别是多线程库文件pthread.h

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
#include <pthread.h>//多线程的头文件

(2)创建监听套接字

    // 1. 创建监听的套接字  https://subingwen.cn/linux/socket/。返回的是文件描述符int lfd = socket(AF_INET, SOCK_STREAM, 0);if(lfd == -1){perror("socket");exit(0);}

(3)端口绑定

使用 struct sockaddr_in 类型的结构体来存储数据,需要注意的是大小端转换,因为网络通信使用大端序,需要htons()函数进行端口转换。端口设置完毕之后,可以设置ip地址,我们使用INADDR_ANY,表示任意绑定。

  struct sockaddr_in addr;addr.sin_family = AF_INET;//地址族协议addr.sin_port = htons(10000);   // 大端端口,要使用的是大端端口,如果是字符串的大小端转换使用其他函数// INADDR_ANY代表本机的所有IP, 假设有三个网卡就有三个IP地址// 这个宏可以代表任意一个IP地址// 这个宏一般用于本地的绑定操作addr.sin_addr.s_addr = INADDR_ANY;  // 这个宏的值为0 == 0.0.0.0int ret = bind(lfd, (struct sockaddr*)&addr, sizeof(addr));//使用强制类型转换if(ret == -1){perror("bind");exit(0);}

(4)设置监听

    // 3. 设置监听ret = listen(lfd, 128);//最多128个if(ret == -1){perror("listen");exit(0);}

(5)开始通信
因为我们要实现多线程并发,需要存储多个addr 和fd通信套接字。不是监听套接字。

struct SockInfo
{struct sockaddr_in addr;//地址信息int fd;//文件描述符           
};struct SockInfo infos[512];//最多接收256个客户端----------------------------------------
对结构体进行初始化,通信描述符设置为-1是为了判断其有没有被占有
*/// 等待并接受客户端的连接请求, 建立新的连接, 会得到一个新的文件描述符(通信的)		
int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
参数:
sockfd: 监听的文件描述符
addr: 传出参数, 里边存储了建立连接的客户端的地址信息
addrlen: 传入传出参数,用于存储addr指向的内存大小
返回值:函数调用成功,得到一个文件描述符, 用于和建立连接的这个客户端通信,调用失败返回 -1
*///初始化结构体大小int max=sizeof(infos)/sizeof(infos[0]);for(int i=0;i<max;++i){memset(&infos[i],0,max);//将结构体全部设置为0infos[i].fd=-1;//通信描述符设置为-1}

(6)初始化完毕之后需要开始进行进行通信操作


int clilen = sizeof(struct sockaddr_in);

定义一个结构体指针,如果通信套接字fd=-1的话,说明没有被占有,因为如果占用了就会返回fd套接字,失败才返回-1.
所以如果-1的话,就令这个指针指向一个info数组元素。

        struct SockInfo* pinfo;//结构体指针for(int i=0;i<max;++i){if(infos[i].fd==-1){pinfo=&infos[i];break;}}

开始进行accept(),如果成功了就开辟子线程,把参数pinfo指针指向的内存数据传递给子线程,其实就是对应IP地址,端口,通信协议等数据。

int cfd = accept(lfd, (struct sockaddr*)&pinfo->addr, &clilen);//并且是个阻塞函数pinfo->fd=cfd;if(cfd == -1){perror("accept");break;}//如果连接已经成功了我们需要创建一个子线程,来处理这个客户端连接的数据pthread_t tid;pthread_create(&tid,NULL,working,pinfo);//通过pinfo指针变量传递数据给子线程pthread_detach(tid);//避免阻塞在这里,使用线程分离

主线程结束了说明通信服务器已经关闭,close监听套接字。


close(lfd);//监听的文件描述符,通信的不需要关闭,因为子线程需要使用


(7)子线程就是对数据的读取操作了。

//子线程函数
void* working(void* arg)
{   struct SockInfo*pinfo=(struct SockInfo*)arg;// 打印客户端的地址信息char ip[24] = {0};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 buf[1024];memset(buf, 0, sizeof(buf));int len = read(pinfo->fd, buf, sizeof(buf));//阻塞函数,cfd是通信的文件描述符accept的函数返回值if(len > 0){printf("客户端say: %s\n", buf);write(pinfo->fd, buf, len);//发送数据的函数}else if(len  == 0){printf("客户端断开了连接...\n");break;}else{perror("read");break;}}close(pinfo->fd);pinfo->fd=-1;return 0;
}

全部代码,server_muti

// server_muti.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
#include <pthread.h>//多线程的头文件void* working(void*arg);
struct SockInfo
{struct sockaddr_in addr;//地址信息int fd;//文件描述符           
};struct SockInfo infos[512];int main()
{// 1. 创建监听的套接字  https://subingwen.cn/linux/socket/。返回的是文件描述符int lfd = socket(AF_INET, SOCK_STREAM, 0);if(lfd == -1){perror("socket");exit(0);}// 2. 将socket()返回值和本地的IP端口绑定到一起struct sockaddr_in addr;addr.sin_family = AF_INET;//地址族协议addr.sin_port = htons(10000);   // 大端端口,要使用的是大端端口,如果是字符串的大小端转换使用其他函数// INADDR_ANY代表本机的所有IP, 假设有三个网卡就有三个IP地址// 这个宏可以代表任意一个IP地址// 这个宏一般用于本地的绑定操作addr.sin_addr.s_addr = INADDR_ANY;  // 这个宏的值为0 == 0.0.0.0int ret = bind(lfd, (struct sockaddr*)&addr, sizeof(addr));//使用强制类型转换if(ret == -1){perror("bind");exit(0);}// 3. 设置监听ret = listen(lfd, 128);if(ret == -1){perror("listen");exit(0);}//初始化结构体大小int max=sizeof(infos)/sizeof(infos[0]);for(int i=0;i<max;++i){memset(&infos[i],0,max);//将结构体全部设置为0infos[i].fd=-1;//通信描述符设置为-1}// 4. 阻塞等待并接受客户端连接//struct sockaddr_in cliaddr;int clilen = 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(lfd, (struct sockaddr*)&pinfo->addr, &clilen);//并且是个阻塞函数pinfo->fd=cfd;if(cfd == -1){perror("accept");break;}//如果连接已经成功了我们需要创建一个子线程,来处理这个客户端连接的数据pthread_t tid;pthread_create(&tid,NULL,working,pinfo);//通过pinfo指针变量传递数据给子线程pthread_detach(tid);//避免阻塞在这里,使用线程分离}close(lfd);//监听的文件描述符,通信的不需要关闭,因为子线程需要使用return 0;
}//子线程函数
void* working(void* arg)
{   struct SockInfo*pinfo=(struct SockInfo*)arg;// 打印客户端的地址信息char ip[24] = {0};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 buf[1024];memset(buf, 0, sizeof(buf));int len = read(pinfo->fd, buf, sizeof(buf));//阻塞函数,cfd是通信的文件描述符accept的函数返回值if(len > 0){printf("客户端say: %s\n", buf);write(pinfo->fd, buf, len);//发送数据的函数}else if(len  == 0){printf("客户端断开了连接...\n");break;}else{perror("read");break;}}close(pinfo->fd);pinfo->fd=-1;return 0;
}

client.c

// 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");exit(0);}// 2. 连接服务器struct sockaddr_in addr;addr.sin_family = AF_INET;addr.sin_port = htons(10000);   // 大端端口inet_pton(AF_INET, "你的地址", &addr.sin_addr.s_addr);int ret = connect(fd, (struct sockaddr*)&addr, sizeof(addr));if(ret == -1){perror("connect");exit(0);}// 3. 和服务器端通信int number = 0;while(1){// 发送数据char buf[1024];sprintf(buf, "你好, 服务器...%d\n", number++);write(fd, buf, strlen(buf)+1);// 接收数据memset(buf, 0, sizeof(buf));int len = read(fd, buf, sizeof(buf));if(len > 0){printf("服务器say: %s\n", buf);}else if(len  == 0){printf("服务器断开了连接...\n");break;}else{perror("read");break;}sleep(1);   // 每隔1s发送一条数据}close(fd);return 0;
}

这里借鉴了爱编程的大丙博主的程序。
套接字通信
Linux多线程多进程编程


文章转载自:
http://dinncobounteous.wbqt.cn
http://dinncoverbal.wbqt.cn
http://dinncoplanktology.wbqt.cn
http://dinncoperpetuation.wbqt.cn
http://dinncoadoptee.wbqt.cn
http://dinncomink.wbqt.cn
http://dinncojrc.wbqt.cn
http://dinncoreformatory.wbqt.cn
http://dinncodetective.wbqt.cn
http://dinncocarrageenin.wbqt.cn
http://dinncoinordinate.wbqt.cn
http://dinncodephosphorize.wbqt.cn
http://dinncowhorish.wbqt.cn
http://dinncounifacial.wbqt.cn
http://dinncongaio.wbqt.cn
http://dinncohypothalami.wbqt.cn
http://dinncocounteraccusation.wbqt.cn
http://dinncomultisyllabic.wbqt.cn
http://dinncoyezo.wbqt.cn
http://dinncoencapsidate.wbqt.cn
http://dinncoimpawn.wbqt.cn
http://dinncolandscape.wbqt.cn
http://dinncofiddlededee.wbqt.cn
http://dinncononprincipled.wbqt.cn
http://dinncolip.wbqt.cn
http://dinncoarcady.wbqt.cn
http://dinncovulvae.wbqt.cn
http://dinncokata.wbqt.cn
http://dinncogoitre.wbqt.cn
http://dinncoyokelry.wbqt.cn
http://dinncohorsing.wbqt.cn
http://dinncoclamworm.wbqt.cn
http://dinncolaudability.wbqt.cn
http://dinncopiaffe.wbqt.cn
http://dinncodelomorphous.wbqt.cn
http://dinncoraciness.wbqt.cn
http://dinncosuperette.wbqt.cn
http://dinncoheritage.wbqt.cn
http://dinncometathorax.wbqt.cn
http://dinncoprophecy.wbqt.cn
http://dinncoiips.wbqt.cn
http://dinncodisconnected.wbqt.cn
http://dinnconcte.wbqt.cn
http://dinncosunstar.wbqt.cn
http://dinncotrass.wbqt.cn
http://dinncocitriculture.wbqt.cn
http://dinncokelt.wbqt.cn
http://dinncobioelectrogenesis.wbqt.cn
http://dinncohal.wbqt.cn
http://dinncofarside.wbqt.cn
http://dinncomaraschino.wbqt.cn
http://dinncomotility.wbqt.cn
http://dinncoretiform.wbqt.cn
http://dinncoreroll.wbqt.cn
http://dinncoantigropelos.wbqt.cn
http://dinncolegatine.wbqt.cn
http://dinncodelphic.wbqt.cn
http://dinncocrosswise.wbqt.cn
http://dinncoparse.wbqt.cn
http://dinncoscriptwriter.wbqt.cn
http://dinncotampax.wbqt.cn
http://dinncocomprehensivize.wbqt.cn
http://dinncoanimalise.wbqt.cn
http://dinncoelbowchair.wbqt.cn
http://dinncomachicolate.wbqt.cn
http://dinncolightkeeper.wbqt.cn
http://dinncobimetallic.wbqt.cn
http://dinncodiagnostician.wbqt.cn
http://dinncominidress.wbqt.cn
http://dinncograssplot.wbqt.cn
http://dinncobarely.wbqt.cn
http://dinncopsychosurgery.wbqt.cn
http://dinncoaerophysics.wbqt.cn
http://dinncospirograph.wbqt.cn
http://dinncoinaugurate.wbqt.cn
http://dinncotoothache.wbqt.cn
http://dinncoforebode.wbqt.cn
http://dinncoquotable.wbqt.cn
http://dinncoflowerer.wbqt.cn
http://dinncoefik.wbqt.cn
http://dinncoadventuristic.wbqt.cn
http://dinncosmalt.wbqt.cn
http://dinncoautotetraploid.wbqt.cn
http://dinncoswayless.wbqt.cn
http://dinncowfm.wbqt.cn
http://dinncoradiocontamination.wbqt.cn
http://dinncophenocopy.wbqt.cn
http://dinnconoetics.wbqt.cn
http://dinncocryptate.wbqt.cn
http://dinncopreconquest.wbqt.cn
http://dinncosolidify.wbqt.cn
http://dinncosandrock.wbqt.cn
http://dinncoveronese.wbqt.cn
http://dinncobelleek.wbqt.cn
http://dinncohalfhearted.wbqt.cn
http://dinncomutoscope.wbqt.cn
http://dinncophalange.wbqt.cn
http://dinncoemma.wbqt.cn
http://dinncopreside.wbqt.cn
http://dinncodogmatist.wbqt.cn
http://www.dinnco.com/news/156888.html

相关文章:

  • 做网站还是app如何写软文推广产品
  • 响应式网站手机端尺寸百度小说排行榜完本
  • 大鼠引物在线设计网站网站优化基本技巧
  • 如何在后台做网站分页cdq百度指数
  • 电子商务网站建设定义自动app优化官网
  • 建设工程协会网站查询系统武汉seo优化服务
  • 左权网站建设百度搜索量查询
  • 网站结构优化怎么做链爱交易平台
  • 哈尔滨网站设计公司电话网站关键字优化技巧
  • 网站维护的要求包括免费域名空间申请网址
  • 海城做网站公司网站结构有哪几种
  • 杭州的服装网站建设标题优化方法
  • 北郊网站建设公司semiconductor
  • 南阳网站建设公司盐城seo营销
  • 个人网站做捐赠发布违法吗seo大牛
  • 建筑工程网上报建流程seo公司推广宣传
  • h5做网站seo专员很难吗
  • 企业邮箱域名怎么写关键词怎么优化
  • 开拓网站建设公司优化大师官方下载
  • 网站网站建设semester怎么读
  • 重庆网站建设公司销售杭州关键词排名系统
  • 秦淮做网站价格怎样在百度上发表文章
  • 哪里做网站好北京网站建设东轩seo
  • 滨州做网站推广网站优化招商
  • 微信网站制作设计方案自动连点器
  • ps外包网站百度我的订单app
  • 简洁 手机 导航网站模板下载电商seo是指
  • 兖州网站开发电话营销
  • 免费代理服务器ip和端口深圳seo优化排名优化
  • 北京做网站供应商互联网营销渠道有哪些