Salut Steve Batson !
--------- WSocket.Pas part 4/5 --------------------
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TWSocket.SetPort(Port : String);
var
szPort : array [0..31] of char;
szProto : array [0..31] of char;
Pse : Pservent;
begin
FPortAssigned := TRUE;
Port := Trim(Port);
if IsDigit(Port[1]) then
sin.sin_port := htons(atoi(Port))
else begin
StrPCopy(szPort, Port);
StrPCopy(szProto, GetProto);
if szProto[0] = #0 then
Pse := WinSock.GetServByName(szPort, nil)
else
Pse := WinSock.GetServByName(szPort, szProto);
if Pse = nil then begin
FPortAssigned := FALSE;
{ raise ESocketException.Create('SetPort: Cannot convert port'); }
SocketError('SetPort: Cannot convert port');
end
else
sin.sin_port := Pse^.s_port;
end;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
function TWSocket.GetPort : String;
var
szProto : array [0..31] of char;
Pse : Pservent;
saddr : TSockAddrIn;
saddrlen : integer;
port : integer;
begin
if not FPortAssigned then
Result := 'not assigned'
else begin
if (FState = Connected) or (FState = Bound) or (FState = Listening)
the
n begin
saddrlen := sizeof(saddr);
if WinSock.GetSockName(FHSocket, TSockAddr(saddr), saddrlen) = 0
th
en begin
port := ntohs(saddr.sin_port);
Result := Format('%d',[port]);
end;
end
else begin
StrPCopy(szProto, GetProto);
pse := GetServByPort(sin.sin_port, szProto);
if Pse = nil then
Result := 'not found'
else begin
SetLength(Result, StrLen(Pse^.s_name));
StrCopy(@Result[1], Pse^.s_name);
end;
end;
end;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
function TWSocket.GetXPort: string;
var
saddr : TSockAddrIn;
saddrlen : integer;
port : integer;
begin
Result := 'error';
if (FState = Connected) or (FState = Bound) then begin
saddrlen := sizeof(saddr);
if WinSock.GetSockName(FHSocket, TSockAddr(saddr), saddrlen) = 0 then
b
egin
port := ntohs(saddr.sin_port);
Result := Format('%d',[port]);
end;
end;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TWSocket.SetAddr(InAddr : String);
var
szData : array[0..256] of char;
Phe : Phostent;
begin
FAddrAssigned := TRUE;
StrPCopy(szData, Trim(InAddr));
sin.sin_addr.s_addr := Inet_addr(szData);
if sin.sin_addr.s_addr = INADDR_NONE then begin
Phe := WinSock.GetHostByName(szData);
if Phe = nil then begin
FAddrAssigned := FALSE;
raise ESocketException.Create('SetAddr: Cannot convert host
address
');
end
else
sin.sin_addr := PInAddr(Phe^.h_addr_list^)^;
end;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
function TWSocket.GetAddr : String;
var
saddr : TSockAddrIn;
saddrlen : integer;
szAddr : PChar;
begin
if not FAddrAssigned then
Result := 'not assigned'
else begin
if (FState = Connected) or (FState = Bound) or (FState = Listening)
the
n begin
saddrlen := sizeof(saddr);
if WinSock.GetSockName(FHSocket, TSockAddr(saddr), saddrlen) = 0
th
en begin
szAddr := Inet_ntoa(saddr.sin_addr);
Result := StrPas(szAddr);
end
else
Result := 'not found';
end
else begin
szAddr := Inet_ntoa(sin.sin_addr);
Result := StrPas(szAddr);
end;
end;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
function TWSocket.GetSockName(var saddr : TSockAddrIn; var saddrlen :
Integer)
: integer;
begin
Result := WinSock.GetSockName(FHSocket, TSockAddr(saddr), saddrlen);
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
function TWSocket.GetPeerAddr: string;
var
saddr : TSockAddrIn;
saddrlen : integer;
szAddr : PChar;
begin
Result := 'error';
if FState = Connected then begin
saddrlen := sizeof(saddr);
if WinSock.GetPeerName(FHSocket, TSockAddr(saddr), saddrlen) = 0 then
b
egin
szAddr := Inet_ntoa(saddr.sin_addr);
Result := StrPas(szAddr);
end;
end;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
function TWSocket.GetPeerName(var Name : TSockAddrIn; NameLen : Integer) :
inte
ger;
begin
if FState = Connected then
Result := WinSock.GetPeerName(FHSocket, TSockAddr(Name), NameLen)
else
Result := SOCKET_ERROR;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TWSocket.Connect;
var
iStatus : integer;
li : TLinger;
optval : integer;
begin
if FState Closed then begin
raise ESocketException.Create('Connect: Socket already in use');
exit;
end;
if not FPortAssigned then begin
raise ESocketException.Create('Connect: No Port Specified');
exit;
end;
if not FAddrAssigned then begin
raise ESocketException.Create('Connect: No IP Address Specified');
exit;
end;
if not FProtoAssigned then begin
raise ESocketException.Create('Connect: No Protocol Specified');
exit;
end;
FHSocket := WinSock.Socket(FAddrFormat, SOCK_STREAM, FProto);
if FHSocket = INVALID_SOCKET then
SocketError('Connect (socket)')
else
ChangeState(Opened);
li.l_onoff := 1; { enable linger }
li.l_linger := 0; { 15 sec timeout }
iStatus := SetSockOpt(FHSocket, SOL_SOCKET, SO_LINGER, @li, SizeOf(li));
if iStatus 0 then
SocketError('setsockopt(SO_LINGER)');
optval := -1;
iStatus := SetSockOpt(FHSocket, SOL_SOCKET, SO_KEEPALIVE, @optval,
SizeOf(o
ptval));
if iStatus 0 then
SocketError('setsockopt(SO_KEEPALIVE)');
optval := -1;
iStatus := SetSockOpt(FHSocket, SOL_SOCKET, SO_REUSEADDR, @optval,
SizeOf(o
ptval));
if iStatus 0 then
SocketError('setsockopt(SO_REUSEADDR)');
iStatus := WSAASyncSelect(FHSocket, Handle, WM_ASYNCSELECT,
FD_READ or FD_WRITE or FD_CLOSE or
FD_CONNECT)
;
if iStatus 0 then
SocketError('WSAAsyncSelect');
iStatus := WinSock.Connect(FHSocket, TSockAddr(sin), sizeof(sin));
if iStatus = 0 then
ChangeState(Connecting)
else begin
iStatus := WSAGetLastError;
if iStatus = WSAEWOULDBLOCK then
ChangeState(Connecting)
else
SocketError('Connect');
end;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
function TWSocket.Listen : Integer;
var
iStatus : integer;
begin
Result := SOCKET_ERROR;
if not FPortAssigned then begin
Application.MessageBox('No Port Specified',
'WINSOCK ERROR',
mb_OKCancel + mb_DefButton1);
exit;
end;
if not FProtoAssigned then begin
Application.MessageBox('No Protocol Specified',
'WINSOCK ERROR',
mb_OKCancel + mb_DefButton1);
exit;
end;
if not FAddrAssigned then begin
Application.MessageBox('No Address Specified',
'WINSOCK ERROR',
mb_OKCancel + mb_DefButton1);
exit;
end;
FHSocket := WinSock.Socket(FAddrFormat, SOCK_STREAM, FProto);
if FHSocket = INVALID_SOCKET then begin
SocketError('socket');
exit;
end;
iStatus := Bind(FHSocket, TSockAddr(sin), sizeof(sin));
if iStatus = 0 then
ChangeState(Bound)
else begin
SocketError('Bind');
Close;
exit;
end;
iStatus := WinSock.Listen(FHSocket, 5);
if iStatus = 0 then
ChangeState(Listening)
else begin
SocketError('Listen');
Close;
exit;
end;
iStatus := WSAASyncSelect(FHSocket, Handle, WM_ASYNCSELECT,
FD_READ or FD_WRITE or
FD_ACCEPT or FD_CLOSE);
if iStatus = 0 then
Result := 0
else begin
SocketError('WSAASyncSelect');
Close;
exit;
end;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
--------------- End of WSocket.Pas part 4/5 ---------------
Amiti‚s,
{-Francois Piette-}
--- SvFido 1.32
---------------
* Origin: OverByte BBS (Embourg-Belgium) 32-41-651395 V-FAST (2:293/2202)
|