📄 client.cpp
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "iostream.h"
#include "io.h"
#include "time.h"
#include "stdio.h"
#include "alloc.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
AnsiString yourName="yourName";
AnsiString IP="255.255.255.0";
AnsiString HostName="";
AnsiString NowlogName="";
AnsiString NowlogHost="";
AnsiString NowlogAddress="";
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
Memo1->Clear();
Memo2->Clear();
Memo3->Clear();
Memo4->Clear();
BitBtn6->Enabled=false;
Edit1->Text="";
Edit2->Text="dy-02";
StaticText1->Caption="";
StaticText5->Caption="";
StaticText6->Caption="";
StaticText7->Caption="";
Panel1->Caption="私聊对象:" ;
//---------------------------------------
//获取本地IP地址及主机名
WORD wVersionRequested;
WSADATA wsaData;
//Start up WinSock
wVersionRequested = MAKEWORD(1, 1);
WSAStartup(wVersionRequested, &wsaData);
hostent *p;
char s[128];
char *p2;
//Get the computer name
gethostname(s, 128);
p = gethostbyname(s);
StaticText5->Caption=p->h_name;
//Get the IpAddress
p2 = inet_ntoa(*((in_addr *)p->h_addr));
StaticText6->Caption=p2;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn4Click(TObject *Sender)
{
Memo3->Clear();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn2Click(TObject *Sender)
{
Memo1->Clear();
}
//---------------------------------------------------------------------------
//连接服务器
void __fastcall TForm1::ClientSocket1Connect(TObject *Sender,
TCustomWinSocket *Socket)
{
StatusBar1->SimpleText="成功连接到服务器"+ClientSocket1->Host;;
ClientSocket1->Socket->SendText(yourName+"@login@@");
StaticText7->Caption=DateToStr(Now())+" "+Time();
}
//---------------------------------------------------------------------------
//发送信息
void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{
if(Memo2->Lines->Text=="")
Application->MessageBox("错误, 不能发空信息!","提示",MB_OK);
else
{
AnsiString Data;
Data=yourName+":"+Memo2->Lines->Text;
ClientSocket1->Socket->SendText(Data);
// Memo1->Lines->Add(Data);
Memo2->Lines->Clear();
Memo2->SetFocus();
}
}
//---------------------------------------------------------------------------
//接收服务器发送过来的信息
void __fastcall TForm1::ClientSocket1Read(TObject *Sender,
TCustomWinSocket *Socket)
{
int i=0;
AnsiString ch="";
NowlogName="";
NowlogHost="";
NowlogAddress="";
AnsiString Data=Socket->ReceiveText(); //接收用户发送的信息
AnsiString SubData1=Data.SubString(1,8); //分析信息
if(SubData1=="@login@@") //如果是用户登陆的信息
{
for(i=9;(ch=Data.SubString(i,1))!="@";i++)
NowlogName+=ch;
for( ;(ch=Data.SubString(i+1,1))!="@";i++)
NowlogHost+=ch;
NowlogAddress=Data.SubString(i+1,18);
ListBox1->Items->Add(NowlogName); //用户信息加入到用户列表
ListBox2->Items->Add(NowlogAddress);
ListBox3->Items->Add(NowlogHost);
AnsiString SysInfo="["+TimeToStr(Now())+"] "+NowlogName+" 加入了会议!";//显示系统信息
Memo1->Lines->Add(SysInfo);
}
else if(SubData1=="@logout@") //如果是用户退出会议
{
for(i=9;(ch=Data.SubString(i,1))!="@";i++)
NowlogName+=ch;
for( ;(ch=Data.SubString(i+1,1))!="@";i++)
NowlogHost+=ch;
NowlogAddress=Data.SubString(i+1,18);
int Index1=ListBox1->Items->IndexOf(NowlogName); //从用户列表中删除该用户信息
ListBox1->Items->Delete(Index1);
int Index2=ListBox2->Items->IndexOf(NowlogAddress);
ListBox2->Items->Delete(Index2);
int Index3=ListBox3->Items->IndexOf(NowlogHost);
ListBox3->Items->Delete(Index3);
Memo1->Lines->Add("["+TimeToStr(Now())+"] "+NowlogName+" 退出了会议!"); //显示系统信息
}
else
Memo1->Lines->Add(Data);
}
//---------------------------------------------------------------------------
//私聊空间或传送文件选定对象
void __fastcall TForm1::ListBox1Click(TObject *Sender)
{
if(ClientSocket2->Active)
{
ClientSocket2->Active=false;
ClientSocket2->Close();
}
int Index=ListBox1->ItemIndex;
IP=ListBox2->Items->Strings[Index];
HostName=ListBox3->Items->Strings[Index];
Panel1->Caption="私聊对象:"+ListBox1->Items->Strings[Index];
StaticText1->Caption=ListBox1->Items->Strings[Index];
ClientSocket2->Host=HostName;
ClientSocket2->Address=IP;
ClientSocket2->Port=2222;
ClientSocket2->Active=true;
BitBtn3->Enabled=true;
}
//---------------------------------------------------------------------------
//在私聊空间发送信息给选定用户
void __fastcall TForm1::BitBtn3Click(TObject *Sender)
{
if(Memo4->Lines->Text=="")
Application->MessageBox("错误, 不能发空信息!","提示",MB_OK);
else
{
AnsiString Data;
Data=yourName+"对"+StaticText1->Caption+"说:"+Memo4->Lines->Text;
ClientSocket2->Socket->SendText(Data);
// Memo3->Lines->Add(Data);
Memo4->Lines->Clear();
Memo4->SetFocus();
}
}
//---------------------------------------------------------------------------
//选择要传送的文件
void __fastcall TForm1::BitBtn5Click(TObject *Sender)
{
if(NMStrm1->Connected)
NMStrm1->Disconnect();
if(OpenDialog1->Execute())
{
Edit1->Text=OpenDialog1->FileName;
BitBtn6->Enabled=true;
}
}
//---------------------------------------------------------------------------
//登陆到服务器
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if(ClientSocket1->Active)
{
ClientSocket1->Active=false;
ClientSocket1->Close();
}
ListBox1->Items->Clear();
ListBox2->Items->Clear();
Memo1->Clear();
Memo3->Clear();
if(InputQuery("登陆到服务器","请输入你的昵称:",yourName)) //输入昵称
{
StatusBar1->SimpleText="正在连接服务器...";
ClientSocket1->Address=MaskEdit1->Text;
ClientSocket1->Host=Edit2->Text;
ClientSocket1->Port=4000;
ClientSocket1->Active=true;
BitBtn1->Enabled=true;
BitBtn3->Enabled=true;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
ListBox1->Items->Clear();
ListBox2->Items->Clear();
ListBox3->Items->Clear();
ClientSocket1->Active=false;
StatusBar1->SimpleText="连接中断...";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
WSACleanup();
ServerSocket1->Close();
ClientSocket1->Close();
ClientSocket2->Close();
NMStrm1->Cancel();
NMStrmServ1->Cancel();
Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::N2Click(TObject *Sender)
{
TabbedNotebook1->PageIndex=3;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -