Socket.ReceiveBuf(50分)

  • 主题发起人 主题发起人 wlmmlw
  • 开始时间 开始时间
W

wlmmlw

Unregistered / Unconfirmed
GUEST, unregistred user!
procedure TForm1.FormCreate(Sender: TObject);
begin
ss.Open;
cs.Open;
end;

procedure TForm1.ssClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
reinfo.Lines.Add('Connect from '+socket.RemoteAddress);
end;

procedure TForm1.btnSendDataClick(Sender: TObject);
var
buf: TMemoryStream;
begin
buf := TMemoryStream.Create;
if od1.Execute then begin
buf.LoadFromFile(od1.FileName);
// recs.Lines.SaveToStream(buf);
buf.Position := 0;
cs.Socket.SendStream(buf);
// cs.Socket.SendStreamThenDrop(buf);
end;
end;

procedure TForm1.ssClientWrite(Sender: TObject; Socket: TCustomWinSocket);
begin
reinfo.Lines.Add('Send to ' + Socket.RemoteAddress);
end;

procedure TForm1.ssClientRead(Sender: TObject; Socket: TCustomWinSocket);
var
rc: Integer;
rs: TStrings;
begin
reinfo.Lines.Add('Receive from ' + Socket.RemoteAddress);
reinfo.Lines.Add('Receive '+ Inttostr(Socket.ReceiveLength) + ' Bytes');

Buf.SetSize(Socket.ReceiveLength);
Socket.ReceiveBuf(buf,Socket.ReceiveLength);
rs := TStringList.Create;
try
rs.Text := Socket.ReceiveText;
rs.SaveToFile('c:/ss.txt');
finally
rs.Free;
end;
end; ///以上的过程总是有错,如果不用Socket.ReceiveBuf就不会有错

initialization
buf := TMemoryStream.Create;
finalization
buf.free;
end.
 
Buf.SetSize(Socket.ReceiveLength);
Socket.ReceiveBuf(buf,Socket.ReceiveLength);
你的Bug在那里定义的?
 
var
Form1: TForm1;
buf: TMemoryStream;

implementation
 
是Buf,呵呵,出Bug了。
 
那里Create的,注意,不要发生重载吆!
 
function recv(s: TSocket; var Buf; len, flags: Integer): Integer; stdcall;
 
int recv (

SOCKET s,
char FAR* buf,
int len,
int flags
);
 
ReceiveBuf(var Buf; Count: Integer)
Buf不是stream类型,必须使用 array[0..nnn] of char 类型
或者GetMem动态分配
buf:Point;
getmem(buf,Socket.ReceiveLength);
Socket.ReceiveBuf(buf^ ,Socket.ReceiveLength); //注意buf后面小小的 ^ 符号
。。。
FreeMem(Buf);
--------------------
另外,用静态array不需要使用^号

 
Up:
说得对,上面的问题解决了,只是接受大文件还是有错,不知如何解决?!
请赐教!!
 
大文件?
pChar类型是否可以?
 
var
Len: Integer;
RecBuf:array of char;
begin
Len:=Socket.ReceiveLength;
SetLength(RecBuf, Len);
Socket.ReceiveBuf(RecBuf, Len);
{上一句出错,如果在定义时用: RecBuf:array[1..2048] of char;则可以,请问这是为什么??
...
end;
 
多人接受答案了。
 
后退
顶部