TClientSocket<==>TServerSocket,情况是在网络故障恢复后,Client无法Connect到Server,此时Client

  • 主题发起人 主题发起人 zjlzd
  • 开始时间 开始时间
Z

zjlzd

Unregistered / Unconfirmed
GUEST, unregistred user!
TClientSocket&lt;==&gt;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&lt;&gt;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&gt;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 &gt; 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;
 
应该和server端没什么关系,做一个函数处理onerror消息中的connect错误消息,重新连接,
这样就可以保证可以恢复连接
 
没有这样表达问题的,自己不提取问题本质,摆出一大堆无聊的代码,谁能看完,谁能知道
你想解决什么问题。看了只好say: by by
 
多人接受答案了。
 

Similar threads

I
回复
0
查看
1K
import
I
I
回复
0
查看
651
import
I
I
回复
0
查看
615
import
I
I
回复
0
查看
550
import
I
后退
顶部