非阻塞方式下Socket讀取資料的一個例子
阿新 • • 發佈:2018-12-20
發信人: 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;