一个最最简单的UDP通信.
一开始把exe发给人家死活运行不了, 提示说程序没配置好. 百度之后了解到跟VS有关.
解决方法是:
把项目属性里的"MFC的使用"这一项改为[在静态库中使用MFC]. 之后再编译就行了.
注意到如此编译出的exe会大许多.
server.cpp
1 #include <iostream>
2 #include <winsock2.h>
3
4 #define BUF_SIZE 64
5
6 SOCKET s;
7
8 int CreateSocket()
9 {
10 WSADATA wsd;
11
12 if (WSAStartup(MAKEWORD(2, 2), &wsd) != 0)
13 {
14 printf("WSAStartup() failed.\n");
15 return 1;
16 }
17
18 s = socket(AF_INET, SOCK_DGRAM, 0);
19 if (s == INVALID_SOCKET)
20 {
21 printf("socket() failed.\n");
22 WSACleanup();
23 return 1;
24 }
25
26 printf("CreateSocket() succeeded.\n");
27 return 1;
28 }
29
30
31 int BindSocket()
32 {
33 SOCKADDR_IN servAddr;
34
35 servAddr.sin_family = AF_INET;
36 servAddr.sin_port = htons((short)5000);
37 servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
38
39 if (bind(s, (SOCKADDR *)&servAddr, sizeof(servAddr)) == SOCKET_ERROR)
40 {
41 printf("bind() failed.\n");
42 closesocket(s);
43 WSACleanup();
44 return 1;
45 }
46
47 printf("BindSocket() succeeded.\n");
48 return 1;
49 }
50
51
52 int ReceiveAndDisplay()
53 {
54 char buf[BUF_SIZE];
55
56 SOCKADDR_IN clientAddr;
57 int nClientLen = sizeof(clientAddr);
58 ZeroMemory(buf, BUF_SIZE);
59 if (recvfrom(s, buf, BUF_SIZE, 0, (SOCKADDR *)&clientAddr, &nClientLen) == SOCKET_ERROR)
60 {
61 printf("recvfrom() failed.\n");
62 closesocket(s);
63 WSACleanup();
64 return 1;
65 }
66
67 printf("%s\n", buf);
68 printf("ReceiveAndDisplay() succeeded.\n");
69 return 1;
70 }
71
72
73 int Exit()
74 {
75 closesocket(s);
76 WSACleanup();
77 return 1;
78 }
79
80
81 int main()
82 {
83 CreateSocket();
84 BindSocket();
85 ReceiveAndDisplay();
86 Exit();
87 return 0;
88 }
2 #include <winsock2.h>
3
4 #define BUF_SIZE 64
5
6 SOCKET s;
7
8 int CreateSocket()
9 {
10 WSADATA wsd;
11
12 if (WSAStartup(MAKEWORD(2, 2), &wsd) != 0)
13 {
14 printf("WSAStartup() failed.\n");
15 return 1;
16 }
17
18 s = socket(AF_INET, SOCK_DGRAM, 0);
19 if (s == INVALID_SOCKET)
20 {
21 printf("socket() failed.\n");
22 WSACleanup();
23 return 1;
24 }
25
26 printf("CreateSocket() succeeded.\n");
27 return 1;
28 }
29
30
31 int BindSocket()
32 {
33 SOCKADDR_IN servAddr;
34
35 servAddr.sin_family = AF_INET;
36 servAddr.sin_port = htons((short)5000);
37 servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
38
39 if (bind(s, (SOCKADDR *)&servAddr, sizeof(servAddr)) == SOCKET_ERROR)
40 {
41 printf("bind() failed.\n");
42 closesocket(s);
43 WSACleanup();
44 return 1;
45 }
46
47 printf("BindSocket() succeeded.\n");
48 return 1;
49 }
50
51
52 int ReceiveAndDisplay()
53 {
54 char buf[BUF_SIZE];
55
56 SOCKADDR_IN clientAddr;
57 int nClientLen = sizeof(clientAddr);
58 ZeroMemory(buf, BUF_SIZE);
59 if (recvfrom(s, buf, BUF_SIZE, 0, (SOCKADDR *)&clientAddr, &nClientLen) == SOCKET_ERROR)
60 {
61 printf("recvfrom() failed.\n");
62 closesocket(s);
63 WSACleanup();
64 return 1;
65 }
66
67 printf("%s\n", buf);
68 printf("ReceiveAndDisplay() succeeded.\n");
69 return 1;
70 }
71
72
73 int Exit()
74 {
75 closesocket(s);
76 WSACleanup();
77 return 1;
78 }
79
80
81 int main()
82 {
83 CreateSocket();
84 BindSocket();
85 ReceiveAndDisplay();
86 Exit();
87 return 0;
88 }
client.cpp
1 #include <iostream>
2 #include <winsock2.h>
3 using namespace std;
4
5 #define BUF_SIZE 64
6
7 SOCKET s;
8
9 int CreateSocket()
10 {
11 WSADATA wsd;
12
13 if (WSAStartup(MAKEWORD(2, 2), &wsd) != 0)
14 {
15 printf("WSAStartup() failed.\n");
16 return 1;
17 }
18
19 s = socket(AF_INET, SOCK_DGRAM, 0);
20 if (s == INVALID_SOCKET)
21 {
22 printf("socket() failed.\n");
23 WSACleanup();
24 return 1;
25 }
26
27 printf("CreateSocket() succeeded.\n");
28 return 1;
29 }
30
31
32 int Send()
33 {
34 char buf[BUF_SIZE];
35 ZeroMemory(buf, BUF_SIZE);
36
37 cout << "Enter your word: ";
38 cin >> buf;
39
40 SOCKADDR_IN servAddr;
41
42 servAddr.sin_family = AF_INET;
43 servAddr.sin_addr.s_addr = inet_addr("192.168.1.104");
44 servAddr.sin_port = htons((short)5000);
45
46 int nServAddlen = sizeof(servAddr);
47
48 if (sendto(s, buf, BUF_SIZE, 0, (SOCKADDR *)&servAddr, nServAddlen) == SOCKET_ERROR)
49 {
50 printf("sendto() failed.\n");
51 closesocket(s);
52 WSACleanup();
53 return 1;
54 }
55
56 printf("Send() succeeded.\n");
57 return 1;
58 }
59
60
61 int Exit()
62 {
63 closesocket(s);
64 WSACleanup();
65 return 1;
66 }
67
68
69 int main()
70 {
71 CreateSocket();
72 Send();
73 Exit();
74 return 0;
75 }
2 #include <winsock2.h>
3 using namespace std;
4
5 #define BUF_SIZE 64
6
7 SOCKET s;
8
9 int CreateSocket()
10 {
11 WSADATA wsd;
12
13 if (WSAStartup(MAKEWORD(2, 2), &wsd) != 0)
14 {
15 printf("WSAStartup() failed.\n");
16 return 1;
17 }
18
19 s = socket(AF_INET, SOCK_DGRAM, 0);
20 if (s == INVALID_SOCKET)
21 {
22 printf("socket() failed.\n");
23 WSACleanup();
24 return 1;
25 }
26
27 printf("CreateSocket() succeeded.\n");
28 return 1;
29 }
30
31
32 int Send()
33 {
34 char buf[BUF_SIZE];
35 ZeroMemory(buf, BUF_SIZE);
36
37 cout << "Enter your word: ";
38 cin >> buf;
39
40 SOCKADDR_IN servAddr;
41
42 servAddr.sin_family = AF_INET;
43 servAddr.sin_addr.s_addr = inet_addr("192.168.1.104");
44 servAddr.sin_port = htons((short)5000);
45
46 int nServAddlen = sizeof(servAddr);
47
48 if (sendto(s, buf, BUF_SIZE, 0, (SOCKADDR *)&servAddr, nServAddlen) == SOCKET_ERROR)
49 {
50 printf("sendto() failed.\n");
51 closesocket(s);
52 WSACleanup();
53 return 1;
54 }
55
56 printf("Send() succeeded.\n");
57 return 1;
58 }
59
60
61 int Exit()
62 {
63 closesocket(s);
64 WSACleanup();
65 return 1;
66 }
67
68
69 int main()
70 {
71 CreateSocket();
72 Send();
73 Exit();
74 return 0;
75 }