W
wangxian111
Unregistered / Unconfirmed
GUEST, unregistred user!
很早以前照书上做的一个聊天工具,其他一切都好就是在运行的时候出现这样的异常:
List index out of bounds(0)
我不知道是什么原因,望指点迷津,谢谢:)
源代码如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Menus, ComCtrls, StdCtrls, Buttons, ScktComp;
type
TForm1 = class(TForm)
ClientSocket1: TClientSocket;
ServerSocket1: TServerSocket;
Memo1: TMemo;
Memo2: TMemo;
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
BitBtn3: TBitBtn;
FontDialog1: TFontDialog;
ColorDialog1: TColorDialog;
StatusBar1: TStatusBar;
MainMenu1: TMainMenu;
File1: TMenuItem;
Listen1: TMenuItem;
Connect1: TMenuItem;
N1: TMenuItem;
N2: TMenuItem;
N4: TMenuItem;
N5: TMenuItem;
N6: TMenuItem;
N7: TMenuItem;
N8: TMenuItem;
N13: TMenuItem;
N14: TMenuItem;
MP31: TMenuItem;
N3: TMenuItem;
N9: TMenuItem;
procedure ServerSocket1Accept(Sender: TObject;
Socket: TCustomWinSocket);
procedure Listen1Click(Sender: TObject);
procedure Connect1Click(Sender: TObject);
procedure ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
procedure Memo1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
procedure ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
procedure ClientSocket1Error(Sender: TObject; Socket: TCustomWinSocket;
ErrorEvent: TErrorEvent; var ErrorCode: Integer);
procedure BitBtn2Click(Sender: TObject);
procedure BitBtn3Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure ServerSocket1ClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure N8Click(Sender: TObject);
procedure N2Click(Sender: TObject);
procedure N3Click(Sender: TObject);
procedure N7Click(Sender: TObject);
procedure N14Click(Sender: TObject);
procedure N9Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
Server: String;
Isserver: Boolean;
implementation
uses Unit2, Unit3, Unit4, Unit5, Unit41;
{$R *.DFM}
procedure TForm1.ServerSocket1Accept(Sender: TObject;
Socket: TCustomWinSocket);
begin
Isserver:=true;
Statusbar1.Panels[0].Text:='Connected to:'+Socket.RemoteAddress;
end;
procedure TForm1.Listen1Click(Sender: TObject);
begin
Listen1.checked:=not listen1.Checked;
If Listen1.Checked then
begin
Clientsocket1.Active:=false;
Serversocket1.Active:=true;
Statusbar1.Panels[0].Text:='监听中……';
end
Else begin
If Serversocket1.Active then
Serversocket1.Active:=false;
Statusbar1.Panels[0].Text:='';
end
end;
procedure TForm1.Connect1Click(Sender: TObject);
begin
If Clientsocket1.Active then
Clientsocket1.Active:=false;
If Inputquery('连接','请输入对方的IP地址:',server)
then
If length(Server)>0 then
with Clientsocket1 do begin
Host:=server;
Active:=true;
end;
end;
procedure TForm1.ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
begin
Statusbar1.Panels[0].Text:='Connected to:'+Socket.remotehost;
Memo1.lines.Clear ;
Memo2.lines.Clear ;
end;
procedure TForm1.Memo1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
If Key=VK_return then
If Isserver then
Serversocket1.Socket.Connections[0].Sendtext(Memo1.Lines[Memo1.Lines.Count-1])
Else
Clientsocket1.Socket.Sendtext(Memo1.Lines[Memo1.Lines.Count-1]);
end;
procedure TForm1.ClientSocket1Read(Sender: TObject;
Socket: TCustomWinSocket);
begin
Memo2.Lines.Add(Socket.Receivetext);
end;
procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
begin
Memo2.Lines.Add(Socket.Receivetext);
end;
procedure TForm1.ClientSocket1Error(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
Memo2.Lines.Add('Error connecting to:'+Server);
Errorcode:=0;
end;
procedure TForm1.BitBtn2Click(Sender: TObject);
begin
If ColorDialog1.Execute then
Memo1.Color:=ColorDialog1.Color;
end;
procedure TForm1.BitBtn3Click(Sender: TObject);
begin
If FontDialog1.Execute then
Memo1.Font:=FontDialog1.Font;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Form2.Show;
end;
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
If Clientsocket1.Active then
Clientsocket1.Active:=false;
If Inputquery('连接','请输入对方的IP地址:',server)
then
If length(Server)>0 then
with Clientsocket1 do begin
Host:=server;
Active:=true;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Listen1click(nil);
end;
procedure TForm1.ServerSocket1ClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
Memo1.lines.Clear ;
Memo2.lines.Clear ;
end;
procedure TForm1.N8Click(Sender: TObject);
begin
If MessageDlg('您真的要退出吗?',mtConfirmation, [mbYes, mbNo], 0) = mrYes then
Close();
end;
procedure TForm1.N2Click(Sender: TObject);
begin
Form3.Show;
end;
procedure TForm1.N3Click(Sender: TObject);
begin
//RMail.Show;
end;
procedure TForm1.N7Click(Sender: TObject);
begin
Server:='';
end;
procedure TForm1.N14Click(Sender: TObject);
begin
//Form4.Show;
end;
procedure TForm1.N9Click(Sender: TObject);
begin
SMail.Show;
end;
end.
List index out of bounds(0)
我不知道是什么原因,望指点迷津,谢谢:)
源代码如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Menus, ComCtrls, StdCtrls, Buttons, ScktComp;
type
TForm1 = class(TForm)
ClientSocket1: TClientSocket;
ServerSocket1: TServerSocket;
Memo1: TMemo;
Memo2: TMemo;
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
BitBtn3: TBitBtn;
FontDialog1: TFontDialog;
ColorDialog1: TColorDialog;
StatusBar1: TStatusBar;
MainMenu1: TMainMenu;
File1: TMenuItem;
Listen1: TMenuItem;
Connect1: TMenuItem;
N1: TMenuItem;
N2: TMenuItem;
N4: TMenuItem;
N5: TMenuItem;
N6: TMenuItem;
N7: TMenuItem;
N8: TMenuItem;
N13: TMenuItem;
N14: TMenuItem;
MP31: TMenuItem;
N3: TMenuItem;
N9: TMenuItem;
procedure ServerSocket1Accept(Sender: TObject;
Socket: TCustomWinSocket);
procedure Listen1Click(Sender: TObject);
procedure Connect1Click(Sender: TObject);
procedure ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
procedure Memo1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
procedure ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
procedure ClientSocket1Error(Sender: TObject; Socket: TCustomWinSocket;
ErrorEvent: TErrorEvent; var ErrorCode: Integer);
procedure BitBtn2Click(Sender: TObject);
procedure BitBtn3Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure ServerSocket1ClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure N8Click(Sender: TObject);
procedure N2Click(Sender: TObject);
procedure N3Click(Sender: TObject);
procedure N7Click(Sender: TObject);
procedure N14Click(Sender: TObject);
procedure N9Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
Server: String;
Isserver: Boolean;
implementation
uses Unit2, Unit3, Unit4, Unit5, Unit41;
{$R *.DFM}
procedure TForm1.ServerSocket1Accept(Sender: TObject;
Socket: TCustomWinSocket);
begin
Isserver:=true;
Statusbar1.Panels[0].Text:='Connected to:'+Socket.RemoteAddress;
end;
procedure TForm1.Listen1Click(Sender: TObject);
begin
Listen1.checked:=not listen1.Checked;
If Listen1.Checked then
begin
Clientsocket1.Active:=false;
Serversocket1.Active:=true;
Statusbar1.Panels[0].Text:='监听中……';
end
Else begin
If Serversocket1.Active then
Serversocket1.Active:=false;
Statusbar1.Panels[0].Text:='';
end
end;
procedure TForm1.Connect1Click(Sender: TObject);
begin
If Clientsocket1.Active then
Clientsocket1.Active:=false;
If Inputquery('连接','请输入对方的IP地址:',server)
then
If length(Server)>0 then
with Clientsocket1 do begin
Host:=server;
Active:=true;
end;
end;
procedure TForm1.ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
begin
Statusbar1.Panels[0].Text:='Connected to:'+Socket.remotehost;
Memo1.lines.Clear ;
Memo2.lines.Clear ;
end;
procedure TForm1.Memo1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
If Key=VK_return then
If Isserver then
Serversocket1.Socket.Connections[0].Sendtext(Memo1.Lines[Memo1.Lines.Count-1])
Else
Clientsocket1.Socket.Sendtext(Memo1.Lines[Memo1.Lines.Count-1]);
end;
procedure TForm1.ClientSocket1Read(Sender: TObject;
Socket: TCustomWinSocket);
begin
Memo2.Lines.Add(Socket.Receivetext);
end;
procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
begin
Memo2.Lines.Add(Socket.Receivetext);
end;
procedure TForm1.ClientSocket1Error(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
Memo2.Lines.Add('Error connecting to:'+Server);
Errorcode:=0;
end;
procedure TForm1.BitBtn2Click(Sender: TObject);
begin
If ColorDialog1.Execute then
Memo1.Color:=ColorDialog1.Color;
end;
procedure TForm1.BitBtn3Click(Sender: TObject);
begin
If FontDialog1.Execute then
Memo1.Font:=FontDialog1.Font;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Form2.Show;
end;
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
If Clientsocket1.Active then
Clientsocket1.Active:=false;
If Inputquery('连接','请输入对方的IP地址:',server)
then
If length(Server)>0 then
with Clientsocket1 do begin
Host:=server;
Active:=true;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Listen1click(nil);
end;
procedure TForm1.ServerSocket1ClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
Memo1.lines.Clear ;
Memo2.lines.Clear ;
end;
procedure TForm1.N8Click(Sender: TObject);
begin
If MessageDlg('您真的要退出吗?',mtConfirmation, [mbYes, mbNo], 0) = mrYes then
Close();
end;
procedure TForm1.N2Click(Sender: TObject);
begin
Form3.Show;
end;
procedure TForm1.N3Click(Sender: TObject);
begin
//RMail.Show;
end;
procedure TForm1.N7Click(Sender: TObject);
begin
Server:='';
end;
procedure TForm1.N14Click(Sender: TObject);
begin
//Form4.Show;
end;
procedure TForm1.N9Click(Sender: TObject);
begin
SMail.Show;
end;
end.