一个关于服务器端接收数据的问题 ( 积分: 100 )

  • 主题发起人 主题发起人 popoaleck
  • 开始时间 开始时间
P

popoaleck

Unregistered / Unconfirmed
GUEST, unregistred user!
使用Indy的IdTcpServer控件,监听一个TCP端口,当客户端发送数据过来时接收该数据.客户端以字节流的方式发送数据.请帮我看看接受程序哪里有问题.现在只要执行到Read那里就会出错.

procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);
var
stream: TStream;
str1, str2: array [0..15] of char;
begin
try
if AThread.Connection.Connected then
begin
stream := TStream.Create;
Athread.Connection.ReadStream(stream);
FillChar(str1, 16, 0);
if stream.Read(str1, 16) > 0 then
memo1.Lines.Append('client send:' + str1);
end;
except
end;
end;
 
咋没人回呀? 自己顶一下
 
try
if AThread.Connection.Connected then
begin
stream := TMemoryStream.Create;
try
Athread.Connection.ReadStream(stream);
FillChar(str1, 16, 0);
if stream.Read(str1, 16) > 0 then
memo1.Lines.Append('client send:' + str1);
end;
finally
stream.Free;
except
end;
 
不行,执行到ReadStream时报出"TStream.Seek not implemented"错误. 而且读不出来
 
读的方法我没试:
procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);
var
stream: TStream;
str1, str2: array [0..15] of char;
begin
try
if AThread.Connection.Connected then
begin
stream := TMemoryStream.Create; //TStream是虚类,必须用它的子类
try
Athread.Connection.ReadStream(stream);
FillChar(str1, 16, 0);
if stream.Read(str1, 16) > 0 then
memo1.Lines.Append('client send:' + str1);
finally
stream.Free;
end;
end;
except
end;
end;
 
应该用哪个子类呢? TStringStream, TMemoryStream还是TWinSocketStream?
 
我在服务端用ReadChar能读出数据,但是用ReadBuffer或ReadStream就不行,读就没有响应了.是否要单实现一个线程来进行读取呢?
 
procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);
var
stream: TStream;
str1, str2: array [0..15] of char;
begin
try
if AThread.Connection.Connected then
begin
stream := TMemoryStream.Create; //TStream是虚类,必须用它的子类
try
Athread.Connection.ReadStream(stream);
FillChar(str1, 16, 0);
if stream.write(str1, 16) > 0 then //Write把?
memo1.Lines.Append('client send:' + str1);
finally
stream.Free;
end;
end;
except
end;
end;
 
TStream是虚类,不能够实例化
只能用其子类实例化
 

Similar threads

S
回复
0
查看
738
SUNSTONE的Delphi笔记
S
S
回复
0
查看
730
SUNSTONE的Delphi笔记
S
后退
顶部