⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 concli2.dpr

📁 文件名称:新曦 我的资源 搜索软件 源程序(Borland Delphi 7)说明
💻 DPR
字号:
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

Author:       Fran鏾is PIETTE
Description:  ConCli2 shows how to use TWSocket in a console mode application.
              ConCli2 use a thread to make the socket run in the program
              background while the foreground is busy with the user interface
              (for simplicity here we just wait for the user to hit the
              enter key).
Creation:     Nov 20, 1997
Version:      1.01
EMail:        http://www.overbyte.be        http://www.rtfm.be/fpiette
              francois.piette@overbyte.be   francois.piette@rtfm.be
                                            francois.piette@pophost.eunet.be
Support:      Use the mailing list twsocket@elists.org
              Follow "support" link at http://www.overbyte.be for subscription.
Legal issues: Copyright (C) 1997-2002 by Fran鏾is PIETTE
              Rue de Grady 24, 4053 Embourg, Belgium. Fax: +32-4-365.74.56
              <francois.piette@overbyte.be>

              This software is provided 'as-is', without any express or
              implied warranty.  In no event will the author be held liable
              for any  damages arising from the use of this software.

              Permission is granted to anyone to use this software for any
              purpose, including commercial applications, and to alter it
              and redistribute it freely, subject to the following
              restrictions:

              1. The origin of this software must not be misrepresented,
                 you must not claim that you wrote the original software.
                 If you use this software in a product, an acknowledgment
                 in the product documentation would be appreciated but is
                 not required.

              2. Altered source versions must be plainly marked as such, and
                 must not be misrepresented as being the original software.

              3. This notice may not be removed or altered from any source
                 distribution.

Updates:
Dec 05, 1998 V1.01 Don't use TWait object anymore.


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
{$IFDEF VER80}
    Bomb('Sorry, Delphi 1 does not support console mode programs');
{$ENDIF}
{$APPTYPE CONSOLE}
program ConCli2;

uses
    Windows, Classes, WSocket;

const
    ServerHostName = 'localhost';
    ServerPort     = 'telnet';

type
    TWSocketThread = class(TThread)
    protected
        FWSocket : TWsocket;
        FRcvBuf  : array [0..1023] of char;
        procedure Execute; override;
        procedure   FWSocketDataAvailable(Sender : TObject; Error : Word);
        procedure   FWSocketSessionConnected(Sender : TObject; Error : Word);
        procedure   FWSocketSessionClosed(Sender : TObject; Error : Word);
    public
        constructor Create; virtual;
    end;

{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
constructor TWSocketThread.Create;
begin
    inherited Create(TRUE);
    FreeOnTerminate := TRUE;
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TWSocketThread.Execute;
begin
    // Let's the user know what we are doing
    Writeln('Connecting to server ''', ServerHostName,
            ''' on port ''', ServerPort, '''');

    // Create the TWSocket we will use to communicate with the server
    FWSocket                    := TWsocket.Create(nil);

    // Assign the event handler for the TWSocket events we care of
    FWSocket.OnDataAvailable    := FWSocketDataAvailable;
    FWSocket.OnSessionClosed    := FWSocketSessionClosed;
    FWSocket.OnSessionConnected := FWSocketSessionConnected;

    // Connect to the server
    FWSocket.Addr     := ServerHostName;
    FWSocket.Port     := ServerPort;
    FWSocket.Proto    := 'tcp';
    FWSocket.Connect;

    // Let the TWSocket component makes his work
    FWSocket.MessageLoop;

    // We are done, destroy the objects we created
    FWSocket.Destroy;
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
{ This event handler is called by the TWSocket when some data has been      }
{ received by the lower level.                                              }
procedure TWSocketThread.FWSocketDataAvailable(Sender : TObject; Error : Word);
var
    Len : Integer;
begin
    // Get the received data
    Len := FWSocket.Receive(@FRcvBuf[0], SizeOf(FRcvBuf) - 1);
    if Len <= 0 then
        Exit;

    // Add a terminating nul byte to allow display using standard I/O
    FRcvBuf[Len] := #0;
    Write(FRcvBuf);
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
{ This event handler is called by TWSocket when the connection is           }
{ established with the remote host                                          }
procedure TWSocketThread.FWSocketSessionConnected(Sender : TObject; Error : Word);
begin
    Writeln('Connected');
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
{ This event handler is called by TWSocket when the connection is broken    }
procedure TWSocketThread.FWSocketSessionClosed(Sender : TObject; Error : Word);
begin
    Writeln('Server has diconnected');
    FWSocket.Close;
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
{ This is the main program.                                                 }
var
    WSocketThread : TWSocketThread;
begin
    Writeln('Hit enter to stop the program');
    Writeln;
    // Create the socket working thread (suspended)
    WSocketThread := TWSocketThread.Create;

    // Start the thread
    WSocketThread.Resume;

    // The main thread continue here. Process user request here.
    Readln;

    // We are done, quit the program
    Writeln('Ok.');
end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -