《11.4 网络套接字.pdf》由会员分享,可在线阅读,更多相关《11.4 网络套接字.pdf(17页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
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 嵌入式软件设计
限制150内