不是我没有自己作,实在是搞不懂,学校封了也不能出去买书看 ; 以下是代码请你们看看
---------------------------------------------------------
Server 端 :
unit Umain;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ScktComp, StdCtrls, Winsock, DB, DBTables, ADODB;
type
TForm1 = class(TForm)
Edit1: TEdit;
Label1: TLabel;
Button1: TButton;
ServerSocket1: TServerSocket;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSocket1ClientError(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
if ServerSocket1.Active = false then ServerSocket1.Open;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
s: string;
begin
s := Edit1.Text;
try
ServerSocket1.Socket.Connections[0].SendText(s);
except
end ;
end;
procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
begin
Label1.Caption := Socket.ReceiveText;
end;
procedure TForm1.ServerSocket1ClientError(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
var
StrErr: string;
begin
case ErrorEvent of
eeConnect: StrErr := '不能建立连接';
eeDisconnect: StrErr := '连接被关闭';
eeReceive: StrErr := '接收数据出错';
eeSend: StrErr := '发送数据出错';
else
StrErr := '连接出错';
end;
ErrorCode := 0;
if ServerSocket1.Active then
ServerSocket1.Active := False;
end;
end.
Client 端 :
object Timer1: TTimer // 默认 True
Interval = 10000
OnTimer = Timer1Timer
Left = 136
Top = 128
end
unit Umain;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ScktComp, ExtCtrls, DB, ADODB;
type
TForm1 = class(TForm)
Edit1: TEdit;
ClientSocket1: TClientSocket;
Label1: TLabel;
Button1: TButton;
Timer1: TTimer;
procedure Button1Click(Sender: TObject);
procedure ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
procedure FormCreate(Sender: TObject);
procedure ClientSocket1Error(Sender: TObject; Socket: TCustomWinSocket;
ErrorEvent: TErrorEvent; var ErrorCode: Integer);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
procedure Connect ;
public
{ Public declarations }
end;
var
Form1: TForm1 ;
t : Cardinal ;
implementation
{$R *.dfm}
procedure TForm1.Connect ;
begin
ClientSocket1.Open ;
end ;
procedure TForm1.Button1Click(Sender: TObject);
begin
ClientSocket1.Socket.SendText(Edit1.Text) ;
t := GetTickCount ;
end;
procedure TForm1.ClientSocket1Read(Sender: TObject;
Socket: TCustomWinSocket);
begin
t := GetTickCount ;
Label1.Caption := Socket.ReceiveText ;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
t := GetTickCount ;
if ClientSocket1.Active = false then
begin
try
Connect ;
except
end ;
end ;
end;
procedure TForm1.ClientSocket1Error(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
var
StrErr: string;
begin
case ErrorEvent of
eeConnect: Connect ;
eeDisconnect: StrErr := '连接被关闭';
eeReceive: StrErr := '接收数据出错';
eeSend: StrErr := '发送数据出错';
else
StrErr := '连接出错';
end;
ErrorCode := 0;
if ClientSocket1.Active then
ClientSocket1.Active := False;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
if Not ClientSocket1.Active then
if GetTickCount - t > 10000 then
begin
ClientSocket1.Active := False ;
Connect ;
end ;
end;
end.
---------------------------------------------------------------------
假设通信正常(服务端已经打开,双方可以正常通信) , 这时关闭 TServerSocket ,
然后重新启动TServerSocket,这时候客户端连接不到服务端了,只有重新启动客户端才能
连接到服务端 ---- 也就是说服务端死机后服务端和客户端都必须重新启动一次 ; 但从
代码看当连接不成功时后反复连接(好像没有错误), 请问为什么 ? 应该怎样解决 ?
---- 上面的代码依然没有解决这个问题,请再指教