R
rezin
Unregistered / Unconfirmed
GUEST, unregistred user!
服务器端
1.打开按钮事件
ServerSocket.Open ;
2.procedure Tfrm_main.ServerSocketClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
var
i,j:integer;
begin
j:=-1;
frm_main.Caption := inttostr(sessions);
for i:=0 to sessionsdo
begin
//在原有的客户端连接数组中有中断的客户端连接
if not session.CUsed then
begin
session.CHandle := Socket.SocketHandle ;//客户端套接字句柄
session.CSocket := Socket;
//客户端套接字
session.CName := Socket.RemoteHost ;
//客户端计算机名称
session.CAddress := Socket.RemoteAddress ;//客户端计算机IP
session.CUsed := True;
//连接数组当前位置已经占用
session.CTime := now;
session.CNum := 0;
//当有新客户段连接上时,将其连接信息添加到列表中
ASG.Cells[0,i+1]:=inttostr(i+1);
ASG.Cells[1,i+1]:=Socket.RemoteHost;
ASG.Cells[2,i+1]:=Socket.RemoteAddress;
frm_main.Caption := frm_main.Caption+inttostr(i);
j := 0;
if session.CUsed then
ASG.Cells[5,i+1]:= '已连接'
else
ASG.Cells[5,i+1]:= '未连接';
Break;
end;
end ;
if j=-1 then
begin
inc(sessions);
i := sessions;
session.CHandle := Socket.SocketHandle ;
session.CSocket := Socket;
session.CName := Socket.RemoteHost ;
session.CAddress := Socket.RemoteAddress ;
session.CUsed := True;
session.CTime := now;
session.CNum := 1;
asg.AddRow ;
ASG.Cells[0,i+1]:=inttostr(i+1);
ASG.Cells[1,i+1]:=Socket.RemoteHost;
ASG.Cells[2,i+1]:=Socket.RemoteAddress;
frm_main.Caption := frm_main.Caption + 'b';
if session.CUsed then
ASG.Cells[5,i+1]:= '已连接'
else
ASG.Cells[5,i+1]:= '未连接';
end;
end;
3.//当客户端断开连接时
procedure Tfrm_main.ServerSocketClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
var
i,j:integer;
begin
for i:=0 to sessionsdo
begin
if (session.CHandle = Socket.SocketHandle) and session.CUsed =true then
begin
session.CHandle :=0;
session.CUsed := False;
Break;
end;
end;
//删除断开连接的机器在列表中的信息
end;
二 客户段就写一个断开按钮
clientsocket.close;
问题是:
1.在客户段连接时正常,
2.如果只用一个客户段也正常,但是同时存在两个以上客户段时,按照我下面的操作方式,服务器段会抱 Access violation at address ********.Read of adress ********错误
操作方式如下:
同时打开两个客户端,然后将第一个客户段关闭在打开,反复操作几次,在到另外的机器上关闭另一个客户端时就会报上述错误
1.打开按钮事件
ServerSocket.Open ;
2.procedure Tfrm_main.ServerSocketClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
var
i,j:integer;
begin
j:=-1;
frm_main.Caption := inttostr(sessions);
for i:=0 to sessionsdo
begin
//在原有的客户端连接数组中有中断的客户端连接
if not session.CUsed then
begin
session.CHandle := Socket.SocketHandle ;//客户端套接字句柄
session.CSocket := Socket;
//客户端套接字
session.CName := Socket.RemoteHost ;
//客户端计算机名称
session.CAddress := Socket.RemoteAddress ;//客户端计算机IP
session.CUsed := True;
//连接数组当前位置已经占用
session.CTime := now;
session.CNum := 0;
//当有新客户段连接上时,将其连接信息添加到列表中
ASG.Cells[0,i+1]:=inttostr(i+1);
ASG.Cells[1,i+1]:=Socket.RemoteHost;
ASG.Cells[2,i+1]:=Socket.RemoteAddress;
frm_main.Caption := frm_main.Caption+inttostr(i);
j := 0;
if session.CUsed then
ASG.Cells[5,i+1]:= '已连接'
else
ASG.Cells[5,i+1]:= '未连接';
Break;
end;
end ;
if j=-1 then
begin
inc(sessions);
i := sessions;
session.CHandle := Socket.SocketHandle ;
session.CSocket := Socket;
session.CName := Socket.RemoteHost ;
session.CAddress := Socket.RemoteAddress ;
session.CUsed := True;
session.CTime := now;
session.CNum := 1;
asg.AddRow ;
ASG.Cells[0,i+1]:=inttostr(i+1);
ASG.Cells[1,i+1]:=Socket.RemoteHost;
ASG.Cells[2,i+1]:=Socket.RemoteAddress;
frm_main.Caption := frm_main.Caption + 'b';
if session.CUsed then
ASG.Cells[5,i+1]:= '已连接'
else
ASG.Cells[5,i+1]:= '未连接';
end;
end;
3.//当客户端断开连接时
procedure Tfrm_main.ServerSocketClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
var
i,j:integer;
begin
for i:=0 to sessionsdo
begin
if (session.CHandle = Socket.SocketHandle) and session.CUsed =true then
begin
session.CHandle :=0;
session.CUsed := False;
Break;
end;
end;
//删除断开连接的机器在列表中的信息
end;
二 客户段就写一个断开按钮
clientsocket.close;
问题是:
1.在客户段连接时正常,
2.如果只用一个客户段也正常,但是同时存在两个以上客户段时,按照我下面的操作方式,服务器段会抱 Access violation at address ********.Read of adress ********错误
操作方式如下:
同时打开两个客户端,然后将第一个客户段关闭在打开,反复操作几次,在到另外的机器上关闭另一个客户端时就会报上述错误