📄 chat.cpp
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include <stdlib.h>
#include "chat.h"
#include "setngfrm.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TTalkForm *TalkForm;
//---------------------------------------------------------------------------
__fastcall TTalkForm::TTalkForm(TComponent* Owner)
: TForm(Owner)
{
ClientConnected = false;
WaitingForServerConnection = false;
OutSocket = NULL;
Application->OnIdle = ValidateControls;
}
//---------------------------------------------------------------------------
void __fastcall TTalkForm::ValidateControls(TObject *Sender, bool &Done){
MenuListen->Enabled = !WaitingForServerConnection;
MenuConnect->Enabled = !ClientConnected;
MenuDisconnect->Enabled = ClientConnected || WaitingForServerConnection;
FileSettings->Enabled = !ClientConnected && !WaitingForServerConnection;
Memo1->ReadOnly = !OutSocket;
}
//---------------------------------------------------------------------------
void __fastcall TTalkForm::MenuConnectClick(TObject *Sender)
{
ClientSocket1->Host = "localhost";
EnableClient();
}
//---------------------------------------------------------------------------
void __fastcall TTalkForm::MenuDisconnectClick(TObject *Sender)
{
if (ClientConnected){
DisableClient();
}
if (WaitingForServerConnection){
DisableServer();
}
}
//---------------------------------------------------------------------------
void __fastcall TTalkForm::FileSettingsClick(TObject *Sender)
{
TalkSettingsForm = new TTalkSettingsForm(this);
TalkSettingsForm->ClientPortMaskEdit->Text = IntToStr(ClientSocket1->Port);
TalkSettingsForm->HostEdit->Text = ClientSocket1->Host;
TalkSettingsForm->ServerPortMaskEdit->Text = IntToStr(ServerSocket1->Port);
if (TalkSettingsForm->ShowModal() == mrOk){
ClientSocket1->Port = atol(TalkSettingsForm->ClientPortMaskEdit->Text.c_str());
ClientSocket1->Host = TalkSettingsForm->HostEdit->Text;
ServerSocket1->Port = atol(TalkSettingsForm->ServerPortMaskEdit->Text.c_str());
}
delete TalkSettingsForm;
}
//---------------------------------------------------------------------------
void __fastcall TTalkForm::Memo1KeyDown(TObject *Sender, WORD &Key,
TShiftState Shift)
{
if (OutSocket){
if (Key == VK_RETURN){
OutSocket->SendText(Memo1->Text);
Memo1->Lines->Clear();
Key = 0;
}
}
}
//---------------------------------------------------------------------------
void __fastcall TTalkForm::ClientSocket1Connect(TObject *Sender,
TCustomWinSocket *Socket)
{
StatusBar1->SimpleText = "Connected to server...";
OutSocket = Socket;
}
//---------------------------------------------------------------------------
void __fastcall TTalkForm::ClientSocket1Disconnect(TObject *Sender,
TCustomWinSocket *Socket)
{
DisableClient();
}
//---------------------------------------------------------------------------
void __fastcall TTalkForm::ClientSocket1Connecting(TObject *Sender,
TCustomWinSocket *Socket)
{
StatusBar1->SimpleText = "Connecting to localhost...";
}
//---------------------------------------------------------------------------
void __fastcall TTalkForm::ClientSocket1Lookup(TObject *Sender,
TCustomWinSocket *Socket)
{
StatusBar1->SimpleText = "Looking up host localhost...";
}
//---------------------------------------------------------------------------
void __fastcall TTalkForm::ClientSocket1Error(TObject *Sender,
TCustomWinSocket *Socket, TErrorEvent ErrorEvent, int &ErrorCode)
{
StatusBar1->SimpleText = "Error Code " + IntToStr(ErrorCode);
ShowMessage("Socket Error " + IntToStr(ErrorCode));
ErrorCode = 0;
DisableClient();
}
//---------------------------------------------------------------------------
void __fastcall TTalkForm::ClientSocket1Read(TObject *Sender,
TCustomWinSocket *Socket)
{
StatusBar1->SimpleText = "从服务器中读入数据...";
Memo2->Lines->Add(Socket->ReceiveText());
}
//---------------------------------------------------------------------------
void __fastcall TTalkForm::MenuListenClick(TObject *Sender)
{
EnableServer();
}
//---------------------------------------------------------------------------
void TTalkForm::EnableServer(){
Caption = "网络对话程序--服务器";
ServerSocket1->Active = true;
StatusBar1->SimpleText = "Waiting for a connection...";
WaitingForServerConnection = true;
}
//---------------------------------------------------------------------------
void TTalkForm::DisableServer(){
DefaultSBText();
OutSocket = NULL;
Caption = "网络对话程序--客户";
ServerSocket1->Active = false;
WaitingForServerConnection = false;
}
//---------------------------------------------------------------------------
void TTalkForm::EnableClient(){
DisableServer();
ClientConnected = true;
StatusBar1->SimpleText = "Connecting to localhost...";
ClientSocket1->Open();
}
//---------------------------------------------------------------------------
void TTalkForm::DisableClient(){
DefaultSBText();
OutSocket = NULL;
ClientConnected = false;
ClientSocket1->Close();
}
//---------------------------------------------------------------------------
void __fastcall TTalkForm::Exit1Click(TObject *Sender)
{
Close();
}
//---------------------------------------------------------------------------
void __fastcall TTalkForm::ServerSocket1ClientRead(TObject *Sender,
TCustomWinSocket *Socket)
{
StatusBar1->SimpleText = "从客户中读入数据...";
Memo2->Lines->Add(Socket->ReceiveText());
}
//---------------------------------------------------------------------------
void __fastcall TTalkForm::ServerSocket1ClientConnect(TObject *Sender,
TCustomWinSocket *Socket)
{
StatusBar1->SimpleText = "Client Connect...";
OutSocket = Socket;
}
//---------------------------------------------------------------------------
void __fastcall TTalkForm::ServerSocket1ClientDisconnect(TObject *Sender,
TCustomWinSocket *Socket)
{
StatusBar1->SimpleText = "Client Disconnect...";
DisableServer();
}
//---------------------------------------------------------------------------
void __fastcall TTalkForm::ServerSocket1ClientError(TObject *Sender,
TCustomWinSocket *Socket, TErrorEvent ErrorEvent, int &ErrorCode)
{
StatusBar1->SimpleText = "Error Code " + IntToStr(ErrorCode);
ShowMessage("Socket Error " + IntToStr(ErrorCode));
ErrorCode = 0;
DisableServer();
}
//---------------------------------------------------------------------------
void TTalkForm::DefaultSBText(){
StatusBar1->SimpleText = "Please Select Mode";
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -