Server端
UDP Socket 编程中服务器端流程与 TCP 相似, 只是不需要通过 listen() 和 accept() 监听和等待客户端请求,直接通过 sendto() 和 recvfrom() 和客户端进行读写操作。
server.cpp
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
68
69
70
71
72
73
74
75
76
// Win socket dynamic library
// Linux kernel socket calling
int main(void){
struct sockaddr_in sockaddr_server, sockaddr_client;
int sockfd;
int i;
char recv_buf[BUFLEN];
// Loading & initializing winsock
WSADATA wsa;
printf("\nInitializing Winsock...");
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0){
printf("Failed. Error Code : %d", WSAGetLastError());
exit(EXIT_FAILURE);
}
printf("Initialized.\n");
// 1. Creating socket, no-connection way, datagram
if ((sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == INVALID_SOCKET{
printf("fail to create socket...\n");
}
// Configuring server ip port, etc
memset((char *)&sockaddr_server, 0, sizeof(sockaddr_server));
sockaddr_me.sin_family = AF_INET;
sockaddr_me.sin_port = htons(PORT);
sockaddr_me.sin_addr.s_addr = htonl(INADDR_ANY);
// 2. Binding server's socket with ip and port
if (bind(sockfd, (sockaddr *)&sockaddr_server, sizeof(sockaddr_server)) == -1){
printf("bind error...\n");
}
socklen_t sockaddr_client_len = sizeof(sockaddr_client);
for (i = 0; i < NPACK; i++) {
// 3. Receiving data from client
if (recvfrom(sockfd, buf, BUFLEN, 0,
(sockaddr *)&sockaddr_client, &sockaddr_client_len) == -1){
printf("recvfrom error...\n");
}
printf("Received packet from %s:%d\nData: %s\n\n",
inet_ntoa(sockaddr_client.sin_addr), ntohs(sockaddr_client.sin_port), buf);
}
// 4. Closing server's socket after transceiving
closesocket(sockfd);
#ifdef WIN32
// Unloading win socket dynamic library
WSACleanup();
#endif
return 0;
}
Client端
UDP Socket 编程客户端流程与服务器端相比更为简单,无需进行绑定操作,直接通过服务器端的ip、端口等信息, 使用 sendto()/recvfrom() 进行数据发送和接收操作。
client.cpp
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
68
69
70
71
72
73
74
75
76
// Win socket dynamic library
// Linux kernel socket calling
// Server's ip address which is pingable
int main(void){
struct sockaddr_in sockaddr_server;
int sockfd;
char buf[BUFLEN];
WSADATA wsa;
// Loading & initializing winsock
printf("\nInitialising Winsock...");
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0){
printf("Failed. Error Code : %d", WSAGetLastError());
exit(EXIT_FAILURE);
}
printf("Initialised.\n");
// 1. Creating socket, no-connection way, datagram
if ((sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == INVALID_SOCKET){
return -1;
}
// Configuring server ip address, port, etc for client
memset((char *)&sockaddr_server, 0, sizeof(sockaddr_server));
sockaddr_server.sin_family = AF_INET;
sockaddr_server.sin_port = htons(PORT);
sockaddr_server.sin_addr.S_un.S_addr = inet_addr(SRV_IP);
sockaddr_server.sin_addr.s_addr = inet_addr(SRV_IP);
// 2. Sending/Receiving data to/from client
for (unsigned i = 0; i<NPACK; i++) {
printf("Sending packet %d\n", i);
sprintf(buf, "This is packet %d\n", i);
if (sendto(sockfd, buf, BUFLEN, 0,
(struct sockaddr *) &sockaddr_server, sizeof(sockaddr_server)) == -1)
fprintf(stderr, "sendto error\n");
}
// 3. Closing client's socket after transceiving
closesocket(sockfd);
// Unloading win socket dynamic library
WSACleanup();
return 0;
}