Z
zjlzd
Unregistered / Unconfirmed
GUEST, unregistred user!
TClientSocket<==>TServerSocket,情况是在网络故障恢复后,Client无法Connect到Server,此时Client端口状态CLOSE_WAIT,Server端FIN_WAIT2.(200分)<br />客户端线程: MySock:TClientSocket==
While not Terminated do
begin
if (MySock.Socket.Connected) and (MySock.Active){多余?}then
begin
try
pSocketStream :=TWinSocketStream.Create(MySock.Socket, 60000);
pSocketStream.Write(tmpStr, Length(str)+1);
if( not pSocketStream.WaitForData(1 * 60 * 1000 )) then
begin
MySock.Socket.Close;
Sleep(2000);
MySock.Socket.Open('',string(pub.ServerIP),'',pub.ServerPort);
//此处若为MySock.Active:=True;将会异常.
Continue;
end;
nTemp := pSocketStream.Read(buffer, 2);//---接收应答...
except
try
if pSocketStream<>nil then pSocketStream.Free;
pSocketStream:=nil;
except
end;
MySock.Socket.Close;
Sleep(2000);
try
MySock.Socket.Open('',string(pub.ServerIP),'',pub.ServerPort);
//此处若为MySock.Active:=True;将会异常.
except
end;
Continue;
end;
pSocketStream.Free;
pSocketStream:=nil;
end else
begin
MySock.Socket.Close;
Sleep(2000);
try
MySock.Socket.Open('',string(pub.ServerIP),'',pub.ServerPort);
except
end;
end;
end;
end;
Server端: TStockServerThread = class(TServerClientThread)
procedure TStockServerThread.ClientExecute;
begin
if ( not Terminated and ClientSocket.Connected ) then
begin
try
try
pSocketStream := TWinSocketStream.Create(ClientSocket, 60000);
while DoClientRequest() do;
finally
pSocketStream.Free;
pSocketStream :=nil;
ClientSocket.Close;
end;
except
ClientSocket.Close;
end;
end;
end;
function TStockServerThread.DoClientRequest:boolean;
begin
if ( not ReadClient() ) then
Result:=False
else begin
////////////
Result:=True;
end;
end;
function TStockServerThread.ReadClient: boolean;
var buffer:array [0..200] of Char;
strLast,str,tmpStr,strResult:string;
crStr:array[0..10] of Char;
okStr:array[0..2] of Char;
nTemp:integer;
bIsSucc:boolean;
begin
nTemp:=0;
okStr[0]:='O'; okStr[1]:='K'; okStr[2]:=Chr(0);
crStr[0]:=Chr(13); crStr[1]:=Chr(10); crStr[2]:=Chr(0);
tmpStr:=string(crStr);
ReadBuffer:='';
strLast:='';
//////////////////////////
bIsSucc:=False;
while( True ) do
begin
if (Terminated or ( not ClientSocket.Connected )) then break;
nTemp := Pos(tmpStr,strLast);
if nTemp>0 then
begin
strResult := Copy(strLast, 1,nTemp - 1 );
ReadBuffer:=ReadBuffer+strResult;
bIsSucc := true;
break;
end;
{======检查是否有数据返回;1分钟没有数据则回头重新等待并检测状态?=====}
try
if( not pSocketStream.WaitForData(1 * 60 * 1000 )) then Continue; //break;
FillChar(buffer, 200, 0);
nTemp := pSocketStream.Read(buffer, 99 );
if ( nTemp > 0 ) then
begin
buffer[ nTemp ] := Chr(0);
str:=String(buffer);
strLast:=strLast+str;
pSocketStream.WriteBuffer(okStr, 2 ); //收到写回应。
end
else
break;
except
ClientSocket.Close;
Terminate;
end;
end;
if ( not bIsSucc ) then
begin
ClientSocket.Close;
Terminate;
end;
Result:=bIsSucc;
end;
While not Terminated do
begin
if (MySock.Socket.Connected) and (MySock.Active){多余?}then
begin
try
pSocketStream :=TWinSocketStream.Create(MySock.Socket, 60000);
pSocketStream.Write(tmpStr, Length(str)+1);
if( not pSocketStream.WaitForData(1 * 60 * 1000 )) then
begin
MySock.Socket.Close;
Sleep(2000);
MySock.Socket.Open('',string(pub.ServerIP),'',pub.ServerPort);
//此处若为MySock.Active:=True;将会异常.
Continue;
end;
nTemp := pSocketStream.Read(buffer, 2);//---接收应答...
except
try
if pSocketStream<>nil then pSocketStream.Free;
pSocketStream:=nil;
except
end;
MySock.Socket.Close;
Sleep(2000);
try
MySock.Socket.Open('',string(pub.ServerIP),'',pub.ServerPort);
//此处若为MySock.Active:=True;将会异常.
except
end;
Continue;
end;
pSocketStream.Free;
pSocketStream:=nil;
end else
begin
MySock.Socket.Close;
Sleep(2000);
try
MySock.Socket.Open('',string(pub.ServerIP),'',pub.ServerPort);
except
end;
end;
end;
end;
Server端: TStockServerThread = class(TServerClientThread)
procedure TStockServerThread.ClientExecute;
begin
if ( not Terminated and ClientSocket.Connected ) then
begin
try
try
pSocketStream := TWinSocketStream.Create(ClientSocket, 60000);
while DoClientRequest() do;
finally
pSocketStream.Free;
pSocketStream :=nil;
ClientSocket.Close;
end;
except
ClientSocket.Close;
end;
end;
end;
function TStockServerThread.DoClientRequest:boolean;
begin
if ( not ReadClient() ) then
Result:=False
else begin
////////////
Result:=True;
end;
end;
function TStockServerThread.ReadClient: boolean;
var buffer:array [0..200] of Char;
strLast,str,tmpStr,strResult:string;
crStr:array[0..10] of Char;
okStr:array[0..2] of Char;
nTemp:integer;
bIsSucc:boolean;
begin
nTemp:=0;
okStr[0]:='O'; okStr[1]:='K'; okStr[2]:=Chr(0);
crStr[0]:=Chr(13); crStr[1]:=Chr(10); crStr[2]:=Chr(0);
tmpStr:=string(crStr);
ReadBuffer:='';
strLast:='';
//////////////////////////
bIsSucc:=False;
while( True ) do
begin
if (Terminated or ( not ClientSocket.Connected )) then break;
nTemp := Pos(tmpStr,strLast);
if nTemp>0 then
begin
strResult := Copy(strLast, 1,nTemp - 1 );
ReadBuffer:=ReadBuffer+strResult;
bIsSucc := true;
break;
end;
{======检查是否有数据返回;1分钟没有数据则回头重新等待并检测状态?=====}
try
if( not pSocketStream.WaitForData(1 * 60 * 1000 )) then Continue; //break;
FillChar(buffer, 200, 0);
nTemp := pSocketStream.Read(buffer, 99 );
if ( nTemp > 0 ) then
begin
buffer[ nTemp ] := Chr(0);
str:=String(buffer);
strLast:=strLast+str;
pSocketStream.WriteBuffer(okStr, 2 ); //收到写回应。
end
else
break;
except
ClientSocket.Close;
Terminate;
end;
end;
if ( not bIsSucc ) then
begin
ClientSocket.Close;
Terminate;
end;
Result:=bIsSucc;
end;