zoukankan      html  css  js  c++  java
  • delphi中使用SocketStream读写数据的技巧


    { TFileServerThread }

      TFileServerThread = class(TServerClientThread)
      public
        procedure ClientExecute; override;
      end;
     
    { TFileServerThread }
    procedure TFileServerThread.ClientExecute;
    var
      Data : array[0..1023] of char;
      RecText : string;
      SocketStream : TWinSocketStream;
    begin
      While Not Terminated And ClientSocket.Connected Do
      Try
        SocketStream := TWinSocketStream.Create(ClientSocket, 30000);
        Try
          FillChar(Data, SizeOf(Data), 0);
          If SocketStream.Read(Data, SizeOf(Data)) = 0 Then
          Begin
            // If we didn't get any data after xx seconds then close the connection
            ClientSocket.SendText('Timeout on Server'+#13#10);
            //Wait a little time to allow sending of text before disconnect
            sleep(1);
            ClientSocket.Close;
            Terminate;
          End;
          RecText := Data;
          If Length(RecText) > 2 Then
            Delete(RecText, Pos(#13#10, RecText), 2); // Delete #13#10
          If ClientSocket.Connected Then
          Begin
            ClientSocket.SendText(RecText);
            SendMessage(Form1.Listbox1.Handle, LB_ADDSTRING, 0, Integer(PChar(RecText)));
            PostMessage(Form1.Handle, CM_INCCOUNT, 0, 0);
          End;
        Finally
          SocketStream.Free;
        End;
      Except
        HandleException;
      End; 
    end;
     
    procedure TForm1.ServerSocketGetThread(Sender: TObject;
      ClientSocket: TServerClientWinSocket;
      var SocketThread: TServerClientThread);
    begin
      SocketThread := TFileServerThread.Create(False, ClientSocket);
    end;

    -----------------------------------------------------------------------------------------------------------------------------

    delphi的帮助中有Writing client threads 和 Writing server threads代码示例。
    procedure TMyClientThread.Execute;
    var
    TheStream: TWinSocketStream;
    buffer: string;
    begin
    { create a TWinSocketStream for reading and writing }
    TheStream := TWinSocketStream.Create(ClientSocket1.Socket, 60000);
    try
    { fetch and process commands until the connection or thread is terminated }
    while (not Terminated) and (ClientSocket1.Active) do
    begin
    try
    GetNextRequest(buffer); { GetNextRequest must be a thread-safe method }

    { write the request to the server }
    TheStream.Write(buffer, Length(buffer) + 1);
    { continue the communication (e.g. read a response from the server) }
    ...
    except
    if not(ExceptObject is EAbort) then
    Synchronize(HandleThreadException); { you must write HandleThreadException }
    end;
    end;
    finally
    TheStream.free;
    end;
    end;

    procedure TMyServerThread.ClientExecute;
    var
    Stream : TWinSocketStream;
    Buffer : array[0 .. 9] of Char;
    begin
    { make sure connection is active }
    while (not Terminated) and ClientSocket.Connected do
    begin
    try
    Stream := TWinSocketStream.Create(ClientSocket, 60000);
    try
    FillChar(Buffer, 10, 0); { initialize the buffer }
    { give the client 60 seconds to start writing }
    if Stream.WaitForData(60000) then

    begin
    if Stream.Read(Buffer, 10) = 0 then { if can't read in 60 seconds }
    ClientSocket.Close; { close the connection }
    { now process the request }
    ...
    end
    else
    ClientSocket.Close; { if client doesn't start, close }
    finally
    Stream.Free;
    end;
    except
    HandleException;
    end;
    end;
    end;

  • 相关阅读:
    Deepin安装Python开发环境
    Deepin怎样安装C/C++编译环境更好
    当 tcpdump -w 遇到 Permission denied
    c++中的虚函数
    c++中的new 和delete
    ubuntu没有输入窗口,不能调用输入法
    Ubuntu下升级VisualBox后无法启动 Kernel driver not installed (rc=-1908)
    BCD与GRUB
    adb shell device not found解决
    unsupported number of arguments...的错误
  • 原文地址:https://www.cnblogs.com/760044827qq/p/4022418.html
Copyright © 2011-2022 走看看