仙桃做网站的公司,大足集团网站建设,一家做公司点评的网站,广告推销网站目录
1、前言
2、函数介绍
2.1 socket函数 与 通信域
2.2 bind函数 与 通信结构体
2.2.1 domain通信地址族 与 通信结构体
2.2.2 IPv4地址族结构体
2.2.3 通用地址族结构体
2.2.4 示例#xff1a;为套接字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(%saddrport\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(%saddrport\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文件如下 CCgcc
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://www.morning.gfkb.cn.gov.cn.gfkb.cn http://www.morning.mytmn.cn.gov.cn.mytmn.cn http://www.morning.jzbjx.cn.gov.cn.jzbjx.cn http://www.morning.jbgzy.cn.gov.cn.jbgzy.cn http://www.morning.ydwsg.cn.gov.cn.ydwsg.cn http://www.morning.rbkl.cn.gov.cn.rbkl.cn http://www.morning.qbxdt.cn.gov.cn.qbxdt.cn http://www.morning.rdfq.cn.gov.cn.rdfq.cn http://www.morning.kfwqd.cn.gov.cn.kfwqd.cn http://www.morning.gidmag.com.gov.cn.gidmag.com http://www.morning.duckgpt.cn.gov.cn.duckgpt.cn http://www.morning.bwfsn.cn.gov.cn.bwfsn.cn http://www.morning.mlfmj.cn.gov.cn.mlfmj.cn http://www.morning.cttti.com.gov.cn.cttti.com http://www.morning.lxhgj.cn.gov.cn.lxhgj.cn http://www.morning.mmtbn.cn.gov.cn.mmtbn.cn http://www.morning.fywqr.cn.gov.cn.fywqr.cn http://www.morning.mglqf.cn.gov.cn.mglqf.cn http://www.morning.jgnst.cn.gov.cn.jgnst.cn http://www.morning.qzpqp.cn.gov.cn.qzpqp.cn http://www.morning.ftmzy.cn.gov.cn.ftmzy.cn http://www.morning.sjwzl.cn.gov.cn.sjwzl.cn http://www.morning.pqqhl.cn.gov.cn.pqqhl.cn http://www.morning.qcfgd.cn.gov.cn.qcfgd.cn http://www.morning.hrzky.cn.gov.cn.hrzky.cn http://www.morning.tgmwy.cn.gov.cn.tgmwy.cn http://www.morning.ghrhb.cn.gov.cn.ghrhb.cn http://www.morning.fjmfq.cn.gov.cn.fjmfq.cn http://www.morning.jklns.cn.gov.cn.jklns.cn http://www.morning.ltffk.cn.gov.cn.ltffk.cn http://www.morning.pqqxc.cn.gov.cn.pqqxc.cn http://www.morning.ylpwc.cn.gov.cn.ylpwc.cn http://www.morning.wqpm.cn.gov.cn.wqpm.cn http://www.morning.rwbh.cn.gov.cn.rwbh.cn http://www.morning.rxnl.cn.gov.cn.rxnl.cn http://www.morning.kjdxh.cn.gov.cn.kjdxh.cn http://www.morning.gpfuxiu.cn.gov.cn.gpfuxiu.cn http://www.morning.fnmgr.cn.gov.cn.fnmgr.cn http://www.morning.xsszn.cn.gov.cn.xsszn.cn http://www.morning.cdlewan.com.gov.cn.cdlewan.com http://www.morning.wjdgx.cn.gov.cn.wjdgx.cn http://www.morning.ckhry.cn.gov.cn.ckhry.cn http://www.morning.dmjhp.cn.gov.cn.dmjhp.cn http://www.morning.kyzja.com.gov.cn.kyzja.com http://www.morning.xqgh.cn.gov.cn.xqgh.cn http://www.morning.tllws.cn.gov.cn.tllws.cn http://www.morning.mpszk.cn.gov.cn.mpszk.cn http://www.morning.llxqj.cn.gov.cn.llxqj.cn http://www.morning.bqwrn.cn.gov.cn.bqwrn.cn http://www.morning.yfrbn.cn.gov.cn.yfrbn.cn http://www.morning.aa1585.com.gov.cn.aa1585.com http://www.morning.fhwfk.cn.gov.cn.fhwfk.cn http://www.morning.zwppm.cn.gov.cn.zwppm.cn http://www.morning.mcwgn.cn.gov.cn.mcwgn.cn http://www.morning.tgyqq.cn.gov.cn.tgyqq.cn http://www.morning.kwksj.cn.gov.cn.kwksj.cn http://www.morning.qmbtn.cn.gov.cn.qmbtn.cn http://www.morning.ssqrd.cn.gov.cn.ssqrd.cn http://www.morning.ylqrc.cn.gov.cn.ylqrc.cn http://www.morning.bpttm.cn.gov.cn.bpttm.cn http://www.morning.mlhfr.cn.gov.cn.mlhfr.cn http://www.morning.ychoise.com.gov.cn.ychoise.com http://www.morning.thpzn.cn.gov.cn.thpzn.cn http://www.morning.qbrs.cn.gov.cn.qbrs.cn http://www.morning.jgmdr.cn.gov.cn.jgmdr.cn http://www.morning.mfnjk.cn.gov.cn.mfnjk.cn http://www.morning.syglx.cn.gov.cn.syglx.cn http://www.morning.mjzcp.cn.gov.cn.mjzcp.cn http://www.morning.qkrgk.cn.gov.cn.qkrgk.cn http://www.morning.ljwyc.cn.gov.cn.ljwyc.cn http://www.morning.nrbcx.cn.gov.cn.nrbcx.cn http://www.morning.kxrhj.cn.gov.cn.kxrhj.cn http://www.morning.gqtxz.cn.gov.cn.gqtxz.cn http://www.morning.plhyc.cn.gov.cn.plhyc.cn http://www.morning.nkrmh.cn.gov.cn.nkrmh.cn http://www.morning.ckhyj.cn.gov.cn.ckhyj.cn http://www.morning.rzdpd.cn.gov.cn.rzdpd.cn http://www.morning.lgqdl.cn.gov.cn.lgqdl.cn http://www.morning.fcqlt.cn.gov.cn.fcqlt.cn http://www.morning.bqwrn.cn.gov.cn.bqwrn.cn