[小弟第一次来]最近招delphi6书上编了一个tcp的聊天程序,调试时编译能通过但是发现exe程序运行时'list index out
of bounds',其结果是服务器端能给客户端发消息,并能列出客户端登陆情况,及昵称改变情况,而客户端却不能给服
务器和别的客户端发消息,请各位大侠帮忙!!程序如下
...全文
38614打赏收藏
tcp的聊天程序‘list index out of bounds’
[小弟第一次来]最近招delphi6书上编了一个tcp的聊天程序,调试时编译能通过但是发现exe程序运行时'list index out of bounds',其结果是服务器端能给客户端发消息,并能列出客户端登陆情况,及昵称改变情况,而客户端却不能给服 务器和别的客户端发消息,请各位大侠帮忙!!程序如下
谢谢各位朋友指点,‘list index out of bounds’问题在do2008(事情做好了,才是事情) 帮助下已经解决了 [服务器端能给客户端发消息,并能列出客户端登陆情况,及昵称改变情况,而客户端却不能给服务器和别的客户端发消息的问题,还没解决,请各位大侠帮忙!!可以在[tcp通信问题续'list index out ‘未发完的代码,大大门只需要在任意一帖中答题一次即可。 ]贴中回答,一样给50分。。。]
目前看,我觉得这一段代码有问题。客户端
if getstring(tmpstr,'^',1)='删除用户@#!'then
begin
for ii:=0 to listview1.Items.Count-1 do
begin
if getstring(tmpstr,'^',5)=listview1.Items[ii].SubItems[2] then
begin
listview1.Items.Delete(ii);
richedit1.Lines.Add(datetostr(date)+''+timetostr(time)+'==>>'
+getstring(tmpstr,'^',2)+'走了!');
end;
end;
end;
改成
if getstring(tmpstr,'^',1)='删除用户@#!'then
begin
for ii:=listview1.Items.Count-1 downto 0 do
begin
if getstring(tmpstr,'^',5)=listview1.Items[ii].SubItems[2] then
begin
listview1.Items.Delete(ii);
richedit1.Lines.Add(datetostr(date)+''+timetostr(time)+'==>>'
+getstring(tmpstr,'^',2)+'走了!');
end;
end;
end;
private
{ Private declarations }
sel_who:integer;
procedure display(msg:string);
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
{$R *.dfm}
procedure tform2.display(msg:string);
begin
statusbar1.SimpleText:=msg;
end;
function getstring(s,mask:string;position:integer):string;
var
str:string;
i,len:integer;
begin
str:='';
for i:=0 to position -1 do
begin
if(pos(mask,s)<=0)then
begin
str:=s;
break;
end;
str:=copy(s,1,pos(mask,s)-1);
len:=length(str);
s:=copy(s,len+2,length(s)-len-1);
end;
result:=str;
end;
procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
serversocket1.Active :=false;
serversocket1.Close;
end;
procedure TForm2.FormCreate(Sender: TObject);
begin
serversocket1.Active:=true;
richedit1.Lines.Add(datetostr(date)+''+timetostr(time)+'==>>'+
'yyd聊天服务器启动!');
end;
procedure TForm2.ComboBox1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if (key=vk_return)and(combobox1.Text<>'')then
begin
combobox1.Items.Add(combobox1.Text);
serversocket1.Socket.Connections[sel_who].SendText('交谈@#!^'+
label3.Caption+'说:'+combobox1.Text+'^');
richedit1.Lines.Add(label3.Caption+'说:'+combobox1.Text);
combobox1.Text:='';
end;
end;
procedure TForm2.ComboBox1Select(Sender: TObject);
begin
if combobox1.Text<>'' then
begin
combobox1.Items.Add(combobox1.Text);
serversocket1.Socket.Connections[sel_who].SendText('交谈@#!^'+
label3.Caption+'说:'+combobox1.Text);
richedit1.Lines.Add(label3.Caption+'说:'+combobox1.Text);
combobox1.Text:='';
end;
end;
procedure TForm1.N5Click(Sender: TObject);
var
tmpstr:string;
begin
tmpstr:=inputbox('昵称输入','请输入用户昵称:','');
if tmpstr<>''then
label3.Caption:=tmpstr;
clientsocket1.Socket.SendText('昵称@#!^'+label3.Caption);
end;
procedure TForm1.N7Click(Sender: TObject);
begin
close;
end;
procedure TForm1.N10Click(Sender: TObject);
begin
showmessage('mailto:yydyyd3@yahoo.com.cn');
//shellexecute(handle,nil,'mailto:yydyyd3@yahoo.com.cn',nil,
//nil,sw_normal);
end;
procedure TForm1.N11Click(Sender: TObject);
begin
showmessage('http//yyd3.com.cn');
//shellexecute(handle,nil,'http//yyd3.com.cn',nil,
//nil,sw_normal);
end;
procedure TForm1.N12Click(Sender: TObject);
begin
showmessage('帮助请见个人主页');
//shellexecute(handle,nil,'帮助.txt',nil ,
//nil,sw_normal);
end;
procedure TForm1.ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
begin
statusbar1.Panels.Items[0].Text:='连接成功!';
end;
procedure TForm1.ClientSocket1Read(Sender: TObject;
Socket: TCustomWinSocket);
var
tmpstr:string;
newitem:tlistitem;
ii:integer;
exist:bool;
begin
exist:=false;
tmpstr:=socket.ReceiveText;
if getstring(tmpstr,'^',1)='昵称@#!'then
begin
for ii:=0 to listview1.Items.Count-1 do
begin
if getstring(tmpstr,'^',5)=listview1.Items[ii].SubItems[2]
then
begin
exist:=true;
if getstring(tmpstr,'^',2)<>listview1.Items[ii].Caption then
begin
richedit1.Lines.Add(datetostr(date)+''+timetostr(time)+
'==>>'+listview1.Items[ii].Caption+'更名为'+
getstring(tmpstr,'^',2));
listview1.Items[ii].Caption:=getstring(tmpstr,'^',2);
end;
end;
end;
end;
if getstring(tmpstr,'^',1)='交谈@#!'then
begin
richedit1.Lines.Add(getstring(tmpstr,'^',2));
end;
if getstring(tmpstr,'^',1)='增加用户@#!'then
begin
for ii:=0 to listview1.Items.Count-1 do
begin
if getstring(tmpstr,'^',5)=listview1.Items[ii].SubItems[2] then
exist:=true;
end;
if exist=false then begin
newitem:=listview1.Items.Add();
newitem.Caption:=getstring(tmpstr,'^',2);
newitem.SubItems.Add(getstring(tmpstr,'^',3));
newitem.SubItems.Add(getstring(tmpstr,'^',4));
newitem.SubItems.Add(getstring(tmpstr,'^',5));
richedit1.Lines.Add(datetostr(date)+''+timetostr(time)+'==>>'+
getstring(tmpstr,'^',2)+'加入进来!');
end;
end;
if getstring(tmpstr,'^',1)='删除用户@#!'then
begin
for ii:=0 to listview1.Items.Count-1 do
begin
if getstring(tmpstr,'^',5)=listview1.Items[ii].SubItems[2] then
begin
listview1.Items.Delete(ii);
richedit1.Lines.Add(datetostr(date)+''+timetostr(time)+'==>>'
+getstring(tmpstr,'^',2)+'走了!');
end;
end;
end;
end;
end.
function getstring(s,mask:string;position:integer):string;
var
str:string;
i,len:integer;
begin
str:='';
for i:=0 to position -1 do
begin
if(pos(mask,s)<=0)then
begin
str:=s;
break;
end;
str:=copy(s,1,pos(mask,s)-1);
len:=length(str);
s:=copy(s,len+2,length(s)-len-1);
end;
result:=str;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
clientsocket1.Active:=false;
clientsocket1.Close;
end;
procedure TForm1.ComboBox1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if (key=vk_return)and(combobox1.Text<>'')then
begin
combobox1.Items.Add(combobox1.Text);
clientsocket1.Socket.SendText('交谈@#!^'+label3.Caption+'说:'
+combobox1.Text);
combobox1.Text:='';
end;
end;
procedure TForm1.ComboBox1Select(Sender: TObject);
begin
if combobox1.Text<>'' then
begin
clientsocket1.Socket.SendText('交谈@#!^'+label3.Caption+'说:'
+combobox1.Text);
combobox1.Text:='';
end;
end;
procedure TForm1.connectbtnClick(Sender: TObject);
var
remotehost:string;
begin
remotehost:=inputbox('建立连接','请输入对方的IP地址或名称:'
,'');
if trim(remotehost)<>'' then
begin
if clientsocket1.Active then clientsocket1.Active:=true;
clientsocket1.Port:=6666;
clientsocket1.Host:=remotehost;
try
clientsocket1.Active:=true;
except
showmessage('连接失败');
end;
end;
end;
procedure TForm1.disconnectbtnClick(Sender: TObject);
begin
clientsocket1.Active:=false;
richedit1.Lines.Add(datetostr(date)+''+timetostr(time)
+'==>>'+'我不聊了!');
listview1.Clear;
end;
procedure TForm1.namebtnClick(Sender: TObject);
var
tmpstr:string;
begin
tmpstr:=inputbox('昵称输入','请输入用户昵称:','');
if tmpstr<>''then
label3.Caption:=tmpstr;
clientsocket1.Socket.SendText('昵称@#!^'+label3.Caption);
end;
procedure TForm1.aboutbtnClick(Sender: TObject);
begin
showmessage('yyd聊天工具1.0版');
//shellabout(handle,'yyd聊天工具',#13+'yyd聊天工具1。0版'+#13
//+'欢迎使用!',0);
end;
procedure TForm1.quitbtnClick(Sender: TObject);
begin
close;
end;
procedure TForm1.N2Click(Sender: TObject);
var
remotehost:string;
begin
remotehost:=inputbox('建立连接','请输入对方的IP地址或名称:'
,'');
if trim(remotehost)<>'' then
begin
if clientsocket1.Active then clientsocket1.Active:=true;
clientsocket1.Port:=6666;
clientsocket1.Host:=remotehost;
try
clientsocket1.Active:=true;
except
showmessage('连接失败');
end;
end;
end;
procedure TForm1.N9Click(Sender: TObject);
begin
showmessage('yyd聊天工具1.0版');
//shellabout(handle,'yyd聊天工具',#13+'yyd聊天工具1。0版'+#13
//+'欢迎使用!',0);
end;
procedure TForm1.N3Click(Sender: TObject);
begin
clientsocket1.Active:=false;
richedit1.Lines.Add(datetostr(date)+''+timetostr(time)
+'==>>'+'我不聊了!');
listview1.Clear;
end;