我用TClientSocket & TServerSocket寫了個程式
一為server,另一為client
如果在兩台電腦上,一台執行server,一台執行client的話
可以正常的傳遞訊息
但是如果我在執行client的那台電腦上執行兩個client的程式的話
則第二個執行的client的程式卻沒辦法正確的傳遞訊息
希望有人可以幫我解決,謝謝
以下為server和client的程式碼 -----server
#include
#pragma hdrstop #include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender)
{
if (Form1->Edit1->Text=="")
{
MessageBeep(MB_ICONWARNING);
MessageBox(NULL,"Port 尚未輸入!!","警告",MB_ICONWARNING);
}
else
{
Form1->ServerSocket1->Port=StrToInt(Form1->Edit1->Text);
Form1->ServerSocket1->Active=true;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ServerSocket1ClientRead(TObject *Sender,
TCustomWinSocket *Socket)
{
Form1->Memo1->Lines->Text=
"[" TimeToStr(Now()) "]"
"[" Socket->RemoteAddress "]"
"[" Socket->RemotePort "]" ":" "\n"
Form1->ServerSocket1->Socket->Connections[0]->ReceiveText();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ServerSocket1ClientConnect(TObject *Sender,
TCustomWinSocket *Socket)
{
Form1->Edit2->Text="連接中";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
Form1->ServerSocket1->Close();
Form1->Edit2->Text="斷線";
}
//-------------------------------------------------------------------- -----client
#include
#pragma hdrstop #include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{ }
//--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender)
{
if (Form1->Edit1->Text==""||Form1->Edit2->Text=="")
{
MessageBeep(MB_ICONWARNING);
MessageBox(NULL,"Server IP and Server Port 尚未輸入完整!!","警告",MB_ICONWARNING);
}
else
{
Form1->ClientSocket1->Host=Form1->Edit1->Text;
Form1->ClientSocket1->Port=StrToInt(Edit2->Text);
Form1->ClientSocket1->Active=true; }
}
//--------------------------------------------------------------------------- void __fastcall TForm1::Button2Click(TObject *Sender)
{
Form1->ClientSocket1->Socket->Close();
Form1->ClientSocket1->Active=false;
Form1->Edit1->Text="";
Form1->Edit2->Text="";
Form1->Edit3->Text="斷線";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ClientSocket1Connect(TObject *Sender,
TCustomWinSocket *Socket)
{
Form1->Edit3->Text="連接中";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
Form1->ClientSocket1->Socket->SendText(Form1->Memo1->Lines->Text);
Form1->Memo1->Lines->Text="";
Form1->Edit4->Text=Form1->ClientSocket1->Socket->LocalPort;
}
//---------------------------------------------------------------------------