ºìÁªLinuxÃÅ»§
Linux°ïÖú

C++Builder´´½¨»ùÓÚInternetµã¶ÔµãChat

·¢²¼Ê±¼ä:2006-05-03 11:29:51À´Ô´:ºìÁª×÷Õß:°®µÄ±ßÔµ
´´½¨»ùÓÚInternetµÄÓ¦ÓóÌÐò£¬ÄãÒ²Ðí»áÏëµ½¸´ÔÓµÄWinSock±à³Ì¡£²»¹ý£¬C++ Builder3ÌṩÁËеÄWebBrokerµÄInternetÌ×¼þ£¬ÆäÖеÄTClientSocketºÍTServerSocket×é¼þ·â×°ÁË WindowsµÄÓйØAPI£¬´ó´ó¼ò»¯ÁËWinSock±à³Ì¡£ÒªÍ¨¹ýInternet´«ÊäÊý¾Ý£¬ÖÁÉÙÐèÒªÒ»¶ÔSocket£¬Ò»¸öSocketÔÚ¿Í»§¶Ë£¬ÁíÒ»¸öSocketÔÚ·þÎñÆ÷¶Ë¡£ÆäʵTClientSocket¡¢TServerSocket×é¼þ²¢²»ÊÇSocket¶ÔÏó£¬ÆäÊôÐÔSocket½«·µ»Ø¸÷×﵀ Socket¶ÔÏó¡£TClientSocketÓÃÀ´´¦Àí¿Í»§¶Ëµ½·þÎñÆ÷¶ËÖ®¼äµÄsocketÁ¬½Ó£¬TServerSocketÓÃÀ´´¦ÀíÓÉ¿Í»§¶Ë·¢À´µÄ socketÁ¬½Ó£¬Ò»µ©¿Í»§¶ËºÍ·þÎñÆ÷¶Ë¶¼½ÓͨÁËsocket£¬¿Í»§¶ËºÍ·þÎñÆ÷¶Ë¾Í¿ÉÒÔÏ໥ͨÐÅÁË¡£

½¨Á¢Ò»ÐÂÏîÄ¿£¬´´½¨Ó¦ÓóÌÐòµÄÓû§½çÃ棺

1.½«×é¼þÒ³Çл»µ½InternetÒ³£¬·ÅÒ»¸öTServerSocket×é¼þºÍÒ»¸öTClientSocket×é¼þµ½´°ÌåÉÏ£¬ÕâÑùÓ¦ÓóÌÐò¼È¿ÉÒÔÊÇTCP/IP·þÎñÆ÷£¬Ò²¿ÉÒÔÊÇTCP/IP¿Í»§¡£½«PortÊôÐÔ¶¼ÉèΪͬһ¸öÖµ(Èç 1000)£¬È·¶¨SocketÖ®¼äµÄÁ¬½ÓÀàÐÍΪNonBlocking(·Ç×èÈû·½Ê½)¡£

2.·ÅÁ½¸öTMemo×é¼þµ½´°ÌåÉÏ£¬ÓÃÀ´·Ö±ðÏÔʾ˫·½µÄ̸»°ÄÚÈÝ£¬½«Memo2µÄReadOnlyÊôÐÔÉèΪTrue¡£

3.ÔÚ´°ÌåµÄ¶¥²¿·ÅÉÏÒ»¸öPanel×é¼þ£¬ÔÚÆäÉÏ·ÅÈý¸ö°´Å¥£º¼àÌý(btnlisten)¡¢Á¬½Ó(btnconnect)¡¢¶Ï¿ª(btndisconnect)£¬ÓÃÀ´Æô¶¯ÏàÓ¦µÄ²Ù×÷¡£

4.ÔÚ´°Ìåµ×²¿·ÅÒ»¸öStatusBar×é¼þ£¬½«ÆäSimplePanelÊôÐÔÉèΪTrue,ÔÚÏàÓ¦µÄʼþ´¦Àí³ÌÐòÖиıä״̬ÌõÐÅÏ¢£¬ÈÃÓû§ËæʱÁ˽âÁ¬½Ó״̬¡£

´ò¿ªÍ·Îļþ£¬ÔÚ´°ÌåÀàµÄPrivate¶ÎÌí¼ÓÁ½¸ö˽ÓгÉÔ±: bool IsServer;String Server¡£Ë«·½Í¨ÐÅʱÐèͬʱÔËÐÐChat³ÌÐò£¬IsServerÓÃÀ´È·¶¨ÄĸöChat³ÌÐò´¦ÓÚ·þÎñÆ÷¶Ë£¬ServerÓÃÀ´´æ·Å·þÎñÆ÷µÄÖ÷»úÃû¡£½¨Á¢´°ÌåÀàµÄ¹¹ÔìÆ÷ÈçÏ£º

fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
IsServer=false;
Server="localhost";
}

ÕâÀïServer±»È±Ê¡ÉèΪlocalhost£¬ÕâÑù³ÌÐò¿ÉÒÔÔÚûÓÐÁ¬ÈëInternetµÄµ¥»úÉϽøÐе÷ÊÔ¡£ÔÚWindows×ÓĿ¼ÏÂÄã¿ÉÒÔÕÒµ½hosts.samÎļþÖУ¬ÔÚ¸ÃÎļþÖÐÒѾ­½«±¾»úIPµØÖ·127.0.0.1¶¨ÒåÁËÖ÷»úÃû£º

localhost¡£

void __fastcall TForm1::FormCreate(TObject *Sender)
{
btndisconnect- >Enabled=false;
}

³ÌÐòÔËÐкó£¬Èç¹ûÓû§°´Ï£¢¼àÌý£¢Å¥£¬Ôò½«¸Ã³ÌÐòÉèΪ·þÎñÆ÷¶Ë£¬ÕâʱӦ½«TServerSocketµÄActiveÊôÐÔÉèΪTrue£¬Ê¹·þÎñÆ÷×Ô¶¯½øÈë¼àÌý״̬¡£

void __fastcall TForm1::btnlistenClick(TObject *Sender)
{
ClientSocket1- >Active=false;
ServerSocket1- >Active=true;
StatusBar1- >SimpleText="ÕýÔÚ¼àÌý...";
btnlisten- >Enabled=false;
btnconnect- >Enabled=false;
}

µ±Óû§°´Ï£¢Á¬½Ó£¢Å¥ºó£¬³ÌÐò»áµ¯³öÒ»¸öѯÎÊ¿ò£¬ÒªÇóÓû§ÊäÈëÒªÁ¬½ÓµÄ·þÎñÆ÷µÄÖ÷»úÃû£¬È»ºó½¨Á¢Á¬½Ó¡£

void __fastcall TForm1::btnconnectClick(TObject *Sender)
{
if(InputQuery("Á¬½Óµ½·þÎñÆ÷","ÊäÈë·þÎñÆ÷µØÖ·:",Server)){
if(Server.Length() >0){
ClientSocket1- >Host=Server;
ClientSocket1- >Active=true;
btnlisten- >Enabled=false;
btnconnect- >Enabled=false;
btndisconnect- >Enabled=true;
}
}
}

µ±Óû§Ìá³öÁ¬½ÓÇëÇóºó£¬¿Í»§¶Ë»á´¥·¢OnCreateʼþ£¬³ÌÐòÏÈÔÚ״̬ÌõÖÐÏÔʾÁ¬½ÓÐÅÏ¢£¬È»ºó½«ÏÔʾ¶Ô·½Ì¸»°ÄÚÈݵÄMemo2Çå¿Õ£¬×¼±¸¿ªÊ¼½»Ì¸¡£

void __fastcall TForm1::ClientSocket1Connect(TObject *Sender,
TCustomWinSocket *Socket)
{
StatusBar1- >SimpleText="Á¬½Óµ½:"+Server;
Memo2- >Lines- >Clear();
}

ÔÚ·þÎñÆ÷½ÓÊÜÁË¿Í»§µÄÇëÇóºó»á´¥·¢OnAcceptʼþ£¬ÔÚÕâ¸öʼþ´¦Àí³ÌÐòÖн«±êÖ¾·þÎñÆ÷¶ËµÄ±äÁ¿IsServerÉèΪTrue£¬²¢×¼±¸¿ªÊ¼½»Ì¸¡£

void __fastcall TForm1::ServerSocket1Accept(
TObject *Sender,
TCustomWinSocket *Socket)
{
Memo2- >Lines- >Clear();
IsServer=true;
StatusBar1- >SimpleText="Á¬½Óµ½:"
+Socket- >RemoteAddress;
}

ÔÚ½¨Á¢Á¬½Óºó£¬Ë«·½¾Í¿ÉÒÔÔÚMemo1ÖÐÊäÈë̸»°ÄÚÈÝ¿ªÊ¼½øÐн»Ì¸ÁË£¬°´ÏÂEnter¼üºó£¬½«ËùÔÚÐеÄÎı¾·¢ËͳöÈ¥¡£·þÎñÆ÷¶ËµÄSocketµÄConnectionsÊôÐÔ·µ»ØÒ»¸öÊý×飬¸ÃÊý×éÓÉ·þÎñÆ÷µ±Ç°»î¶¯µÄÁ¬½Ó×é³É¡£

void __fastcall TForm1::Memo1KeyDown(
TObject *Sender, WORD &Key,
TShiftState Shift)
{
if(Key==VK_RETURN){
if(IsServer)
ServerSocket1- >Socket- >Connections[0]- >SendText(
Memo1- >Lines- >Strings[Memo1- >Lines- >Count-1]);
else
ClientSocket1- >Socket- >SendText(
Memo1- >Lines- >Strings[Memo1- >Lines- >Count-1]);
}
}

ÔÚ±¾ÀýÖÐÎÒÃDzÉÓ÷Ç×èÈû´«Ê䷽ʽ£¬µ±ÆäÖеÄÒ»·½½øÐÐд²Ù×÷ʱ£¬ÁíÒ»·½»á´¥·¢OnReadʼþ(¿Í»§¶Ë)»òOnClientReadʼþ(·þÎñÆ÷¶Ë)£¬ÕâÁ½¸öʼþµÄ´¦Àí³ÌÐòÖ»Êǽ«½ÓÊÕµ½µÄÄÚÈÝÌí¼Óµ½Memo2µÄºóÃæ¡£

Memo2- >Lines- >Add(Socket- >ReceiveText());

Èç¹ûÔÚÓû§½¨Á¢Á¬½Óºóµ¥»÷£¢¶Ï¿ª£¢Å¥£¬½«¶Ï¿ª¿Í»§¶ËÓë·þÎñÆ÷µÄÁ¬½Ó£¬·þÎñÆ÷¶Ë½«´¥·¢ OnClientDisconnectʼþ£¬¶ø¿Í»§¶ËÔò»á´¥·¢OnDisconnectʼþ£¬Õâʱ·þÎñÆ÷¶ËÓ¦»Øµ½¼àÌý״̬£¬µÈ´ýÓû§µÄÁ¬½Ó£»¶ø¿Í»§¶Ë½«·µ»Øµ½Á¬½ÓÇ°µÄ״̬£¬µÈ´ýÓû§Ôٴν¨Á¢Á¬½Ó£¬Èç¹ûÓв»Ö¹Ò»¸ö·þÎñÆ÷µÄ»°£¬¿ÉÒÔÑ¡ÔñÁ¬½Óµ½ÆäËûµÄ·þÎñÆ÷ÉÏ¡£

void __fastcall TForm1::btndisconnectClick(
TObject *Sender)
{
ClientSocket1- >Close();
}
void __fastcall TForm1::ServerSocket1ClientDisconnect(
TObject *Sender,
TCustomWinSocket *Socket)
{
StatusBar1- >SimpleText="ÕýÔÚ¼àÌý...";
}
void __fastcall TForm1::ClientSocket1Disconnect(
TObject *Sender, TCustomWinSocket *Socket)
{
btnlisten- >Enabled=true;
btnconnect- >Enabled=true;
btndisconnect- >Enabled=false;
StatusBar1- >SimpleText="";
}

´ËÍâÔÚ¿Í»§¶Ë»¹Ó¦¸ÃÔö¼Ó´íÎ󲶻ñ»úÖÆ£¬µ±Óû§ÊäÈëÎÞЧµÄ·þÎñÆ÷Ãû»ò·þÎñÆ÷¶ËûÓд¦ÓÚ¼àÌý״̬ʱÄܹ»¼°Ê±¸øÓû§·´À¡ÐÅÏ¢¡£

void __fastcall TForm1::ClientSocke
t1Error(TObject *Sender,
TCustomWinSocket *Socket,
TErrorEvent ErrorEvent, int &ErrorCode)
{
StatusBar1- >SimpleText="ÎÞ·¨Á¬½Óµ½:
"+Socket- >RemoteHost;
ErrorCode=0;
}

ÒÔÉÏ´úÂëÔÚC++ Builder3 C/S°æÖбàÒë¡¢ÔËÐÐͨ¹ý¡£
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 0 ÌõÆÀÂÛ