D
darnis
Unregistered / Unconfirmed
GUEST, unregistred user!
实在是搞不懂,为啥第一次用 TWinSocketStream进行读写操作时总是正确的,
而第二进行读写操作时,总是要丢掉 cTmp 缓冲区大小的数据,
实在没辙了,GG帮帮忙 ,,
我把 线程部分贴上来。。
unit CommThreadUnit;
interface
uses
Windows, ScktComp, SyncObjs;
type
// 控制Server Thread 在操作时的读写顺序,
// rmRBW Read Before Write
// rmWBR Write Before Read
TRWMode = (rmRBW, rmWBR);
TOnReceiveData = procedure (Receiver: TServerClientThread;
var Buffer: string) of object;
TCommServerThread = class(TServerClientThread)
private
FCS: TCriticalSection;
FLeftTime: cardinal;
FRWMode: TRWMode;
FLastBuffer: string;
FOnReceiveData: TOnReceiveData;
procedure SetRWMode(const Value: TRWMode);
procedure SetTimeOut(const Value: cardinal);
function GetCS: TCriticalSection;
procedure RaiseReadEvent;
protected
procedure ClientExecute; override;
public
destructor Destroy; override;
property CS: TCriticalSection read GetCS;
property RWMode: TRWMode read FRWMode write SetRWMode;
property LeftTime: cardinal read FLeftTime write SetTimeOut;
property OnReceiveData: TOnReceiveData read FOnReceiveData write
FOnReceiveData;
end;
implementation
uses
SysUtils;
{ TCommServerThread }
// 也不晓得问题究竟出在哪儿了,
// 想来应该是对 TWinSocketStream 的操作没有正确,但也实在是找不出原因。
procedure TCommServerThread.ClientExecute;
var
Stream : TWinSocketStream;
strTmp: string;
cTmp: array[0 .. 4] of Char;
iPos: integer;
begin
{ make sure connection is active }
while (not Terminated) and ClientSocket.Connected do
begin
Stream := TWinSocketStream.Create(ClientSocket, 10);
//Stream.TimeOut := 0;
try
try
case FRWMode of
rmRbW:
begin
if Stream.WaitForData(60000) then
begin // 等待接收数据
FillChar(cTmp, 5, 0);
FLastBuffer := '';
iPos := Stream.Position;
while Stream.Read(cTmp, 5) <> 0 do
begin
FLastBuffer := FLastBuffer + cTmp;
end;
if FLastBuffer = '' then
begin
// 既然不是超时进来,一定可以读取到数据。
// 没有读取到数据可以认定是连接有问题,
// 这时可以断开连接。
ClientSocket.Close;
Break;
end;
ClientSocket.SendText(Format('当前位置: %d, Receive Text: %s',
[iPos, FLastBuffer]));
// if Assigned(FOnReceiveData) then
// FOnReceiveData(self, FLastBuffer);
//Synchronize(RaiseReadEvent);
end;
end;
rmWBR:
begin
end;
end;
except
HandleException;
break;
end;
finally
Stream.Free;
Stream := nil;
end;
end;
end;
destructor TCommServerThread.Destroy;
begin
inherited;
end;
function TCommServerThread.GetCS: TCriticalSection;
begin
if not Assigned(FCS) then
FCS := TCriticalSection.Create;
Result := FCS;
end;
procedure TCommServerThread.RaiseReadEvent;
begin
if Assigned(FOnReceiveData) then
FOnReceiveData(self, FLastBuffer);
end;
procedure TCommServerThread.SetRWMode(const Value: TRWMode);
begin
FRWMode := Value;
end;
procedure TCommServerThread.SetTimeOut(const Value: cardinal);
begin
CS.Acquire ;
try
FLeftTime := Value;
finally
CS.Release;
end;
end;
end.
而第二进行读写操作时,总是要丢掉 cTmp 缓冲区大小的数据,
实在没辙了,GG帮帮忙 ,,
我把 线程部分贴上来。。
unit CommThreadUnit;
interface
uses
Windows, ScktComp, SyncObjs;
type
// 控制Server Thread 在操作时的读写顺序,
// rmRBW Read Before Write
// rmWBR Write Before Read
TRWMode = (rmRBW, rmWBR);
TOnReceiveData = procedure (Receiver: TServerClientThread;
var Buffer: string) of object;
TCommServerThread = class(TServerClientThread)
private
FCS: TCriticalSection;
FLeftTime: cardinal;
FRWMode: TRWMode;
FLastBuffer: string;
FOnReceiveData: TOnReceiveData;
procedure SetRWMode(const Value: TRWMode);
procedure SetTimeOut(const Value: cardinal);
function GetCS: TCriticalSection;
procedure RaiseReadEvent;
protected
procedure ClientExecute; override;
public
destructor Destroy; override;
property CS: TCriticalSection read GetCS;
property RWMode: TRWMode read FRWMode write SetRWMode;
property LeftTime: cardinal read FLeftTime write SetTimeOut;
property OnReceiveData: TOnReceiveData read FOnReceiveData write
FOnReceiveData;
end;
implementation
uses
SysUtils;
{ TCommServerThread }
// 也不晓得问题究竟出在哪儿了,
// 想来应该是对 TWinSocketStream 的操作没有正确,但也实在是找不出原因。
procedure TCommServerThread.ClientExecute;
var
Stream : TWinSocketStream;
strTmp: string;
cTmp: array[0 .. 4] of Char;
iPos: integer;
begin
{ make sure connection is active }
while (not Terminated) and ClientSocket.Connected do
begin
Stream := TWinSocketStream.Create(ClientSocket, 10);
//Stream.TimeOut := 0;
try
try
case FRWMode of
rmRbW:
begin
if Stream.WaitForData(60000) then
begin // 等待接收数据
FillChar(cTmp, 5, 0);
FLastBuffer := '';
iPos := Stream.Position;
while Stream.Read(cTmp, 5) <> 0 do
begin
FLastBuffer := FLastBuffer + cTmp;
end;
if FLastBuffer = '' then
begin
// 既然不是超时进来,一定可以读取到数据。
// 没有读取到数据可以认定是连接有问题,
// 这时可以断开连接。
ClientSocket.Close;
Break;
end;
ClientSocket.SendText(Format('当前位置: %d, Receive Text: %s',
[iPos, FLastBuffer]));
// if Assigned(FOnReceiveData) then
// FOnReceiveData(self, FLastBuffer);
//Synchronize(RaiseReadEvent);
end;
end;
rmWBR:
begin
end;
end;
except
HandleException;
break;
end;
finally
Stream.Free;
Stream := nil;
end;
end;
end;
destructor TCommServerThread.Destroy;
begin
inherited;
end;
function TCommServerThread.GetCS: TCriticalSection;
begin
if not Assigned(FCS) then
FCS := TCriticalSection.Create;
Result := FCS;
end;
procedure TCommServerThread.RaiseReadEvent;
begin
if Assigned(FOnReceiveData) then
FOnReceiveData(self, FLastBuffer);
end;
procedure TCommServerThread.SetRWMode(const Value: TRWMode);
begin
FRWMode := Value;
end;
procedure TCommServerThread.SetTimeOut(const Value: cardinal);
begin
CS.Acquire ;
try
FLeftTime := Value;
finally
CS.Release;
end;
end;
end.