-
Notifications
You must be signed in to change notification settings - Fork 0
/
WinsockUtil.pas
174 lines (149 loc) · 3.98 KB
/
WinsockUtil.pas
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
unit WinsockUtil;
interface
uses Classes, Dialogs, SysUtils, Winsock;
var
WSData: WSAData;
function StrToAddr(const s: string): longword;
procedure QuickSendUDP(const h: string; const port: integer;
var Data; const DataLen: integer);
procedure GetLocalAddresses(const s: TStrings);
function HaveLocalAddress: boolean;
type
TQuickUDPServerThread = class(TThread)
private
FListenPort: Word;
listener: sockaddr_in;
arg: integer;
ExceptMsg: string;
procedure ShowError;
protected
Sock: TSocket;
Remote: sockaddr_in;
Req: array[1..1024] of byte;
Req_Len: integer;
procedure Execute; override;
procedure DoRequest; virtual; abstract;
public
constructor Create(const Suspended: boolean; const ListenPort: Word);
end;
implementation
type
PLongWord = ^LongWord;
function StrToAddr(const s: string): longword;
var
pHE: PHostEnt;
begin
result := inet_addr(pchar(s));
if result = longword(INADDR_NONE) then
begin
pHE := GetHostByName(pchar(s));
if pHE = nil then
result := longword(INADDR_NONE)
else
result := LongWord(PLongWord(pHE^.h_addr_list^)^);
end;
end;
function HaveLocalAddress: boolean;
var
sl: TStringList;
begin
sl := TStringList.Create;
try
GetLocalAddresses(sl);
result := (sl.Count <> 0) and ((sl.Count > 1) or (sl[0] <> '127.0.0.1'));
finally
sl.Free;
end;
end;
procedure GetLocalAddresses(const s: TStrings);
type
TaPInAddr = array[0..250] of PInAddr;
PaPInAddr = ^TaPInAddr;
var
i: integer;
HostAddr: PHostEnt;
PAdrPtr: PaPInAddr;
HostName: string;
begin
s.Clear;
setlength(HostName,255);
GetHostName(pchar(HostName),255);
setlength(HostName,strlen(pchar(HostName)));
HostAddr := GetHostByName(PChar(HostName));
if HostAddr = nil then
exit;
PAdrPtr := PAPInAddr(HostAddr^.h_addr_list);
i := 0;
while PAdrPtr^[i] <> nil do
begin
s.Add(inet_ntoa(PAdrPtr^[I]^));
Inc(I);
end;
end;
procedure QuickSendUDP(const h: string; const port: integer;
var Data; const DataLen: integer);
var
addr: sockaddr_in;
sock: TSocket;
begin
addr.sin_family := AF_INET;
addr.sin_addr.S_addr := StrToAddr(h);
addr.sin_port := htons(port);
sock := Socket(AF_INET, SOCK_DGRAM, 0);
if sock = INVALID_SOCKET then
raise exception.create('Could not create socket');
try
if sendto(sock,Data,DataLen,0,addr,sizeof(addr)) = SOCKET_ERROR then
raise exception.create('Could not send packet');
finally
closesocket(sock);
end;
end;
constructor TQuickUDPServerThread.Create(const Suspended: boolean; const ListenPort: Word);
begin
inherited Create(true);
FListenPort := ListenPort;
if not Suspended then
Resume;
end;
procedure TQuickUDPServerThread.ShowError;
begin
ShowMessage(ExceptMsg);
end;
procedure TQuickUDPServerThread.Execute;
begin
try
FreeOnTerminate := true;
sock := Socket(AF_INET, SOCK_DGRAM, 0);
if sock = INVALID_SOCKET then
raise exception.create('Could not allocate a socket: Winsock error '+inttostr(WSAGetLastError));
arg := 10000; // 10 seconds
if setsockopt(sock,SOL_SOCKET,SO_RCVTIMEO,@arg,sizeof(arg)) = SOCKET_ERROR then
raise exception.create('Could set socket timeout: Winsock error '+inttostr(WSAGetLastError));
fillchar(listener,sizeof(listener),0);
listener.sin_family := AF_INET;
listener.sin_addr.S_addr := INADDR_ANY;
listener.sin_port := htons(FListenPort);
if bind(sock,listener,sizeof(sockaddr_in)) = SOCKET_ERROR then
raise exception.create('Cannot bind to address: Winsock error '+inttostr(WSAGetLastError));
while not Terminated do
begin
arg := sizeof(sockaddr_in);
Req_Len := recvfrom(sock,Req,sizeof(Req),0,remote,arg);
if Req_Len <> SOCKET_ERROR then
DoRequest;
end;
closesocket(sock);
except
on e: Exception do
begin
ExceptMsg := e.Message;
Synchronize(ShowError);
end;
end;
end;
initialization
WSAStartup($0101,WSData);
finalization
WSACleanup;
end.