关于ServerSocket/ClientSocket的并发和接收问题
我写了一个c/s模式的服务器客户端
server要向某个用户发送消息我写了一个函数根据用户的handle值来分别发送
//对某个用户发送信息
void SomeSend(AnsiString Msg, int SomeHandle)
{
for(int i = 0 ; i < Form1->ServerSocket1->Socket->ActiveConnections; i ++ )
{
if( Form1->ServerSocket1->Socket->Connections[i]->SocketHandle == SomeHandle )
{
Form1->ServerSocket1->Socket->Connections[i]->SendText(Msg);//发送信息
break;
}
}
}
----------------------------------------------------------------------------------------
然后在主程序server的Read里用
int ClientHandle;
ClientHandle = Socket->SocketHandle;来记录某个用户发送消息来时的handle值.在对应返回消息
因为客户端异常退出的话,server端和client(断开)的连接还存在着.
所以我用了2个timer来对用户发送消息
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
for( int i = 0; i < ListView1->Items->Count ; i ++ )
{
if( ListView1->Items->Item[i]->SubItems->Strings[4] == "Yes" )
{
ListView1->Items->Item[i]->SubItems->Strings[4] = "No";//如果在线,设为不在线
}
Form1->ServerSocket1->Socket->Connections[i]->SendText(JiaMi("GetYesNo"));//并且对所有用户发送消息
}
}
void __fastcall TForm1::Timer2Timer(TObject *Sender)
{
int Timers;
for( int i = 0; i < ListView1->Items->Count ; i ++ )
{
if( ListView1->Items->Item[i]->SubItems->Strings[4] == "No")
{
if( ListView1->Items->Item[i]->SubItems->Strings[5] == Edit18->Text )
{
//如果用户超过2次无返回消息回来,证明已经死机
ServerSocket1->Socket->Connections[i]->Close();//关闭该用户
SomePlayerBoolAdd(); //重新排列用户ID表
}
else
{
Timers = StrToInt(ListView1->Items->Item[i]->SubItems->Strings[5]);
Timers++;
ListView1->Items->Item[i]->SubItems->Strings[5] = IntToStr(Timers);
}
}
}
}
----------------------------------------------------------------------------------------
现在我的问题是这样的.
比如说我的ListView1->Items->Count有大约5000个人.
那么timer1对5000个人发送消息后
5000个人肯定会返回消息给我.
那么我这里取出的handle会不会乱掉?
由于我刚开始一直是在本机测试,所以只登陆了大约30个人左右`,没问题~.要是大概有5000个人的话`会不会有问题了?
纯属讨论~~
int ClientHandle;
ClientHandle = Socket->SocketHandle;