在程程序中为了控制超时使用了TWinSocketStream 对象来读取socket中的数据,socket是阻塞方式。
调用如下
csSocket.Active:=true;
csSocket.Socket.SendText('i want passwd');
RecvStr:=SocketRead(csSocket,5000);
csSocket.Active:=false;
SocketRead读出的串为空,但不是由于超时返回的。
如果用 csSocket.Socket.receivebuf 读取可以读到我想要的内容,但是这样就无法
设置超时。
SocketRead读出的串为空,这种情况我试验了几台机器有的机器可以读出,有的就是返回空串。
//////////////SocketRead
Function SocketRead(ClientSocket : TClientSocket; timeout: integer): string;
var
Stream : TWinSocketStream;
Buffer : array[0 .. 2048] of Char;
Packet_len : Integer;
i : Integer;
begin
Result := '';
{ make sure connection is active }
if ClientSocket.Socket.Connected then
begin
Stream := TWinSocketStream.Create(ClientSocket.Socket, timeout);
try
FillChar(Buffer, 2048, 0); { initialize the buffer } if Stream.WaitForData(timeout) then
begin
Packet_len := Stream.Read(Buffer, 2048);
if Packet_len > 0 then
begin
for i:=0 to Packet_len-1 do
begin
if Buffer[i] = Chr(0) then
Buffer[i] := ' ';
end;
Result := Buffer;
end
else
Result := '';
end
else
begin
Result := '';
end;
finally
Stream.Free;
end;
end;
end;
调用如下
csSocket.Active:=true;
csSocket.Socket.SendText('i want passwd');
RecvStr:=SocketRead(csSocket,5000);
csSocket.Active:=false;
SocketRead读出的串为空,但不是由于超时返回的。
如果用 csSocket.Socket.receivebuf 读取可以读到我想要的内容,但是这样就无法
设置超时。
SocketRead读出的串为空,这种情况我试验了几台机器有的机器可以读出,有的就是返回空串。
//////////////SocketRead
Function SocketRead(ClientSocket : TClientSocket; timeout: integer): string;
var
Stream : TWinSocketStream;
Buffer : array[0 .. 2048] of Char;
Packet_len : Integer;
i : Integer;
begin
Result := '';
{ make sure connection is active }
if ClientSocket.Socket.Connected then
begin
Stream := TWinSocketStream.Create(ClientSocket.Socket, timeout);
try
FillChar(Buffer, 2048, 0); { initialize the buffer } if Stream.WaitForData(timeout) then
begin
Packet_len := Stream.Read(Buffer, 2048);
if Packet_len > 0 then
begin
for i:=0 to Packet_len-1 do
begin
if Buffer[i] = Chr(0) then
Buffer[i] := ' ';
end;
Result := Buffer;
end
else
Result := '';
end
else
begin
Result := '';
end;
finally
Stream.Free;
end;
end;
end;
解决方案 »
- dbgrideH隔行变色
- 如何知道scrollbox的滚动条已经移到最底部了?
- 周五周六睡太晚了,今天到现在却还没睡意,明天还要上班啊。。。。。
- 有没有强制类型转换函数 我有 一个 8923abcd这样的一串数 其中8923是整型的。abcd是字符型的 我想把8923abcd转换成字符的 用什么函
- 通过query组件如何将触发器代码保存到对应的表中,谢谢。
- 新年散分,顺便问个问题
- idftpserver的问题
- 关于DELPHI和VC互调DLL的问题。
- pws4.0哪有下载啊
- 如何修改Listview,ListBox选中项目的颜色,这个一般由windows外观控制?
- 关于COM+的问题,急!!!!!!!
- 我的Sql Server的回复错在哪?
这里要求socket一定要使用阻塞模式阿,
Wally_wu(韦利) 你说的是什么意思,可以说的详细些么。