11.4 网络套接字.pdf

上传人:奉*** 文档编号:4059926 上传时间:2021-01-13 格式:PDF 页数:17 大小:240.97KB
返回 下载 相关 举报
11.4 网络套接字.pdf_第1页
第1页 / 共17页
11.4 网络套接字.pdf_第2页
第2页 / 共17页
点击查看更多>>
资源描述

《11.4 网络套接字.pdf》由会员分享,可在线阅读,更多相关《11.4 网络套接字.pdf(17页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、网络套接字 网络套接字 采用回路网络连接到一个网络套接字。 回路网络只包含一台计算机,通常称为localhost,IP地址为127.0.0.1,可 以在/etc/hosts文件中找到。 #include #include #include #include #include #include int main() int server_sockfd, client_sockfd; int server_len, client_len; struct sockaddr_in server_address; struct sockaddr_in client_address; server_soc

2、kfd = socket(AF_INET, SOCK_STREAM, 0); server_address.sin_family = AF_INET; server_address.sin_addr.s_addr = inet_addr(“127.0.0.1”); server_address.sin_port = 9734; 例:服务器端程序 server2.c #include #include #include #include #include #include int main() int server_sockfd, client_sockfd; int server_len, c

3、lient_len; struct sockaddr_in server_address; struct sockaddr_in client_address; server_sockfd = socket(AF_INET, SOCK_STREAM, 0); server_address.sin_family = AF_INET; server_address.sin_addr.s_addr = inet_addr(“127.0.0.1”); server_address.sin_port = 9734; 例:服务器端程序 server2.c #include #include #includ

4、e #include #include #include int main() int server_sockfd, client_sockfd; int server_len, client_len; struct sockaddr_in server_address; struct sockaddr_in client_address; server_sockfd = socket(AF_INET, SOCK_STREAM, 0); server_address.sin_family = AF_INET; server_address.sin_addr.s_addr = inet_addr

5、(“127.0.0.1”); server_address.sin_port = 9734; 例:服务器端程序 server2.c server_len = sizeof(server_address); bind(server_sockfd, (struct sockaddr *) listen(server_sockfd, 5); while(1) char ch; printf(server waitingn); client_len = sizeof(client_address); client_sockfd = accept(server_sockfd, (struct socka

6、ddr*) read(client_sockfd, ch+; write(client_sockfd, close(client_sockfd); 例:服务器端程序 server2.c server_len = sizeof(server_address); bind(server_sockfd, (struct sockaddr *) listen(server_sockfd, 5); while(1) char ch; printf(server waitingn); client_len = sizeof(client_address); client_sockfd = accept(s

7、erver_sockfd, (struct sockaddr*) read(client_sockfd, ch+; write(client_sockfd, close(client_sockfd); 例:服务器端程序 server2.c server_len = sizeof(server_address); bind(server_sockfd, (struct sockaddr *) listen(server_sockfd, 5); while(1) char ch; printf(server waitingn); client_len = sizeof(client_address

8、); client_sockfd = accept(server_sockfd, (struct sockaddr *) read(client_sockfd, ch+; write(client_sockfd, close(client_sockfd); 例:服务器端程序 server2.c #include #include #include #include #include #include int main() int sockfd; int len; struct sockaddr_in address;/地址格式 int result; char ch = A; sockfd =

9、 socket(AF_INET, SOCK_STREAM, 0);/生成套接字 address.sin_family = AF_INET; address.sin_addr.s_addr = inet_addr(“127.0.0.1”);/格式转换 例:客户端程序 client2.c address.sin_port = 9734; len = sizeof(address); result = connect(sockfd, (struct sockaddr *) if(result = -1) perror(oops: client2); exit(1); write(sockfd, re

10、ad(sockfd, printf(char from server = %cn, ch); close(sockfd); exit(0); 例:客户端程序 client2.c address.sin_port = 9734; len = sizeof(address); result = connect(sockfd, (struct sockaddr *) if(result = -1) perror(oops: client2); exit(1); write(sockfd, read(sockfd, printf(char from server = %cn, ch); close(s

11、ockfd); exit(0); 例:客户端程序 client2.c 字节序问题 不同计算机的字节序不一样,例如intel机器一般采用小端方式,而 motorola一般采用大端方式,因此传输信息时会造成混乱。 网络字节序:客户和服务器在传输之前,将内部整数表示方式转换为网络 字节序可解决字节序问题。 #include unsigned long int htonl(unsigned long int hostlong); unsigned short int htons(unsigned short int hostshort); unsigned long int ntohl(unsigne

12、d long int netlong); unsigned short int ntohs(unsigned short int netshort); #include #include #include #include #include #include int main() int server_sockfd, client_sockfd; int server_len, client_len; struct sockaddr_in server_address; struct sockaddr_in client_address; server_sockfd = socket(AF_I

13、NET, SOCK_STREAM, 0); server_address.sin_family = AF_INET; server_address.sin_addr.s_addr = htonl(INADDR_ANY); server_address.sin_port = htons(9734); 服务器端改为网络字节序 例:服务器端程序 server3.c server_len = sizeof(server_address); bind(server_sockfd, (struct sockaddr *) listen(server_sockfd, 5); while(1) char ch

14、; printf(server waitingn); client_len = sizeof(client_address); client_sockfd = accept(server_sockfd, (struct sockaddr*) read(client_sockfd, ch+; write(client_sockfd, close(client_sockfd); 例:服务器端程序 server3.c #include #include #include #include #include #include int main() int sockfd; int len; struct

15、 sockaddr_in address; int result; char ch = A; sockfd = socket(AF_INET, SOCK_STREAM, 0); address.sin_family = AF_INET; 客户端 改为网络字节序 例:客户端程序 client3.c address.sin_addr.s_addr = inet_addr(“127.0.0.1”); /inet_addr的返回值已经是网络字节序 address.sin_port = htons(9734); len = sizeof(address); result = connect(sockfd, (struct sockaddr *) if(result = -1) perror(oops: client2); exit(1); write(sockfd, read(sockfd, printf(char from server = %cn, ch); close(sockfd); exit(0); 例:客户端程序 client3.c 嵌入式软件设计

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 大学资料

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁