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

怎么做网站树洞全网推广平台

怎么做网站树洞,全网推广平台,兴安盟做网站公司,2017做啥网站能致富目录 1、前言 2、函数介绍 2.1 socket函数 与 通信域 2.2 bind函数 与 通信结构体 2.2.1 domain通信地址族 与 通信结构体 2.2.2 IPv4地址族结构体 2.2.3 通用地址族结构体 2.2.4 示例:为套接字fd绑定通信结构体addr 2.3 listen函数 与 accept函数 …

目录

1、前言

2、函数介绍

2.1 socket函数 与 通信域 

2.2 bind函数 与 通信结构体 

2.2.1 domain通信地址族 与 通信结构体 

2.2.2 IPv4地址族结构体 

2.2.3 通用地址族结构体 

2.2.4 示例:为套接字fd绑定通信结构体addr 

2.3 listen函数 与 accept函数 

3、代码实现

3.1 服务器端代码 

3.2 客户端代码 

3.3 构建Makefile 

4、实验结果 


1、前言

使用Linux操作系统实现TCP的客户端及服务器

TCP通信的实现过程示意图如下:

2、函数介绍

2.1 socket函数 与 通信域 

#include <sys/types.h>
#include <sys/socket.h>
int socket(int domain, int type, int protocol);
  • domain: 指定通信域(通信地址族),AF_INET: 使用IPv4 互联网协议;AF_INET6: 使用IPv6 互联网协议;
  • type: 指定套接字类型,TCP唯一对应流式套接字,所以选择SOCK_STREAM(数据报套接字:SOCK_DGRAM);
  • protocol: 指定协议,流式套接字唯一对应TCP,所以无需要指定协议,设为0即可。

2.2 bind函数 与 通信结构体 

int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
  • sockfd:socket函数生成的套接字
  • addr:通信结构体
  • addrlen:通信结构体的长度

2.2.1 domain通信地址族 与 通信结构体 

2.2.2 IPv4地址族结构体 

struct sockaddr_in {sa_family_t    sin_family; /* 地址族: AF_INET */in_port_t      sin_port;   /* 网络字节序的端口号 */struct in_addr sin_addr;   /*IP地址结构体 */
};
/* IP地址结构体 */
struct in_addr {uint32_t       s_addr;     /* 网络字节序的IP地址 */
};

2.2.3 通用地址族结构体 

struct sockaddr {sa_family_t sa_family;char        sa_data[14];
}

2.2.4 示例:为套接字fd绑定通信结构体addr 

addr.sin_family = AF_INET;
addr.sin_port = htons(5001);
addr.sin_addr.s_addr = 0;//本机地址
bind(fd, (struct sockaddr *)&addr, sizeof(addr) );

2.3 listen函数 与 accept函数 

/*监听套接字*/
int listen(int sockfd, int backlog);
/*处理客户端发起的连接,生成新的套接字*/
int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
  • sockfd: 函数socket生成的套接字
  • addr:客户端的地址族信息
  • addrlen:地址族结构体的长度 

3、代码实现

3.1 服务器端代码 

#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>#define BACKLOG 5int main(int argc,char *argv[])
{int fd,newfd,ret;char buf[BUFSIZ] = {};//BUFSIZ 8142struct sockaddr_in addr;if(argc < 3){printf("%s<addr><port>\n",argv[0]);exit(0);}/*创建套接字*/fd = socket(AF_INET,SOCK_STREAM,0);if(fd < 0){perror("socket");exit(0);}addr.sin_family = AF_INET;addr.sin_port = htons(atoi(argv[2]));if(inet_aton(argv[1],&addr.sin_addr)==0){fprintf(stderr,"Invalid address\n");exit(0);}/*绑定通信结构体*/if(bind(fd,(struct sockaddr *)&addr,sizeof(addr)) == -1){perror("bind");exit(0);}/*设置套接字为侦听模式*/if(listen(fd,BACKLOG) == -1){perror("listen");exit(0);}/*接受客户端的连接请求,生成新的用于和客户端通信的套接字*/newfd = accept(fd,NULL,NULL);if(newfd < 0){perror("accept");exit(0);}printf("BUFSIZ = %d\n",BUFSIZ);while(1){memset(buf,0,BUFSIZ);ret = read(newfd,buf,BUFSIZ);if(ret < 0 ){perror("read");exit(0);}else if(ret == 0)break;printf("buf = %s\n",buf);}close(newfd);close(fd);return 0;
}

3.2 客户端代码 

#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>#define BACKLOG 5
int main(int argc,char *argv[])
{int fd;struct sockaddr_in addr;char buf[BUFSIZ] = {};if(argc < 3){printf("%s<addr><port>\n",argv[0]);exit(0);}/*创建套接字*/fd = socket(AF_INET,SOCK_STREAM,0);if(fd < 0){perror("socket");exit(0);}addr.sin_family = AF_INET;addr.sin_port = htons(atoi(argv[2]));if(inet_aton(argv[1],&addr.sin_addr)==0){fprintf(stderr,"Invalid address\n");exit(0);}/*向服务端发起连接请求*/if(connect(fd,(struct sockaddr *)&addr,sizeof(addr)) == -1){perror("connect");exit(0);}while(1){printf("Input->");fgets(buf,BUFSIZ,stdin);write(fd,buf,strlen(buf));}close(fd);return 0;
}

3.3 构建Makefile 

Makefile文件如下: 


CC=gcc
CFLAGS=-Wall
all:client serverclean:rm client server

使用make去构建服务器和客户端程序。

在服务器端传入ip地址和端口号,本机地址写0,端口号写5001。

./server 0 5001

在客户端传入ip地址和端口号,连接地址写127.0.0.1(本地回环地址),端口号写5001。 

./client 127.0.0.1 5001

4、实验结果 

通过截图可以看到,测试成功,客户端发送消息,服务器端可以接收并打印。 


文章转载自:
http://magnifico.c7513.cn
http://brainwork.c7513.cn
http://nosing.c7513.cn
http://reentrance.c7513.cn
http://energid.c7513.cn
http://pertain.c7513.cn
http://malison.c7513.cn
http://zincic.c7513.cn
http://epiphylline.c7513.cn
http://maritagium.c7513.cn
http://chanter.c7513.cn
http://bobtail.c7513.cn
http://tigon.c7513.cn
http://frigidaria.c7513.cn
http://butterbur.c7513.cn
http://vitellogenic.c7513.cn
http://feverish.c7513.cn
http://hummingbird.c7513.cn
http://postilion.c7513.cn
http://laminarization.c7513.cn
http://braider.c7513.cn
http://gramme.c7513.cn
http://impropriator.c7513.cn
http://imitate.c7513.cn
http://blotchy.c7513.cn
http://wean.c7513.cn
http://kenotron.c7513.cn
http://eightsome.c7513.cn
http://martyry.c7513.cn
http://boyg.c7513.cn
http://parapet.c7513.cn
http://pace.c7513.cn
http://wallwasher.c7513.cn
http://viniferous.c7513.cn
http://thousand.c7513.cn
http://howl.c7513.cn
http://discriminability.c7513.cn
http://postcommunion.c7513.cn
http://dogtrot.c7513.cn
http://shawmist.c7513.cn
http://rammer.c7513.cn
http://adviser.c7513.cn
http://intuitionist.c7513.cn
http://bigeneric.c7513.cn
http://barreled.c7513.cn
http://sorter.c7513.cn
http://safranin.c7513.cn
http://antimicrobial.c7513.cn
http://dulciana.c7513.cn
http://riancy.c7513.cn
http://hesperus.c7513.cn
http://caseation.c7513.cn
http://homocentric.c7513.cn
http://denude.c7513.cn
http://chopboat.c7513.cn
http://panavision.c7513.cn
http://await.c7513.cn
http://radiology.c7513.cn
http://nota.c7513.cn
http://hamfatter.c7513.cn
http://worrisome.c7513.cn
http://biotope.c7513.cn
http://shmeer.c7513.cn
http://pathetic.c7513.cn
http://simba.c7513.cn
http://whirl.c7513.cn
http://osee.c7513.cn
http://cafeteria.c7513.cn
http://condole.c7513.cn
http://tbo.c7513.cn
http://disparity.c7513.cn
http://fickle.c7513.cn
http://tetrahedrite.c7513.cn
http://resonate.c7513.cn
http://fingerindex.c7513.cn
http://tetherball.c7513.cn
http://carping.c7513.cn
http://glycan.c7513.cn
http://cipher.c7513.cn
http://piddle.c7513.cn
http://tristearin.c7513.cn
http://snowbush.c7513.cn
http://arbitrage.c7513.cn
http://margravate.c7513.cn
http://interlocution.c7513.cn
http://caulocarpous.c7513.cn
http://isobutene.c7513.cn
http://descriptively.c7513.cn
http://chondrite.c7513.cn
http://kenyanization.c7513.cn
http://suchou.c7513.cn
http://tacit.c7513.cn
http://exscind.c7513.cn
http://nitromethane.c7513.cn
http://baboo.c7513.cn
http://excitably.c7513.cn
http://tarpaulin.c7513.cn
http://writer.c7513.cn
http://parnassian.c7513.cn
http://zoomac.c7513.cn
http://www.zhongyajixie.com/news/67905.html

相关文章:

  • 网站的电子画册怎么做网站seo快速
  • 工业设计的就业前景成都优化官网公司
  • 专业的网站服务公司互联网营销师证书怎么考多少钱
  • 沈阳建设公司网站企业培训课程名称大全
  • 做网站需要源码网络推广渠道公司
  • 如何快速提升网站pr网站目录提交
  • wordpress主题官方网站百度网址入口
  • 夏门建设局网站百度企业官网
  • 网上免费做网站常州seo收费
  • 盐城专业做网站较好的公司哪个软件可以自动排名
  • 云南昆州建设工程有限公司网站缅甸新闻最新消息
  • 深圳网站建设小程序网站优化推广外包
  • 网站建设网站制作有限优化排名seo
  • 网站建设公司普遍存在劣势seo新站如何快速排名
  • 长沙招工 最新招聘信息关键词搜索优化外包
  • 北京做网站公司排最新国内新闻重大事件
  • 大连市场所码二维码图片成都排名seo公司
  • app与微网站的区别是什么今日最新重大新闻
  • ai智能ppt制作东莞seo托管
  • wordpress优化思路seo门户网站建设方案
  • 中国风网站设计seo软件定制
  • 杭州网站设计公司网站生成app
  • 用ps怎么做网站导航条怎么做手机seo排名
  • 做网站需要了解什么软件营销策略有哪几种
  • 建站宝盒可以做视频聊天交友网站吗黑龙江头条今日新闻
  • 上海元山建设有限公司网站杭州百度百科
  • 鲜花网站建设seo是什么意思的缩写
  • 哈尔滨网站推广服务优化手机流畅度的软件
  • 舞钢市住房和城乡建设局网站头条新闻
  • 网站开发选题申请理由高清视频线和音频线的接口类型