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

📄 client.cpp

📁 一个简单的聊天程序(BCB) 服务器端:server.cpp 客户端:client.cpp
💻 CPP
📖 第 1 页 / 共 2 页
字号:
 StatusBar1->SimpleText="传送文件...";
 Edit1->SetFocus();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::N1Click(TObject *Sender)
{
 TabbedNotebook1->PageIndex=2;
 StatusBar1->SimpleText="私聊空间...";
 Memo4->SetFocus();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::TabbedNotebook1Change(TObject *Sender, int NewTab,
      bool &AllowChange)
{
 if(NewTab==0)        StatusBar1->SimpleText="连接设置...";
 else if(NewTab==1)   StatusBar1->SimpleText="网络会议...";
 else if(NewTab==2)   StatusBar1->SimpleText="私聊空间...";
 else if(NewTab==3)   StatusBar1->SimpleText="传送文件...";
 else if(NewTab==4)   StatusBar1->SimpleText="本机信息...";
 else if(NewTab==5)   StatusBar1->SimpleText="版本信息...";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
 ListBox1->Items->Clear();
 ListBox2->Items->Clear();
 ListBox3->Items->Clear();
 ClientSocket1->Active=false;
 StatusBar1->SimpleText="连接已中断...";
 Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ServerSocket1ClientRead(TObject *Sender,
      TCustomWinSocket *Socket)
{
 AnsiString Data=Socket->ReceiveText(); //接收私聊对象发送的信息
 Memo3->Lines->Add(Data);

}
//---------------------------------------------------------------------------

void __fastcall TForm1::ClientSocket1Disconnect(TObject *Sender,
      TCustomWinSocket *Socket)
{
 ClientSocket1->Socket->SendText(yourName+"@logout@");
 ClientSocket1->Close();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ClientSocket1Error(TObject *Sender,
      TCustomWinSocket *Socket, TErrorEvent ErrorEvent, int &ErrorCode)
{
 Application->MessageBox("无法连接主机!网络不通或者服务端未启动!","连接失败!",MB_OK);
 StatusBar1->SimpleText="连接服务器失败...";
 Close();
}
//---------------------------------------------------------------------------


void __fastcall TForm1::ClientSocket2Connect(TObject *Sender,
      TCustomWinSocket *Socket)
{
 StatusBar1->SimpleText="成功连接到"+StaticText1->Caption;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ClientSocket2Disconnect(TObject *Sender,
      TCustomWinSocket *Socket)
{
 ClientSocket2->Close();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ClientSocket2Error(TObject *Sender,
      TCustomWinSocket *Socket, TErrorEvent ErrorEvent, int &ErrorCode)
{
 Application->MessageBox("无法连接到对对象!网络不通或对方未在线!","连接失败!",MB_OK);
 StatusBar1->SimpleText="连接对象失败...";

}
//---------------------------------------------------------------------------

 

void __fastcall TForm1::BitBtn6Click(TObject *Sender)
{
 if(Edit1->Text=="")
  {
   Application->MessageBox("请选择需要传送的文件!","...",MB_OK);
   BitBtn6->Enabled=false;
  }
 else
  {
   BitBtn6->Enabled=true;
   TFileStream *MyFStream;
   MyFStream = new TFileStream(OpenDialog1->FileName, fmOpenRead);
   try
    {
     NMStrm1->Host = HostName;
     NMStrm1->FromName = Edit1->Text;
     NMStrm1->PostIt(MyFStream);
    }
   catch(...)
    {
    }
   MyFStream->Free();
  }
}
//---------------------------------------------------------------------------
//接收并保存发送过来的文件
void __fastcall TForm1::NMStrmServ1MSG(TComponent *Sender,
      const AnsiString sFrom, TStream *strm)
{
 AnsiString nmFilenameExt="";
 int count=1;
 for(count=1;count<=sFrom.Length();)
  {
   if(sFrom.SubString(count,1)==".")
    break;
   else
    count++;
  }
 nmFilenameExt=sFrom.SubString(count+1,sFrom.Length()-count);
 SaveDialog1->Title="保存文件到:";
 //设置对话框的标题
 SaveDialog1->InitialDir="\\Data";
 //设置对话框的缺省路径
 SaveDialog1->Filter="Files(*."+nmFilenameExt+")|*."+nmFilenameExt;
  //设置文件过滤器
 SaveDialog1->DefaultExt=nmFilenameExt;
 //设置缺省文件名
 if(SaveDialog1->Execute())
  {
//   StatusBar1->SimpleText="正在接收文件:"+sFrom+"...";
   TFileStream *MyFStream;
   MyFStream = new TFileStream(SaveDialog1->FileName, fmCreate);
   try
    {
     MyFStream->CopyFrom(strm, strm->Size);
    }
   catch(...)
    {
    }
   MyFStream->Free();
  }

 if(NMStrmServ1->BytesRecvd!=NMStrmServ1->BytesTotal)
  ProgressBar1->Position=(float(NMStrmServ1->BytesRecvd)/float(NMStrmServ1->BytesTotal))*100;
 else
  {
   ProgressBar1->Position=100;
//   Application->MessageBox("接收文件结束!","提示",MB_OK);
   ProgressBar1->Position=0;
   StatusBar1->SimpleText="接收文件结束!";
  }
}
//---------------------------------------------------------------------------

 

void __fastcall TForm1::BitBtn7Click(TObject *Sender)
{
   NMStrm1->Cancel();
   NMStrmServ1->Cancel();
   StatusBar1->SimpleText="传送文件...";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::NMStrm1InvalidHost(bool &Handled)
{
 Application->MessageBox("无法识别的主机!","",MB_OK);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::NMStrm1ConnectionFailed(TObject *Sender)
{
 Application->MessageBox("连接失败,请确认对方地址是否正确!","",MB_OK);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::NMStrm1MessageSent(TObject *Sender)
{
 if(NMStrm1->BytesSent!=NMStrm1->BytesTotal)
  StatusBar1->SimpleText="正在传送文件:"+Edit1->Text+"...";
 else
  StatusBar1->SimpleText="传送文件结束!";
}
//---------------------------------------------------------------------------

void __fastcall TForm1::NMStrm1Status(TComponent *Sender,
      AnsiString Status)
{
  StatusBar1->SimpleText="Last WinSock error: "+IntToStr(NMStrm1->LastErrorNo);
  if (NMStrm1->BeenCanceled)
    StatusBar1->SimpleText="操作被取消...";
  if (NMStrm1->BeenTimedOut)
    StatusBar1->SimpleText="操作超时...";
}
//---------------------------------------------------------------------------

void __fastcall TForm1::NMStrm1Connect(TObject *Sender)
{
  StatusBar1->SimpleText="连接成功...";
}
//---------------------------------------------------------------------------

void __fastcall TForm1::NMStrm1Disconnect(TObject *Sender)
{
// StatusBar1->SimpleText="中断连接...";
}
//---------------------------------------------------------------------------

void __fastcall TForm1::NMStrm1HostResolved(TComponent *Sender)
{
  StatusBar1->SimpleText = "Host Resolved";
}
//---------------------------------------------------------------------------

void __fastcall TForm1::NMStrmServ1ClientContact(TObject *Sender)
{
 StatusBar1->SimpleText = "Client Contacted";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::NMStrmServ1Status(TComponent *Sender,
      AnsiString Status)
{
  if (StatusBar1 != 0)
    StatusBar1->SimpleText = Status;
}
//---------------------------------------------------------------------------


void __fastcall TForm1::NMStrm1PacketSent(TObject *Sender)
{
 if(NMStrm1->BytesSent!=NMStrm1->BytesTotal)
  ProgressBar1->Position=(float(NMStrm1->BytesSent)/float(NMStrm1->BytesTotal))*100;
 else
  {
   ProgressBar1->Position=100;
//   Application->MessageBox("传送文件结束!","提示",MB_OK);
   ProgressBar1->Position=0;
   StatusBar1->SimpleText="传送文件结束!";
  }
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

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