请问:我用Socket做的一个简单聊天程序,为什么监听后有List index out of bounds(0)?哪儿错了呢? ( 积分: 0 )

  • 主题发起人 主题发起人 xkdcc
  • 开始时间 开始时间
X

xkdcc

Unregistered / Unconfirmed
GUEST, unregistred user!
源代码如下(窗体上有三个按钮:ListenButton,ConnectButton,DisconnectButton;一个ServerSocket和一个ClientSocket)
procedure TForm1.ListenButtonClick(Sender: TObject);
begin

if ServerSocket.Active then
begin
ServerSocket.Active := False;
StatusBar1.Panels[0].Text := '';
end;

if InputQuery('Port to listen','Port:',ListenPort) then
ServerSocket.Port:=StrtoInt(ListenPort);
ClientSocket.Active := False;
ServerSocket.Active := True;
StatusBar1.Panels[0].Text := 'Listening @'+ListenPort+' ...';
end;

procedure TForm1.ConnectButtonClick(Sender: TObject);
begin
if ClientSocket.Active then
begin
ClientSocket.Active := False;
StatusBar1.Panels[0].Text := '';
end;
if InputQuery('Computer to connect to','Address Name:',Server) then
if InputQuery('Port to connect to','Port:',ConnectPort) then
if Length(Server)>0 then
with ClientSocketdo
begin
Host := Server;
Port := StrtoInt(ConnectPort);
Active := True;
end;
end;

procedure TForm1.DisconnectButtonClick(Sender: TObject);
begin
ClientSocket.Active := False;
StatusBar1.Panels[0].Text := '';
end;

procedure TForm1.ServerSocketAccept(Sender: TObject;
Socket: TCustomWinSocket);
begin
IsServer := True;
StatusBar1.Panels[0].Text := 'Accept connection from:' + Socket.RemoteAddress;
end;

procedure TForm1.ServerSocketClientRead(Sender: TObject;
Socket: TCustomWinSocket);
begin
Memo2.Lines.Add(Socket.ReceiveText);
end;

procedure TForm1.ServerSocketClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
StatusBar1.Panels[0].Text := 'Listening ...';
end;

procedure TForm1.ServerSocketClientError(Sender: TObject;
Socket: TCustomWinSocket;
ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
ShowMessage('ErrorCode:' + InttoStr(ErrorCode));
end;

procedure TForm1.ClientSocketConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
StatusBar1.Panels[0].Text := 'Connected to:' + Socket.RemoteHost;
end;

procedure TForm1.ClientSocketRead(Sender: TObject;
Socket: TCustomWinSocket);
begin
Memo2.Lines.Add(Socket.ReceiveText);
end;

procedure TForm1.ClientSocketError(Sender: TObject;
Socket: TCustomWinSocket;
ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
Memo2.Lines.Add('Error connecting to:' + Server);
ErrorCode :=0;
end;

procedure TForm1.Memo1KeyDown(Sender: TObject;
var Key: Word;
Shift: TShiftState);
begin
if Key = VK_RETURN then
if IsServer then

ServerSocket.Socket.Connections[0].SendText(Memo1.Lines[Memo1.Lines.Count - 1])
else

ClientSocket.Socket.SendText(Memo1.Lines[Memo1.Lines.count - 1]);
end;
 
源代码如下(窗体上有三个按钮:ListenButton,ConnectButton,DisconnectButton;一个ServerSocket和一个ClientSocket)
procedure TForm1.ListenButtonClick(Sender: TObject);
begin

if ServerSocket.Active then
begin
ServerSocket.Active := False;
StatusBar1.Panels[0].Text := '';
end;

if InputQuery('Port to listen','Port:',ListenPort) then
ServerSocket.Port:=StrtoInt(ListenPort);
ClientSocket.Active := False;
ServerSocket.Active := True;
StatusBar1.Panels[0].Text := 'Listening @'+ListenPort+' ...';
end;

procedure TForm1.ConnectButtonClick(Sender: TObject);
begin
if ClientSocket.Active then
begin
ClientSocket.Active := False;
StatusBar1.Panels[0].Text := '';
end;
if InputQuery('Computer to connect to','Address Name:',Server) then
if InputQuery('Port to connect to','Port:',ConnectPort) then
if Length(Server)>0 then
with ClientSocketdo
begin
Host := Server;
Port := StrtoInt(ConnectPort);
Active := True;
end;
end;

procedure TForm1.DisconnectButtonClick(Sender: TObject);
begin
ClientSocket.Active := False;
StatusBar1.Panels[0].Text := '';
end;

procedure TForm1.ServerSocketAccept(Sender: TObject;
Socket: TCustomWinSocket);
begin
IsServer := True;
StatusBar1.Panels[0].Text := 'Accept connection from:' + Socket.RemoteAddress;
end;

procedure TForm1.ServerSocketClientRead(Sender: TObject;
Socket: TCustomWinSocket);
begin
Memo2.Lines.Add(Socket.ReceiveText);
end;

procedure TForm1.ServerSocketClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
StatusBar1.Panels[0].Text := 'Listening ...';
end;

procedure TForm1.ServerSocketClientError(Sender: TObject;
Socket: TCustomWinSocket;
ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
ShowMessage('ErrorCode:' + InttoStr(ErrorCode));
end;

procedure TForm1.ClientSocketConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
StatusBar1.Panels[0].Text := 'Connected to:' + Socket.RemoteHost;
end;

procedure TForm1.ClientSocketRead(Sender: TObject;
Socket: TCustomWinSocket);
begin
Memo2.Lines.Add(Socket.ReceiveText);
end;

procedure TForm1.ClientSocketError(Sender: TObject;
Socket: TCustomWinSocket;
ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
Memo2.Lines.Add('Error connecting to:' + Server);
ErrorCode :=0;
end;

procedure TForm1.Memo1KeyDown(Sender: TObject;
var Key: Word;
Shift: TShiftState);
begin
if Key = VK_RETURN then
if IsServer then

ServerSocket.Socket.Connections[0].SendText(Memo1.Lines[Memo1.Lines.Count - 1])
else

ClientSocket.Socket.SendText(Memo1.Lines[Memo1.Lines.count - 1]);
end;
 
追踪一下代码,应该是Panels[0]或者Connections[0]这里的问题,超出范围了
 
数据列表中没有项目,而程序试图读取第一个数据,就会出现这样的错误提示
!
 
cjf1009说得对哈,我知道该是StatusBar1.SimpleText
因为我得StatusBar没有panels:)
 
跟踪,应该是数组访问越界
 

Similar threads

后退
顶部