基于UDP协议的Socket编程

注意:
UDP没有建立连接的过程!
创建一个基于udp协议的套接字,使用socket函数时第二个参数不能传递SOCK_STREAM,而是传递SOCK_DGRAM
如创建一个基于IPv4地址族的UDP套接字: socket(AF_INET,SOCK_DGRAM, 0);
通常用于基于UDP协议的I/O一般使用 recvfrom 和 sendto 两个函数进行数据收发!
服务器:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
/************************************************************************* > File Name: server.c > Author:Lynn-Zhang > Mail: iynu17@yeah.net > Created Time: Wed 03 Aug 2016 01:14:30 PM CST ************************************************************************/#include<stdio.h>#include<sys/types.h>#include<sys/socket.h>#include<netinet/in.h>#include<arpa/inet.h>#include<string.h>void usage(const char* proc){ printf("Usage: %s [ip] [port]\n",proc);}int main(int argc,char* argv[]){ //要求输出配套到套接字 if(argc!=3) { usage(argv[0]); return 1; } //建立套接字描述符 int sock=socket(AF_INET,SOCK_DGRAM,0); if(sock<0) { perror("socket"); return 2; } struct sockaddr_in local; local.sin_family=AF_INET; local.sin_port=htons(atoi(argv[2])); local.sin_addr.s_addr=inet_addr(argv[1]); //将套接字与地址绑定 int ret=bind(sock,(struct sockaddr*)&local,sizeof(local)); if(ret<0) { perror("bind"); return 3; } int done=0; struct sockaddr_in peer; socklen_t len=sizeof(peer); char buf[1024]; while(!done) { memset(buf,'\0',sizeof(buf)); recvfrom(sock,buf,sizeof(buf),0,(struct sockaddr*)&peer,&len); printf("##########################\n"); printf("get a client , socket:%s:%d\n",inet_ntoa(peer.sin_addr),ntohs(peer.sin_port)); printf("client:%s,echo client!\n",buf); printf("##########################\n");printf("server:"); fflush(stdout); memset(buf,'\0',sizeof(buf)); ssize_t _s= read(0,buf,sizeof(buf)-1); buf[_s]='\0'; sendto(sock,buf,sizeof(buf),0,(struct sockaddr*)&peer,len); } return 0;} |
客户端:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
/************************************************************************* > File Name: client.c > Author:Lynn-Zhang > Mail: iynu17@yeah.net > Created Time: Wed 03 Aug 2016 03:48:21 PM CST ************************************************************************/#include<stdio.h>#include<sys/types.h>#include<sys/socket.h>#include<netinet/in.h>#include<arpa/inet.h>#include<string.h>static void usage(const char* proc){ printf("Usage:%s [remote_ip] [remote_port]\n",proc);}int main(int argc,char* argv[]){ if(argc!=3) { usage(argv[0]); return 1; } int sock=socket(AF_INET,SOCK_DGRAM,0); if(sock<0) { perror("socket"); return 2; } struct sockaddr_in remote; remote.sin_family=AF_INET; remote.sin_port=htons(atoi(argv[2])); remote.sin_addr.s_addr=inet_addr(argv[1]); int done=0; char buf[1024]; struct sockaddr_in peer; socklen_t len=sizeof(peer); while(!done) { printf("Please Enter: "); fflush(stdout); ssize_t _s=read(0,buf,sizeof(buf)-1); if(_s>0) { buf[_s-1]='\0'; sendto(sock,buf,sizeof(buf),0,(struct sockaddr*)&remote,sizeof(remote)); memset(buf,'\0',sizeof(buf)); recvfrom(sock,buf,sizeof(buf),0,(struct sockaddr*)&peer,&len); printf("server echo %s\nsocket: %s:%d\n",buf,inet_ntoa(peer.sin_addr),ntohs(peer.sin_port)); } } return 0;} |
运行结果:
服务器:
客户端:

本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-08/134307.htm
