一.相关过程以及知识请详见我的另一篇博客《winsock套接字编程》,这里不再累述。
二.相关代码:
server.c:
1 /**************************************** 2 > File Name:server.c 3 > Author:xiaoxiaohui 4 > mail:[email protected] 5 > Created Time:2016年05月21日 星期六 15时05分23秒 6 ****************************************/ 7 8 #include<stdio.h> 9 #include<stdlib.h> 10 #include<sys/types.h> 11 #include<sys/socket.h> 12 #include<unistd.h> 13 #include<arpa/inet.h> 14 #include<netinet/in.h> 15 #include<string.h> 16 17 const int PORT = 9090; 18 const int LEN = 1024; 19 int serverSock; 20 struct sockaddr_in local; 21 struct sockaddr_in client; 22 23 24 int main() 25 { 26 serverSock = socket(AF_INET, SOCK_DGRAM, 0); 27 28 local.sin_family = AF_INET; 29 //local.sin_addr.s_addr = htonl(INADDR_ANY); 30 local.sin_port = htons(PORT); 31 local.sin_addr.s_addr = inet_addr("127.0.0.1"); 32 bind(serverSock, (struct sockaddr*)&local, sizeof(local)); 33 34 char buf[LEN]; 35 while(1) 36 { 37 int ret = 0; 38 socklen_t len = sizeof(client); 39 memset(buf, ‘\0‘, LEN); 40 ret = recvfrom(serverSock, buf, LEN - 1, 0, (struct sockaddr*)&client, &len); 41 42 printf("ret is %d", ret); 43 if(ret == 0) 44 { 45 printf("client is closed!\n"); 46 exit(2); 47 } 48 else if(ret < 0) 49 { 50 perror("recvfrom"); 51 continue; 52 } 53 else 54 { 55 buf[ret] = ‘\0‘; 56 printf("client[ip:%s][port:%d]# %s\n", inet_ntoa(client.sin_addr), 57 ntohs(client.sin_port), buf); 58 fflush(stdout); 59 } 60 61 if(strstr(buf, "quit") != NULL) 62 { 63 close(serverSock); 64 return 0; 65 } 66 } 67 return 0; 68 }
client.c:
1 /**************************************** 2 > File Name:client.c 3 > Author:xiaoxiaohui 4 > mail:[email protected] 5 > Created Time:2016年05月21日 星期六 15时35分32秒 6 ****************************************/ 7 8 #include<stdio.h> 9 #include<sys/types.h> 10 #include<sys/socket.h> 11 #include<unistd.h> 12 #include<arpa/inet.h> 13 #include<netinet/in.h> 14 #include<string.h> 15 16 const int PORT = 9090; 17 const char* IP = "127.0.0.1"; 18 //const char* IP = "192.168.0.145"; 19 const int LEN = 1024; 20 int clientSock; 21 struct sockaddr_in server; 22 23 int main() 24 { 25 26 clientSock = socket(AF_INET, SOCK_DGRAM, 0); 27 28 server.sin_family = AF_INET; 29 server.sin_addr.s_addr = inet_addr(IP); 30 server.sin_port = htons(PORT); 31 32 char buf[LEN]; 33 while(1) 34 { 35 memset(buf, ‘\0‘, LEN); 36 printf("please input: "); 37 fflush(stdout); 38 gets(buf); 39 40 int ret = sendto(clientSock, buf, strlen(buf), 0, (struct sockaddr*)&server, sizeof(server)); 41 if(ret <= 0) 42 { 43 perror("sendto"); 44 continue; 45 } 46 47 if(strcmp(buf, "quit") == 0) 48 { 49 close(clientSock); 50 return 0; 51 } 52 } 53 54 return 0; 55 }
Makefile:
1 .PHONY:all 2 all:server client 3 4 server:server.c 5 gcc -o [email protected] $^ -g 6 client:client.c 7 gcc -o [email protected] $^ -g 8 9 .PHONY:clean 10 clean: 11 rm -f server client
执行结果:
三.总结:
UDP套接字编程是不用建立链接的,所以服务器不用listen和accept,客户端不用connect,recvfrom和sendto中有对方的套接字信息。
UDP因为是面向链接的,所以在传输数据过程中比TCP要高效,适用于流媒体或对可靠性要求不高的应用。
时间: 2024-10-13 03:35:04