我想使用套接字将文件发送到客户端应用程序(远程PC),但是当我发送时ClientSocket不接收SendText消息,例如如下:
ClientSocket3.Socket.SendText('<|sobe|>');
用于ClientSocket3Read。
CLIENT SIDE =&gt;
var
tamanho: Integer;
nome: string;
recebendo: boolean;
Stream: TMemoryStream;
//==================================================
procedure TForm1.ClientSocket2Error(Sender: TObject; Socket: TCustomWinSocket; ErrorEvent: TErrorEvent; var ErrorCode: Integer);
begin
ErrorCode := 0;
end;
procedure TForm1.ClientSocket2Read(Sender: TObject; Socket: TCustomWinSocket);
var
dados: String;
begin
dados := Socket.ReceiveText;
if Copy(dados,1,6)='<|up|>' then
Begin
Delete(dados, 1, 6) ; // Getting only complete path with file name as example C:\file.txt from OpenDialog1 component in my Server application.
Edit1.Text:= dados; // Receives as for example 'C:\file.txt' :D
nome:= dados; // // Receives as for example 'C:\file.txt' :D
button1.Click; // Calling button click for SendText('<|sobe|'); for ClientSocket3
exit;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
ClientSocket3.Socket.SendText('<|sobe|>');
end;
procedure TForm1.ClientSocket3Read(Sender: TObject; Socket: TCustomWinSocket);
var
s: string;
begin
s := Socket.ReceiveText;
// Testing with warning message, but don't receives '<|sobe|' text in: s := Socket.ReceiveText; and don't execute this line.
// Then here begins my trouble :(
Application.MessageBox(pchar(s), 'Aviso', 64);
//================================================================================
if Recebendo = False then begin
if Pos(#0, s)>0 then begin
Tamanho := StrToInt(Copy(s, 1, Pos(#0, s)-1));
Delete(s, 1, Pos(#0, s));
Stream := TMemoryStream.Create;
Pb1.Max := tamanho; // PB1 <= ProgressBar1
Recebendo := true;
end;
end;
try
Stream.Write(s[1], Length(s));
PB1.Position := Stream.Size;
if Stream.Size >= Tamanho then begin
Stream.Position := 0;
if nome = edit1.Text then begin
Stream.SaveToFile(nome);
Application.MessageBox('Salvo com sucesso!', 'Aviso', 64);
end;
Recebendo := false;
Stream.Free;
end;
except
Stream.Free;
Recebendo := false;
end;
end;
procedure TForm1.ClientSocket3Error(Sender: TObject; Socket: TCustomWinSocket; ErrorEvent: TErrorEvent; var ErrorCode: Integer);
begin
ErrorCode := 0;
end;
在我的服务器应用程序中,我将文件发送为:
SERVER SIDE =&gt;
type
TServ_Sock3 = class(TThread)
private
procedure Execute; override;
public
Socket: TCustomWinSocket;
end;
procedure TForm1.EnviarArquivo1Click(Sender: TObject);
var
caminho: String;
begin
If not Assigned(LV.Selected) then exit;
if LV.Selected.Checked = false then exit;
if OpenDialog1.Execute then begin
caminho := opendialog1.FileName; // 'C:\file.txt'
ServerSocket1.Socket.Connections[Form1.LV.ItemIndex].SendText('<|up|>'+caminho);
// PS: LV, is a ListView component that show me all connected computers in my network.
end;
end;
procedure TServ_Sock3.Execute;
var
s: String;
Stream: TMemoryStream;
begin
inherited;
while not Terminated and Socket.Connected do begin
if Socket.ReceiveLength>0 then begin
s := Socket.ReceiveText;
if Pos('<|sobe|>', s)>0 then begin
Stream := TMemoryStream.Create;
Stream.LoadFromFile(caminho);
Stream.Position := 0;
Socket.SendText(IntToStr(Stream.Size)+#0);
Sleep(1000);
Socket.SendStream(Stream);
end;
end;
end;
Sleep(10);
end;
procedure TForm1.ServerSocket2ClientError(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
ErrorCode:=0
end;
procedure TForm1.ServerSocket2Accept(Sender: TObject;
Socket: TCustomWinSocket);
var
Conex: TServ_Sock3;
begin
Conex := TServ_Sock3.Create(true);
Conex.Socket := Socket;
Conex.FreeOnTerminate := True;
Conex.Resume;
end;
有人可以帮助我吗?