我用timer组件实现了接收服务器端的返回数据来进行相应操作..但是总觉得应该有更好的方法来接收服务器发到客户端的数据,希望大家踊跃发言啥都不说,先上代码..我的代码已经可以用,没有错误~~
客户端代码...procedure TFormlogin.ButtonLoginClick(Sender: TObject);
begin
if self.IdTCPClient1.Connected then
idtcpclient1.Disconnect;
if (self.ComboBoxLoginName.Text<>'') and (self.EditPassword.Text<>'') then
begin
IdTCPClient1.Host:=intToStr(ini.ReadInteger('config','ip0',192))+'.'+intToStr(ini.ReadInteger('config','ip1',168))+'.'+intToStr(ini.ReadInteger('config','ip2',1))+'.'+intToStr(ini.ReadInteger('config','ip3',101));
IdTCPClient1.Port:=8088;
IdTCPClient1.Connect;
info.User_Number:=comboboxLoginName.Text;
info.User_Password:=EditPassword.Text;
info.User_IpAddress:=IdIPWatch1.LocalIP;
//self.IdTCPClient1.ReadBuffer(info,sizeof(info));
self.IdTCPClient1.WriteBuffer(info,sizeof(info),true);
end
else
begin
showmessage('用户名或密码不能为空');
end;
end;服务器端代码procedure TformServer.IdTCPServer1Connect(AThread: TIdPeerThread);
begin
//self.StatusBar1.Panels[0].Text:='客户端:'+Athread.Connection.Socket.Binding.PeerIP+'已经连接';
//Athread.Connection.WriteLn('欢迎登录景焦聊天工具');
Athread.Connection.ReadBuffer(info1,sizeof(info1));
self.ADOQuery1.SQL.Clear;
self.ADOQuery1.SQL.Add('select * from users where user_Number='+info1.User_Number+' and user_password="'+info1.User_Password+'"');
self.ADOQuery1.Open;
if not ADOQuery1.IsEmpty then
begin
Athread.Connection.WriteLn('ls');
formlogin.Timer1.Enabled:=true;
end
else
begin
athread.Connection.WriteLn('lf');
formlogin.Timer1.Enabled:=true;
end;
self.ADOQuery1.Close;
end;
客户端代码...procedure TFormlogin.ButtonLoginClick(Sender: TObject);
begin
if self.IdTCPClient1.Connected then
idtcpclient1.Disconnect;
if (self.ComboBoxLoginName.Text<>'') and (self.EditPassword.Text<>'') then
begin
IdTCPClient1.Host:=intToStr(ini.ReadInteger('config','ip0',192))+'.'+intToStr(ini.ReadInteger('config','ip1',168))+'.'+intToStr(ini.ReadInteger('config','ip2',1))+'.'+intToStr(ini.ReadInteger('config','ip3',101));
IdTCPClient1.Port:=8088;
IdTCPClient1.Connect;
info.User_Number:=comboboxLoginName.Text;
info.User_Password:=EditPassword.Text;
info.User_IpAddress:=IdIPWatch1.LocalIP;
//self.IdTCPClient1.ReadBuffer(info,sizeof(info));
self.IdTCPClient1.WriteBuffer(info,sizeof(info),true);
end
else
begin
showmessage('用户名或密码不能为空');
end;
end;服务器端代码procedure TformServer.IdTCPServer1Connect(AThread: TIdPeerThread);
begin
//self.StatusBar1.Panels[0].Text:='客户端:'+Athread.Connection.Socket.Binding.PeerIP+'已经连接';
//Athread.Connection.WriteLn('欢迎登录景焦聊天工具');
Athread.Connection.ReadBuffer(info1,sizeof(info1));
self.ADOQuery1.SQL.Clear;
self.ADOQuery1.SQL.Add('select * from users where user_Number='+info1.User_Number+' and user_password="'+info1.User_Password+'"');
self.ADOQuery1.Open;
if not ADOQuery1.IsEmpty then
begin
Athread.Connection.WriteLn('ls');
formlogin.Timer1.Enabled:=true;
end
else
begin
athread.Connection.WriteLn('lf');
formlogin.Timer1.Enabled:=true;
end;
self.ADOQuery1.Close;
end;
呵呵TIDTCPServer 的 ONExecute 就是一个线程的 OnExecute .在那里处理就行了.
前面判断下Buffer里的字节大小有没在达到你的 Record 的size 到了再 ReadBuffer, 不然会卡住等待的.很影响性能的.