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

📄 main.cpp

📁 Server application. Unused without client application...
💻 CPP
字号:
//----------------------------------------------------------------------------
//Borland C++Builder
//Copyright (c) 1987, 1997-2002 Borland International Inc. All Rights Reserved.
//----------------------------------------------------------------------------
//---------------------------------------------------------------------------
#include <vcl.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#pragma hdrstop

#include "main.h"
//---------------------------------------------------------------------------
#pragma link "ScktComp"
#pragma resource "*.dfm"
TChatForm *ChatForm;
//---------------------------------------------------------------------------
__fastcall TChatForm::TChatForm(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TChatForm::FileListenItemClick(TObject *Sender)
{
  FileListenItem->Checked = !FileListenItem->Checked;
  if (FileListenItem->Checked)
  {
     ClientSocket->Active = false;
     ServerSocket->Active = true;
     StatusBar1->Panels->Items[0]->Text = "Listening...";
  }else
  {
     if (ServerSocket->Active)
     { 
        ServerSocket->Active = false;
     }
     StatusBar1->Panels->Items[0]->Text = "";    
  }          
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall TChatForm::FileConnectItemClick(TObject *Sender)
{
  if (ClientSocket->Active)
  { 
    ClientSocket->Active = false;
  }   
  if (InputQuery("Computer to connect to", "Address Name:", Server))
  {
     if (Server.Length() > 0)
     {
        ClientSocket->Host = Server;
        ClientSocket->Active = true;
        FileListenItem->Checked = false;  
     }
  }
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall TChatForm::Exit1Click(TObject *Sender)
{
  ServerSocket->Close();
  ClientSocket->Close();
  Close();   
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall TChatForm::FormCreate(TObject *Sender)
{
  FileListenItemClick(NULL);   
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall TChatForm::ClientSocketConnect(TObject *Sender,
      TCustomWinSocket *Socket)
{
  StatusBar1->Panels->Items[0]->Text = "Connect to: " + Socket->RemoteHost;   
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall TChatForm::Disconnect1Click(TObject *Sender)
{
  ClientSocket->Active = false;
  ServerSocket->Active = true;
  StatusBar1->Panels->Items[0]->Text = "Listening...";
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall TChatForm::ClientSocketRead(TObject *Sender,
      TCustomWinSocket *Socket)
{
  Memo2->Lines->Add(Socket->ReceiveText());  
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall TChatForm::ServerSocketClientRead(TObject *Sender,
      TCustomWinSocket *Socket)
{
  message = Socket->ReceiveText();
  Memo2->Lines->Add(message);

  FILE *OUTfile;
  OUTfile = fopen("protokol.txt", "a");
    if (!OUTfile)
       {
	StatusBar1->Panels->Items[0]->Text = "file error...";
       }
       for(int l=1; l<=message.Length(); l++)
         {
            CH_message = message[l];
            fputc(CH_message, OUTfile);
         }
       putenv(tzstr);
       tzset();

       t = time(NULL);
       area = localtime(&t);
       fputs(asctime(area), OUTfile);
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall TChatForm::ServerSocketAccept(TObject *Sender,
      TCustomWinSocket *Socket)
{
  IsServer = true;
  StatusBar1->Panels->Items[0]->Text = "Connect to: " + Socket->RemoteAddress;   
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall TChatForm::ServerSocketClientConnect(TObject *Sender,
      TCustomWinSocket *Socket)
{
  Label3->Caption = ServerSocket->Socket->ActiveConnections;
//  Memo2->Lines->Clear();
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall TChatForm::ClientSocketDisconnect(TObject *Sender,
      TCustomWinSocket *Socket)
{
  FileListenItemClick(NULL);   
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall TChatForm::ClientSocketError(TObject *Sender,
      TCustomWinSocket *Socket, TErrorEvent ErrorEvent, int &ErrorCode)
{
  Memo2->Lines->Add("Error connecting to:" + Server);   
  ErrorCode = 0;
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall TChatForm::ServerSocketClientDisconnect(TObject *Sender,
      TCustomWinSocket *Socket)
{
  Label3->Caption = ServerSocket->Socket->ActiveConnections;
  StatusBar1->Panels->Items[0]->Text = "...";
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------

⌨️ 快捷键说明

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