tcp的聊天程序‘list index out of bounds’

yydyyd3 2006-11-11 05:29:06
[小弟第一次来]最近招delphi6书上编了一个tcp的聊天程序,调试时编译能通过但是发现exe程序运行时'list index out

of bounds',其结果是服务器端能给客户端发消息,并能列出客户端登陆情况,及昵称改变情况,而客户端却不能给服

务器和别的客户端发消息,请各位大侠帮忙!!程序如下
...全文
386 14 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
yydyyd3 2006-11-13
  • 打赏
  • 举报
回复
你可以留个地址,我把原程序发给你,帮我看看
yydyyd3 2006-11-13
  • 打赏
  • 举报
回复
兄弟,俺就是不太懂这个。。。郁闷
yydyyd3 2006-11-13
  • 打赏
  • 举报
回复
谢谢各位朋友指点,‘list index out of bounds’问题在do2008(事情做好了,才是事情) 帮助下已经解决了 [服务器端能给客户端发消息,并能列出客户端登陆情况,及昵称改变情况,而客户端却不能给服务器和别的客户端发消息的问题,还没解决,请各位大侠帮忙!!可以在[tcp通信问题续'list index out ‘未发完的代码,大大门只需要在任意一帖中答题一次即可。 ]贴中回答,一样给50分。。。]
gogogo 2006-11-13
  • 打赏
  • 举报
回复
看来楼上的正确了,还有这种事,LZ够。。。。。。。。。
do2008 2006-11-13
  • 打赏
  • 举报
回复
看了你的代码,问题了出在客户端:

procedure TForm1.ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
begin
statusbar1.Panels.Items[0].Text:='连接成功!';//这一句
end;

你的statusbar1没有添加任何的StatusPanel,也就是说状态栏中没有分栏,因此:

//statusbar1.Panels.Items[0].Text:='连接成功!';这一句改为下面这一句:
statusbar1.SimpleText:='连接成功!';
dabaicai 2006-11-13
  • 打赏
  • 举报
回复
晕倒,你自己命名控件了,还不加以注释,难道让我还来分析代码啊,没仔细看

从你说的错误上是越界溢出问题,根据你说的情况,应该是你客户端发送信息,服务器接受和处理这块的问题(或者干脆就是客户端就不知道给谁发)
gogogo 2006-11-13
  • 打赏
  • 举报
回复
变化在第3行
gogogo 2006-11-13
  • 打赏
  • 举报
回复
目前看,我觉得这一段代码有问题。客户端
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;
gogogo 2006-11-13
  • 打赏
  • 举报
回复
断点调试,单步不会?如果这些都不知到,这个程序能写成这种程度,佩服你。
gogogo 2006-11-11
  • 打赏
  • 举报
回复
静态数组越界,动态数组没有分配数据,第二种情况也是很有可能的。
gogogo 2006-11-11
  • 打赏
  • 举报
回复
断点调试,找到触发错误的位置,单步调试查看变量的值是不是你想象的内容,从错误信息看,多数是某个数组下标越界,代码太多,没有仔细看,你调试看看,应该更容易找到错误。
yydyyd3 2006-11-11
  • 打赏
  • 举报
回复
服务器端:
unit Unit2;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, ToolWin, Menus, StdCtrls, ExtCtrls, ScktComp, ImgList;

type
TForm2 = class(TForm)
MainMenu1: TMainMenu;
N1: TMenuItem;
N2: TMenuItem;
N3: TMenuItem;
N4: TMenuItem;
N5: TMenuItem;
N6: TMenuItem;
N7: TMenuItem;
N8: TMenuItem;
N9: TMenuItem;
N10: TMenuItem;
N11: TMenuItem;
N12: TMenuItem;
ToolBar1: TToolBar;
startbtn: TToolButton;
namebtn: TToolButton;
stopbtn: TToolButton;
aboutbtn: TToolButton;
quitbtn: TToolButton;
ServerSocket1: TServerSocket;
StatusBar1: TStatusBar;
Panel1: TPanel;
Panel2: TPanel;
RichEdit1: TRichEdit;
Panel3: TPanel;
ListView1: TListView;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
ComboBox1: TComboBox;
ImageList1: TImageList;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormCreate(Sender: TObject);
procedure ComboBox1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure ComboBox1Select(Sender: TObject);
procedure ListView1SelectItem(Sender: TObject; Item: TListItem;
Selected: Boolean);
procedure startbtnClick(Sender: TObject);
procedure stopbtnClick(Sender: TObject);
procedure namebtnClick(Sender: TObject);
procedure aboutbtnClick(Sender: TObject);
procedure quitbtnClick(Sender: TObject);
procedure N10Click(Sender: TObject);
procedure N12Click(Sender: TObject);
procedure N9Click(Sender: TObject);
procedure N8Click(Sender: TObject);
procedure N6Click(Sender: TObject);
procedure N5Click(Sender: TObject);
procedure N4Click(Sender: TObject);
procedure N2Click(Sender: TObject);
procedure ServerSocket1Accept(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSocket1ClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);

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;
yydyyd3 2006-11-11
  • 打赏
  • 举报
回复
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.
yydyyd3 2006-11-11
  • 打赏
  • 举报
回复
客户端:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ScktComp, StdCtrls, ExtCtrls, ComCtrls, ImgList, ToolWin, Menus;
type
TForm1 = class(TForm)
MainMenu1: TMainMenu;
N1: TMenuItem;
N2: TMenuItem;
N3: TMenuItem;
N4: TMenuItem;
N5: TMenuItem;
N6: TMenuItem;
N7: TMenuItem;
N8: TMenuItem;
N9: TMenuItem;
N10: TMenuItem;
N11: TMenuItem;
N12: TMenuItem;
ToolBar1: TToolBar;
ImageList1: TImageList;
connectbtn: TToolButton;
disconnectbtn: TToolButton;
namebtn: TToolButton;
aboutbtn: TToolButton;
quitbtn: TToolButton;
Panel1: TPanel;
GroupBox1: TGroupBox;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
ComboBox1: TComboBox;
ClientSocket1: TClientSocket;
StatusBar1: TStatusBar;
Panel2: TPanel;
RichEdit1: TRichEdit;
ListView1: TListView;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure ComboBox1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure ComboBox1Select(Sender: TObject);
procedure connectbtnClick(Sender: TObject);
procedure disconnectbtnClick(Sender: TObject);
procedure namebtnClick(Sender: TObject);
procedure aboutbtnClick(Sender: TObject);
procedure quitbtnClick(Sender: TObject);
procedure N2Click(Sender: TObject);
procedure N9Click(Sender: TObject);
procedure N3Click(Sender: TObject);
procedure N5Click(Sender: TObject);
procedure N7Click(Sender: TObject);
procedure N10Click(Sender: TObject);
procedure N11Click(Sender: TObject);
procedure N12Click(Sender: TObject);
procedure ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;

implementation

{$R *.dfm}

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;

1,594

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 网络通信/分布式开发
社区管理员
  • 网络通信/分布式开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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