BCB之Socket通信

类别:编程语言 点击:0 评论:0 推荐:
 

BCB之Socket通信

曾棕根

山水清音阁:http://blog.csdn.net/ssqy

2/26/2005 11:15:32 PM

Socket套接字:每个套接字由IP地址+服务号码(称之为端口port)组成。数据传输之后,必须释放和撤销连接。

TclientSocketTserverSocket两个网络组件,位于Internet页。

 

TserverSocket

1.       Port属性:int型,设置端口号,如6767

2.       Activate属性:bool型,设置监听:true;关闭监听:false

3.       OnListen事件:侦听连接形成之前,发生此事件

4.       OnAccept事件:接受了某客户端连接请求后发生此事件

5.       OnClientRead事件:通过此事件,从套接字读出数据(从客户端发来的)

6.       OnClientWrite事件:通过此事件,可以对套接字写入数据

 

TclientSocket

1.       Address属性:服务器IP地址

2.       Host属性:服务器主机名称,当Host和Address都指定时,TclientSocket将使用Host属性工作

3.       Port属性: int型,设置套接字端口号,如6767

4.       Activate属性:true:与服务器建立连接;false:与服务器断开连接

5.       OnLookup事件:在定位服务器套接字之前,发生此事件

6.       OnConnecting事件:在服务器套接字被定位后,发生此事件

7.       OnConnect事件:与服务器建立连接后,发生此事件

8.       OnRead事件:通过此事件,可从连接套接字读出数据

9.       OnWrite事件:通过此事件,可对套接字写入数据

10.   OnDisConnect事件:在与服务器断开连接时,发生此事件

 

 

一个网络聊天程序例子

服务器

1.       port:6767

2.       代码:

///---------------------------------------------------------------------------

 

#include <vcl.h>

#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)

{

  ///发送信息

  AnsiString Msg=Edit1->Text+ ":" + Edit2->Text;

  Edit2->Text="";

  Memo1->Lines->Add(Msg);

  ///将信息发送到所有客户端

  for(int i=0;i<ServerSocket1->Socket->ActiveConnections;i++)

    ServerSocket1->Socket->Connections[i]->SendText(Msg);      

}

///---------------------------------------------------------------------------

 

void __fastcall TForm1::ServerSocket1Accept(TObject *Sender,

      TCustomWinSocket *Socket)

{

  ///服务器接收客户端请求后发生Accept事件

  AnsiString Msg="服务器消息:" + Socket->RemoteHost +"进入了聊天室";

  ///向所有客户端发送消息

  for(int i=0;i<ServerSocket1->Socket->ActiveConnections;i++)

    ServerSocket1->Socket->Connections[i]->SendText(Msg);

  ///在自己的信息栏中加入该信息

  Memo1->Lines->Add(Msg);

}

///---------------------------------------------------------------------------

void __fastcall TForm1::ServerSocket1ClientRead(TObject *Sender,

      TCustomWinSocket *Socket)

{

  ///从套接字中读出数据

  AnsiString Msg=Socket->ReceiveText();

  //把信息发送到所有客户端

  for(int i=0;i<ServerSocket1->Socket->ActiveConnections;i++)

    ServerSocket1->Socket->Connections[i]->SendText(Msg);

  ///在自己的信息栏上加入该信息

  Memo1->Lines->Add(Msg);

}

///---------------------------------------------------------------------------

void __fastcall TForm1::FormCreate(TObject *Sender)

{

  Edit1->Text="";

  Edit2->Text="";        

}

///---------------------------------------------------------------------------

void __fastcall TForm1::Button2Click(TObject *Sender)

{

  ServerSocket1->Port=StrToInt(Edit3->Text);

  ServerSocket1->Active=true;

  Shape1->Visible=true;     

}

///---------------------------------------------------------------------------

 

void __fastcall TForm1::Button3Click(TObject *Sender)

{

  ServerSocket1->Active=false;

  Shape1->Visible=false;       

}

///---------------------------------------------------------------------------

 

 

 

 

客户端:

1.       Address:127.0.0.1(服务器IP地址)

2.       Port:6767(服务器的端口号)

3.       代码

///---------------------------------------------------------------------------

 

#include <vcl.h>

#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)

{

  AnsiString Msg=Edit1->Text + ":" + Edit2->Text;

  Memo1->Lines->Add(Msg);

  Edit2->Text="";

  ///向服务器发送信息

  ClientSocket1->Socket->SendText(Msg);

}

//---------------------------------------------------------------------------

void __fastcall TForm1::ClientSocket1Read(TObject *Sender,

      TCustomWinSocket *Socket)

{

  ///客户端读取信息

  AnsiString Msg=Socket->ReceiveText();

  ///判断是不是自己发出的信息

  if(Msg!=Memo1->Lines->Strings[Memo1->Lines->Count-1])

  Memo1->Lines->Add(Msg);       

}

///---------------------------------------------------------------------------

void __fastcall TForm1::FormCreate(TObject *Sender)

{

  Edit1->Text="";

  Edit2->Text="";       

}

///---------------------------------------------------------------------------

void __fastcall TForm1::Button2Click(TObject *Sender)

{

  ClientSocket1->Address=Edit3->Text;

  ClientSocket1->Port=StrToInt(Edit4->Text);

  ClientSocket1->Active=true;       

}

///---------------------------------------------------------------------------

 

void __fastcall TForm1::Button3Click(TObject *Sender)

{

  ClientSocket1->Active=false;       

}

///---------------------------------------------------------------------------

 

void __fastcall TForm1::ClientSocket1Connect(TObject *Sender,

      TCustomWinSocket *Socket)

{

 

  AnsiString Msg="连接成功,[主机]"  + Socket->RemoteHost +"[主机地址]" + Socket->RemoteAddress + "[主机监听端口]" + IntToStr(Socket->RemotePort);

  ShowMessage(Msg);

}

///---------------------------------------------------------------------------

 

void __fastcall TForm1::ClientSocket1Disconnect(TObject *Sender,

      TCustomWinSocket *Socket)

{

  Socket->SendText(Edit1->Text + "下线了");

  ///ClientSocket1->Socket->SendText(Edit1->Text + "下线了");

  ShowMessage("已断开连接");

 

}

///---------------------------------------------------------------------------正文 

本文地址:http://com.8s8s.com/it/it23208.htm