标题:非阻塞方式下Socket读取数据的一个例子 | |
作者:DreamTiger | 发表时间:2000-1-15 下午 05:59:13 |
发信人: lBlade (刀锋), 信区: Delphi 标 题: Re: 斑竹帮忙看看偶这段Socket程序!救救我! (转载)发信站: BBS 水木清华站 (Tue Jan 11 14:22:56 2000)【 在 windlike (风花雪月) 的大作中提到: 】: 多谢指教!: 我去大富翁看了。其中有这么一端:: 阻塞方式可以产生OnClientRead(Write)事件: 而非阻塞方式,需要自己去读。不会产生这些事件: 请问,是这样的吗?如果是,那么如何知道有数据: 到达呢?: 偶的Server肯定要用stThreadBlocking: 谢谢的确如此。在Block方式下,可以通过Windows消息机制进行事件处理,但在NonBlock方式下,读写操作都必须自己手动处理。可以看看下面的示例程序。(一个简单的文件服务器,客户端发送文件名到Server上,Server读取文件名后把Server上的该文件内容发送到客户端)定义一个TFileServerThread线程类,重载其ClientExecute方法。在ServerSocket的GetThread事件中创建出一个新线程进行处理。procedure TForm1.ServerSocketGetThread(Sender: TObject; ServerSocket: TServerClientWinSocket; var SocketThread: TServerClientThread);begin // Create a new thread for connection SocketThread := TFileServerThread.Create(False, ServerSocket);end;有关TFileServerThread的定义和ServerExecute过程实现如下:(可以通过SocketStream.WaitForData(时间长度)进行数据等待) TFileServerThread = class(TServerClientThread) public procedure ServerExecute; override; end;procedure TFileServerThread.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 then close the connection ServerSocket.Close; Terminate; end; FileName := Data; if Length(FileName) > 2 then Delete(FileName, Pos(#13#10, FileName), 2); // Delete #13#10 if FileExists(FileName) and ServerSocket.Connected then 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(FileName))); // PostMessage here is OK because we are not relying on any data PostMessage(Form1.Handle, CM_INCCOUNT, 0, 0); end; finally SocketStream.Free; end; except HandleException; end;end; |