瞎谈Socket编程(三) Win/Linux下的UDP编程

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
#ifdef WIN32
#define _CRT_SECURE_NO_WARNINGS
#define socklen_t int
// Win socket dynamic library
#include <windows.h>
#include <Winsock.h>
#pragma comment(lib, "Wsock32.lib")
#else
#define _snprintf snprintf
#define INVALID_SOCKET (-1)
// Linux kernel socket calling
#define closesocket close
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#endif // WIN32

#include <stdio.h>
#include <string.h> // for memset

#define BUFLEN 512
#define NPACK 10
#define PORT 2368

int main(void){
struct sockaddr_in sockaddr_server, sockaddr_client;
int sockfd;
int i;
char recv_buf[BUFLEN];

#ifdef WIN32
// Loading & initializing winsock
WSADATA wsa;
printf("\nInitializing Winsock...");
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0){
printf("Failed. Error Code : %d", WSAGetLastError());
exit(EXIT_FAILURE);
}
#endif
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
#ifdef WIN32
#define _CRT_SECURE_NO_WARNINGS
#define socklen_t int
// Win socket dynamic library
#include <windows.h>
#include <Winsock.h>
#pragma comment(lib, "Wsock32.lib")
#else
#define _snprintf snprintf
#define INVALID_SOCKET (-1)
// Linux kernel socket calling
#define closesocket close
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#endif // WIN32

#include <stdio.h>
#include <string.h> // for memset

#define BUFLEN 512
#define NPACK 10
#define PORT 2368
// Server's ip address which is pingable
#define SRV_IP "127.0.0.1"

int main(void){
struct sockaddr_in sockaddr_server;
int sockfd;
char buf[BUFLEN];

#ifdef WIN32
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");
#endif
// 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);
#ifdef WIN32
sockaddr_server.sin_addr.S_un.S_addr = inet_addr(SRV_IP);
#else
sockaddr_server.sin_addr.s_addr = inet_addr(SRV_IP);
#endif

// 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);

#ifdef WIN32
// Unloading win socket dynamic library
WSACleanup();
#endif

return 0;
}

文章目录
  1. 1. Server端
  2. 2. Client端