diff --git a/mdl/m_io_sockets.pas b/mdl/m_io_sockets.pas index cc2cb5c..a341085 100644 --- a/mdl/m_io_sockets.pas +++ b/mdl/m_io_sockets.pas @@ -66,7 +66,7 @@ Type Function Connect (Address: String; Port: Word) : Boolean; Function ResolveAddress (Host: String) : LongInt; Procedure WaitInit (NetInterface: String; Port: Word); - Function WaitConnection : TIOSocket; + Function WaitConnection (TimeOut: LongInt) : TIOSocket; Function PeekChar (Num: Byte) : Char; Override; Function ReadChar : Char; Override; @@ -492,18 +492,39 @@ Begin If Res < 0 Then Result := -1 Else Result := Length(Str); End; +{$IFDEF UNIX} Function TIOSocket.SetBlocking (Block: Boolean): LongInt; -//Var -// Data : DWord; +Var + Flags : LongInt; Begin If FSocketHandle = -1 Then Begin Result := FSocketHandle; Exit; End; -// Data := Ord(Not Block); -// Result := ioctlSocket(FSocketHandle, FIONBIO, Data); + Flags := fpFCntl(FSocketHandle, F_GETFL); + + If Block Then + Flags := Flags OR O_NONBLOCK + Else + Flags := Flags AND NOT O_NONBLOCK; + + Result := fpFCntl(FSocketHandle, F_SETFL, Flags); End; +{$ELSE} +Function TIOSocket.SetBlocking (Block: Boolean): LongInt; +Var + Data : DWord; +Begin + If FSocketHandle = -1 Then Begin + Result := FSocketHandle; + Exit; + End; + + Data := Ord(Not Block); + Result := ioctlSocket(FSocketHandle, FIONBIO, Data); +End; +{$ENDIF} Function TIOSocket.WaitForData (TimeOut: LongInt) : LongInt; Var @@ -594,7 +615,7 @@ Begin SetBlocking(True); End; -Function TIOSocket.WaitConnection : TIOSocket; +Function TIOSocket.WaitConnection (TimeOut: LongInt) : TIOSocket; Var Sock : LongInt; Client : TIOSocket; @@ -605,7 +626,22 @@ Var Begin Result := NIL; - If fpListen(FSocketHandle, 5) = -1 Then Exit; + If TimeOut > 0 Then Begin + SetBlocking(False); + + If fpListen(FSocketHandle, 5) = -1 Then Begin + SetBlocking(True); + + Exit; + End; + + If WaitForData(TimeOut) <= 0 Then Begin + SetBlocking(True); + + Exit; + End; + End Else + If fpListen(FSocketHandle, 5) = -1 Then Exit; Temp := SizeOf(SIN); Sock := fpAccept(FSocketHandle, @SIN, @Temp); diff --git a/mdl/mdltest3.pas b/mdl/mdltest3.pas index 17df42d..a6545b2 100644 --- a/mdl/mdltest3.pas +++ b/mdl/mdltest3.pas @@ -3,20 +3,20 @@ Program Test3; {$I m_ops.pas} Uses - m_Socket_Class; + m_io_Sockets; Var - Server : TSocketClass; - Client : TSocketClass; + Server : TIOSocket; + Client : TIOSocket; Str : String; Begin - Server := TSocketClass.Create; + Server := TIOSocket.Create; - Server.WaitInit(23); + Server.WaitInit('0.0.0.0', 23); WriteLn('Waiting on port 23 for TEST4 client example...'); - Client := Server.WaitConnection; + Client := Server.WaitConnection(5000); If Client = NIL Then Begin WriteLn ('An error has occured; no client detected'); diff --git a/mdl/mdltest4.pas b/mdl/mdltest4.pas index 7466ce8..f84dced 100644 --- a/mdl/mdltest4.pas +++ b/mdl/mdltest4.pas @@ -1,21 +1,27 @@ Program Test4; Uses - m_Socket_Class; + m_io_Sockets; Var - Client : TSocketClass; + Client : TIOSocket; Str : String; Begin - Client := TSocketClass.Create; + Client := TIOSocket.Create; WriteLn ('Attempting to connect to localhost port 23 for TEST3 server test'); - If Not Client.Connect ('localhost', 23) Then Begin - WriteLn ('Connection failed'); - Client.Free; - Halt; - End; + Client.ConnectInit('localhost', 23); + Client.SetBlocking(False); + + Repeat + Until Client.Connect; + +// If Not Client.Connect ('localhost', 23) Then Begin +// WriteLn ('Connection failed'); +// Client.Free; +// Halt; +// End; Client.ReadLine(Str);