Title: Under non-blocking Socket an example of reading data: DreamTiger Time: 2000-1-15 05:59:13 pm Sender: lBlade (blade), the letter area: Delphi
Title: Re: 竹 忙 看 看 这 s s! Save me! (Reproduced)
Sending station: BBS Shuimu Tsinghua Station (Tue Jan 11 14:22:56 2000)
[Mentioned in the masterpiece of Windlike (Snow Moon):]
: Thank you for your advice!
: I am going to the big rich. One of these ends:
: Blocking method can generate an onclientread event
: Rather than blocking, you need yourself to read. Will not produce these events
: Is this this? If so, how do you know that there is data?
: Arrival?
: Even SERVER is sure to use StthreadBlocking
Thank you
indeed so. In block mode, event processing can be performed through the Windows message mechanism.
But in the Nonblock mode, read and write operations must be handled manually.
You can take a look at the sample program below.
(A simple file server, the client sends a file name to Server, SERVER read
After the file name, send the file content on the server to the client)
Define a TFileSerVerthread thread class to overload its ClientExecute method.
A new thread is created in the GetThRead event of Serversocket.
Procedure TFORM1.SERVERSOCKETGETTHREAD (Sender: TOBJECT)
Serversocket: TSERVERCLIENTWINSOCKET;
Var Socketthread: TSERVERCLIENTTHREAD;
Begin
// Create a new thread for connection
Socketthread: = TfileServertHread.create (false, serversocket);
END;
The definition of TFileServertHread is as follows:
(You can carry out data by SocketStream.waitfordata)
TfileServerthread = Class (TSERVERCLIENTTHREAD)
public
Procedure ServeRexecute; Override;
END;
Procedure tfieserverthread.serverexecute;
VAR
Data: array [0..1023] of char;
FILENAME: STRING;
SocketStream: TwinsocketStream;
Begin
While not Terminated and Serversocket.connected do
Try
SocketStream: = TwinsocketStream.create (Serversocket, 60000);
Try
Fillchar (Data, Sizeof (Data), 0);
IF socketStream.read (data, sizeof (data)) = 0 THEN
Begin
// if We Didn't Get Any Data After 60 Seconds Ten Close The Connect
Ion
Serversocket.close;
Terminate;
END;
Filename: = DATA;
If Length> 2 THEN
Delete (FileName, POS (# 13 # 10, filename), 2); // delete # 13 # 10if fileexists (filename) and serversocket.connected the
Begin
Serversocket.sendStream (TFileStream.create (FileName, Fmopenread OR
FMShareCompat or fmsharedenynone));
// NEED TO Use SendMessage Here OtherWise S Could Change
SendMessage (Form1.ListBox1.Handle, LB_Addstring, 0, Integer (Pchar (Fi
Lename)))))))))))))
// PostMessage Here IS OK Because We Are Not Relying on Any Data
Postmessage (Form1.Handle, CM_INCCVOUNT, 0, 0);
END;
Finally
SocketStream.Free;
END;
Except
Handlexception;
END;
END;