11.4 网络套接字.pdf
网络套接字 网络套接字 采用回路网络连接到一个网络套接字。 回路网络只包含一台计算机,通常称为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_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 #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_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, 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(“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 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); client_sockfd = accept(server_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); 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 = 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, read(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(sockfd); 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(unsigned 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_INET, 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; 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 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 嵌入式软件设计