用Internet/ServerSocket写的聊天程式,多个客户端互发信息问题?

loakia 2005-11-13 11:05:11
我用Internet下面的ServeSocket控件写一个聊天程式,但是不知如何处理多个客户互发信息的问题。
是这样的:
一、当有一个客户端登陆进来时,可以记下他的用户名,ip,端口,以及他的index号
二、当他给服务器发信息时,也可以得知他的以上信息。
三、有很多人(>20人)进入聊天室,记下他的用户名,ip,端口,以及他的index号。

问题之一、客户端之间发信息用什么方法进行呢?我试过用IP+端口,但是找不到相应的发送函数。
用index号又不知如何用。

问题之二、如果有一人退出,很多人的index号会变。如5号退出,6号会变成5号,7号会变成6号。
如何解决这个问题。

或者是否有更好的解决方法?
...全文
252 5 打赏 收藏 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
loakia 2005-11-14
  • 打赏
  • 举报
回复
谢谢各位!!我研究一下!
wizardqi 2005-11-14
  • 打赏
  • 举报
回复
楼主的意思是服务器在此充当信息中转的角色,我的考虑思路是这样:
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
struct TClientInfo{
AnsiString Name;
TCustomWinSocket *Socket;
};
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
ClientList=new TThreadList();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ServerSocket1ClientConnect(TObject *Sender,
TCustomWinSocket *Socket)
{
TClientInfo *CI=new TClientInfo();
CI->Name="";
CI->Socket=Socket;
ClientList->Add(CI);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
TList *cl=ClientList->LockList();
for(int i=0;i<cl-> Count-1;i++)
delete cl->Items[i];
ClientList->UnlockList();
delete ClientList;

}
//---------------------------------------------------------------------------
void __fastcall TForm1::ServerSocket1ClientRead(TObject *Sender,
TCustomWinSocket *Socket)
{
TList *cl=ClientList->LockList();
TClientInfo *ci;
int Index=0;
for(;Index<cl->Count;Index++)
{
ci=(TClientInfo *)cl->Items[Index];
if(ci->Socket==Socket)break;
}
if(Index<cl->Count)
{
if(ci->Name=="")
{
ci->Name=Socket->ReceiveText();
ClientListBox->Items->Add(ci->Name);
}
else
{
AnsiString SendTo,SendFrom=ci->Name;
AnsiString msg=Socket->ReceiveText();
SendTo=msg.SubString(2,msg.Pos(":")-2);
msg.Delete(1,msg.Pos(":"));
for(Index=0;Index<cl->Count;Index++)
{
ci=(TClientInfo *)cl->Items[Index];
if(ci->Name==SendTo)break;
}
if(Index<cl->Count)
{
ci->Socket->SendText(SendFrom+" said to "+SendTo+":"+msg);
}
}
}
ClientList->UnlockList();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ClientSocket1Connect(TObject *Sender,
TCustomWinSocket *Socket)
{
Socket->SendText("User One");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ClientSocket2Connect(TObject *Sender,
TCustomWinSocket *Socket)
{
Socket->SendText("User Two");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ClientSocket3Connect(TObject *Sender,
TCustomWinSocket *Socket)
{
Socket->SendText("User Three");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
ClientSocket1->Socket->SendText("$User Two:asfasdf2");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ClientSocket1Read(TObject *Sender,
TCustomWinSocket *Socket)
{
MsgMemo->Lines->Add(Socket->ReceiveText());
}
//---------------------------------------------------------------------------
constantine 2005-11-14
  • 打赏
  • 举报
回复
再者,ServeSocket这套控件是tcp控件来的,ClientSocket之间不能直接发消息吧,如果是在lan
可以考虑用udp就可以简单直接发,或者都有公网ip也可以直接发,如果很多是内网就不行了
简单的模式可以考虑用serversocket转发给所有客户端,比如说你是群聊,就要告诉server你发的包是群聊的包,让他转给所有人,如果私了,也要告诉server,只要把用户名一起跟上让server知道要发给那个user就可以了
constantine 2005-11-14
  • 打赏
  • 举报
回复
index用来干什么?看不出你这里的index有什么用,简单点说,如果你的结果save在tlist里面
要index,直接用indexof得到就可以了,你自己记录没有用
我不懂电脑 2005-11-14
  • 打赏
  • 举报
回复
ServerSocket1->Socket->Connections[i]->RemoteAddr
获取客户端的ip端口等。

1,317

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder 网络及通讯开发
社区管理员
  • 网络及通讯开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧