關于Socket的SendStream的疑惑(100分)

  • 主题发起人 主题发起人 Jelly0228
  • 开始时间 开始时间
J

Jelly0228

Unregistered / Unconfirmed
GUEST, unregistred user!
ClientSocket﹑ServerSocket組件。
都采用阻塞模式。
發送一個RichEdit的文本內容﹐為何這樣寫不行﹕
客戶端﹕
TClientThread.execute 過程﹕//TClientThread是從TThread派生下來的
SendStr:=TStringStream.Create('ok');
Form1.RichEdit1.Lines.SaveToStream(SendStr);
ClientSocket.Socket.SendStream(str);

服務端﹕
全局變量T:
T: TStringStream;

TServerThread.ClientExecute 過程﹕//從TServerClientThread派生下來
var
S:=TWinSocketStream;
While (not Terminated) and (ClientSocket.Connected) do
begin
S:= TWinSocketStream.Create(ClientSocket,60000);
T:=TStringStream.Create('AAAA');
S.seek(0,soFromBeginning);
T.CopyFrom(S,S.Size);
Synchronize(A過程);
//A過程的代碼是這樣的﹕Form2.RichEdit1.Lines.LoadFromStream(T)
end;

問題﹕
1.客戶端執行完ClientSocket.Socket.SendStream(str)后就不知道跑到哪兒去了﹐但當強行中斷程序后﹐就會跑到服務端來﹔
除非這樣寫才行﹕
A:=Form1.RichEdit1.Lines.Text;
ClientSocket.Socket.SendStream(TStringStream.Create(A));

2.服務端的文字流T也讀不到數據﹐﹐只能用ReadBuf讀。這是為什么呢??
 
嗚嗚嗚嗚嗚嗚嗚嗚嗚嗚嗚嗚嗚嗚嗚嗚﹐﹐﹐
沒有人理偶~~
 
接收数据:
procedure TSrvrClntThrd.Execute;
var
ret,ReceiveLength:integer;
s:string;
pStream:TWinSocketStream;
Buf:Pointer; //接收、发送的数据结构
begin
while (not Terminated) and (ClientSocket.Connected) do
begin
try
pStream:=TWinSocketStream.Create(ClientSocket, 60000);
try
if pStream.WaitForData(6000) then
begin
ReceiveLength := ClientSocket.ReceiveLength;
Buf := AllocMem( ReceiveLength+1 );
ret := pStream.Read(Buf^,ReceiveLength);
SetLength( s, ReceiveLength );
Move( Buf^, PChar(s)^, ReceiveLength );
//s为收到的数据
ClientSocket.Close;
end;
finally
pStream.Free;
Sleep(10);
end;
except
end;
end;
end;
试试!!!
 
嗯﹐這樣一定行﹐就是不明白為什么CopyFrom不行??查看了CopyFrom方法的代碼﹐也就是分塊寫入緩沖再寫入流啊~~~~
 
后退
顶部