在C语言中就学过scanf和printf,其实理解socket就跟这两个输入输出差不多,只不过是信息的传输而已。
1.TCP服务器端(server)的默认函数调用顺序:
按照上述的调用顺序,我们可以来写个最简单的"hello world" server程序。
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 #include<unistd.h> 5 #include<arpa/inet.h> 6 #include<sys/socket.h> 7 8 void error_handling(char* message); 9 10 void error_handling(char* message) { 11 fputs(message, stderr); 12 fputc(‘\n‘, stderr); 13 exit(1); 14 } 15 16 int main(int argc, char **argv) 17 { 18 int serv_sock; 19 int clnt_sock; 20 21 //设置一个server地址serv_addr,client地址clnt_addr 22 struct sockaddr_in serv_addr; 23 struct sockaddr_in clnt_addr; 24 socklen_t clnt_addr_size = sizeof(clnt_addr); 25 26 memset(&serv_addr, 0, sizeof(serv_addr)); 27 memset(&serv_addr, 0, sizeof(serv_addr)); 28 char message[] = "Hello world"; 29 30 if (argc != 2) { 31 printf("Usage : %s <port>\n", argv[0]); 32 exit(1); 33 } 34 35 //创建用于internet的流协议(TCP)socket 36 serv_sock = socket(PF_INET, SOCK_STREAM, 0); 37 if (serv_sock == -1) { 38 error_handling("socket() error"); 39 } 40 41 serv_addr.sin_family = AF_INET; 42 serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); 43 serv_addr.sin_port = htons(atoi(argv[1])); 44 45 //把socket和socket地址结构联系起来 46 if( bind(serv_sock,(struct sockaddr*)&serv_addr,sizeof(serv_addr)) == -1) { 47 error_handling("bind() error"); 48 } 49 50 //server_socket用于监听 51 if ( listen(serv_sock, 5) == -1) { 52 error_handling("lisen() error"); 53 } 54 55 // 受理连接请求 56 clnt_sock = accept(serv_sock,(struct sockaddr*)&clnt_addr,&clnt_addr_size); 57 if ( clnt_sock == -1) { 58 error_handling("accept() error"); 59 } 60 61 write(clnt_sock, message, sizeof(message)); 62 close(clnt_sock); 63 close(serv_sock); 64 65 return 0; 66 }
2.与server端相对应的是客户端,其调用顺序为:
相对应的“hello world”client端程序就为:
1 hello_client.c 2 ------ 3 #include<stdio.h> 4 #include<stdlib.h> 5 #include<string.h> 6 #include<unistd.h> 7 #include<arpa/inet.h> 8 #include<sys/socket.h> 9 10 void error_handling(char* message); 11 12 void error_handling(char* message) { 13 fputs(message, stderr); 14 fputc(‘\n‘, stderr); 15 exit(1); 16 } 17 18 int main(int argc, char **argv) { 19 int sock; 20 struct sockaddr_in serv_addr; 21 char message[30]; 22 int str_len; 23 memset(&serv_addr, 0, sizeof(serv_addr)); 24 25 if (argc != 3) 26 { 27 printf("Usage: /%s <IP> <Port>\n",argv[0]); 28 exit(1); 29 } 30 31 //创建用于internet的流协议(TCP)socket 32 sock = socket(PF_INET, SOCK_STREAM, 0); 33 if (sock == -1) { 34 error_handling("socket() error"); 35 } 36 37 //设置一个socket地址结构client_addr,代表客户机internet地址, 端口 38 serv_addr.sin_family = AF_INET; 39 serv_addr.sin_addr.s_addr = inet_addr(argv[1]); 40 serv_addr.sin_port = htons(atoi(argv[2])); 41 42 //把socket和socket地址结构联系起来 43 if( connect(sock,(struct sockaddr*)&serv_addr,sizeof(serv_addr)) == -1) { 44 error_handling("connect() error"); 45 } 46 47 str_len = read(sock, message, sizeof(message) - 1); 48 if (str_len == -1) { 49 error_handling("read() error"); 50 } 51 52 printf("Message from server : %s \n", message); 53 close(sock); 54 return 0; 55 }
开始运行服务器端:
#gcc hello_server.c -o hserver #./hserver 9010 ......开始挂起
开始运行客户端:
#gcc hello_client.c -o hclient #./hclient 127.0.0.1 9010 Message from server: Hello world
时间: 2024-11-07 23:53:12