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

📄 srv5.cpp

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

Author:       Fran鏾is PIETTE
Object:       Demo program to show how to use TWSocket object is a very
              simple server program. This server just wait for a client to
              connect, then send 'Hello'. When the user click on the
              disconnect button, the client is disconnected.
EMail:        francois.piette@swing.be          http://www.rtfm.be/fpiette
              francois.piette@rtfm.be           francois.piette@pophost.eunet.be
Creation:     September 27, 1997 (from Delphi version created sept 19, 1996)
Version:      2.02
Legal issues: Copyright (C) 1997-2000 by Fran鏾is PIETTE
              <francois.piette@pophost.eunet.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:
Mar 19, 1997 Use enhanced TWSocket object
Sep 06, 1997 Beautified
Apr 11, 1998 V2.02 Adapted for BCB3

---------------------------------------------------------------------------*/
#if __BORLANDC__ == 0x520     // BCB1 is BC5.20   BCB3 is BC5.30
    #define _WINSOCKAPI_      // Prevent winsock.h from being included
#endif
#include <vcl\vcl.h>
#pragma hdrstop

#include "srv5.h"
//---------------------------------------------------------------------------
#pragma link "WSocket"
#pragma resource "*.dfm"
TServerForm *ServerForm;
//---------------------------------------------------------------------------
__fastcall TServerForm::TServerForm(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TServerForm::FormShow(TObject *Sender)
{
    static BOOL FirstTime = TRUE;

    if (FirstTime) {
        FirstTime          = FALSE;            // Do it only once !
        SrvSocket->Addr    = "0.0.0.0";        // Accept any client
        SrvSocket->Listen();                   // Start listening for client
        InfoLabel->Caption = "Waiting for client";
    }
}
//---------------------------------------------------------------------------
// This event handler is called once a client has connected the server.
void __fastcall TServerForm::SrvSocketSessionAvailable(TObject *Sender,
    WORD Error)
{
    int         NewHSocket;
    TSockAddrIn PeerName;
    AnsiString  Peer;

    // We need to accept the client connection
    NewHSocket = SrvSocket->Accept();

    // And then associate this connection with our client socket
    CliSocket->Dup(NewHSocket);

    // Wants to know who is connected to display on screen
    CliSocket->GetPeerName(PeerName, sizeof(PeerName));

    // User likes to see internet address in dot notation
    Peer = inet_ntoa(PeerName.sin_addr);
    InfoLabel->Caption = "Remote " + Peer + " connected";

    // Send a welcome message to the client
    CliSocket->SendStr("Hello\r\n");

    // Enable the server user to disconect the client
    DisconnectButton->Enabled = TRUE;
}
//---------------------------------------------------------------------------
// This event handler is called once the client connection is broken.
// Either by the client or the server.
void __fastcall TServerForm::CliSocketSessionClosed(TObject *Sender, WORD Error)
{
    DisconnectButton->Enabled = FALSE;
    InfoLabel->Caption        = "Waiting for client"; // Inform the user             
}
//---------------------------------------------------------------------------
// This event handler is called once the user clicks on Ddisconnect
void __fastcall TServerForm::DisconnectButtonClick(TObject *Sender)
{
    CliSocket->Shutdown(2);                    // Shut the communication down
    CliSocket->Close();                        // Close the communication
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

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