标签:ada sas syntax clu port bsp names highlight net
实现网络广播,开启了收发两个线程,具有收、发两个功能
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
#define _WINSOCK_DEPRECATED_NO_WARNINGS #include <iostream> #include <process.h> #include <WinSock2.h>//windows API #pragma comment(lib,"ws2_32.lib") #define IP "127.0.0.1" #define SENDPORT 0 #define RECVPORT 3781 #define BROADCAST "255.255.255.255" using namespace std; struct ARGLIST{ SOCKET sock; sockaddr_in addr; }; void send_process( void *p) { ARGLIST send_process; send_process = *((ARGLIST *)p); char sendbuffer[4096]; while ( true ) { gets_s(sendbuffer); if (0 == strcmp (sendbuffer, "#" )) { _endthread(); } if (SOCKET_ERROR == sendto(send_process.sock, sendbuffer, strlen (sendbuffer), 0, (sockaddr *)&send_process.addr, sizeof (send_process.addr))) { cout << "发送失败" << GetLastError() << endl; _endthread(); } else { cout << "发送成功" << endl; } } } void recv_process( void *p) { ARGLIST recv_process; recv_process = *((ARGLIST *)p); char recvbuffer[4096]; int nLen = sizeof (recv_process.addr); while ( true ) { int n = recvfrom(recv_process.sock, recvbuffer, sizeof (recvbuffer), 0, (sockaddr *)&recv_process.addr, &nLen); if (SOCKET_ERROR == n) { cout << "接收失败" << GetLastError() << endl; _endthread(); } recvbuffer[n] = 0; cout << "发送者IP:" << inet_ntoa(recv_process.addr.sin_addr) << " 端口:" << htons(recv_process.addr.sin_port) << endl; cout << "接收:" << recvbuffer << endl; } } int GetSendList(ARGLIST &sendlist) { WSADATA wd; WSAStartup(0x0202, &wd); SOCKET sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (INVALID_SOCKET == sock) { cout << "创建套接字错误" << GetLastError() << endl; } sockaddr_in sin = { AF_INET }; //sin.sin_addr.s_addr = INADDR_BROADCAST; sin .sin_addr.S_un.S_addr = inet_addr(IP); sin .sin_port = htons(SENDPORT); if (SOCKET_ERROR == bind(sock, (sockaddr *)& sin , sizeof ( sin ))) { cout << "Send绑定套接字错误" << GetLastError() << endl; return 0; } bool bOpt = true ; //设置该套接字为广播类型 if (SOCKET_ERROR == setsockopt(sock, SOL_SOCKET, SO_BROADCAST, ( char *)&bOpt, sizeof (bOpt))) { cout << "Send设置套接字错误" << GetLastError() << endl; } sendlist.sock = sock; sockaddr_in sin_from = { AF_INET }; //sin.sin_addr.s_addr = INADDR_BROADCAST; sin_from.sin_addr.S_un.S_addr = inet_addr(BROADCAST); sin_from.sin_port = htons(RECVPORT); sendlist.addr = sin_from; return 1; } int GetRecvList(ARGLIST &recvlist) { WSADATA wd; WSAStartup(0x0202, &wd); SOCKET sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (sock == INVALID_SOCKET) { cout << "创建套接字错误" << GetLastError() << endl; } sockaddr_in sin = { AF_INET }; //sin.sin_addr.s_addr = INADDR_BROADCAST; sin .sin_addr.S_un.S_addr = inet_addr(IP); sin .sin_port = htons(RECVPORT); if (SOCKET_ERROR == bind(sock, (sockaddr *)& sin , sizeof ( sin ))) { cout << "Recv绑定套接字错误" << GetLastError() << endl; return 0; } bool bOpt = true ; //设置该套接字为广播类型 if (SOCKET_ERROR == setsockopt(sock, SOL_SOCKET, SO_BROADCAST, ( char *)&bOpt, sizeof (bOpt))) { cout << "Recv设置套接字错误" << GetLastError() << endl; } recvlist.sock = sock; sockaddr_in sin_from = { AF_INET }; sin_from.sin_addr.s_addr = INADDR_BROADCAST; //sa.sin_addr.S_un.S_addr = inet_addr(IP); sin_from.sin_port = htons(SENDPORT); recvlist.addr = sin ; return 1; } int main() { ARGLIST sendlist,recvlist; if (0 == GetSendList(sendlist)) { return 0; } if (0 == GetRecvList(recvlist)) { return 0; } char *hello = "Hello" ; if (SOCKET_ERROR == sendto(sendlist.sock, hello, strlen (hello), 0, (sockaddr *)&sendlist.addr, sizeof (sendlist.addr))) { cout << "发送失败" << GetLastError() << endl; return 0; } _beginthread(send_process, 0, &sendlist); _beginthread(recv_process, 0, &recvlist); while ( true ) { } return 0; } |
标签:ada sas syntax clu port bsp names highlight net
原文地址:http://www.cnblogs.com/amu-long/p/7467340.html