Registriert seit: 1. Mär 2003
2.078 Beiträge
Delphi 2005 Professional
|
Re: Bitmap über Netzwerk verschicken - Aber wie empfangen?
6. Jul 2003, 21:46
Dank der großartigen Hilfe (was heißt Hilfe, Marco hat das allein Gemacht!) gibts jetzt eine Lösung für das Problem:
Beim ServerProgramm:
Delphi-Quellcode:
TReceiveThread = class(TServerClientThread)
protected
procedure Execute; override;
end;
var
FormServer: TFormServer;
WS: TWinSocketStream;
implementation
{$R *.dfm}
procedure TReceiveThread.Execute;
var
WS: TWinSocketStream;
MS: TMemoryStream;
GetSize: Integer;
Buffer: array[0..8191] of Char;
begin
MS := TMemoryStream.Create;
try
while (not Terminated) and ServerSocket.Connected do
begin
WS := TWinSocketStream.Create(ClientSocket, 500);
try
if WS.WaitForData(60000) then
begin
GetSize := WS.Read(Buffer, SizeOf(Buffer));
while (GetSize > 0) do
begin
MS.Write(Buffer, GetSize);
GetSize := WS.Read(Buffer, SizeOf(Buffer));
end;
MS.Position := 0;
FormServer.Image.Picture.Bitmap.LoadFromStream(MS);
Terminate;
end;
finally
WS.Free;
end;
end;
finally
MS.Free;
end;
end;
procedure TFormServer.ServerSocketAccept(Sender: TObject;
Socket: TCustomWinSocket);
begin
Statusbar.SimpleText := 'Verbunden mit: ' + Socket.RemoteAddress;
end;
procedure TFormServer.ServerSocketClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
Statusbar.SimpleText := 'Verbindung wurde getrennt';
end;
procedure TFormServer.FormCreate(Sender: TObject);
begin
SetBounds(10,150,Width,Height);
ServerSocket.Open;
end;
procedure TFormServer.FormDestroy(Sender: TObject);
begin
ServerSocket.Close;
end;
procedure TFormServer.ServerSocketGetThread(Sender: TObject;
ClientSocket: TServerClientWinSocket;
var SocketThread: TServerClientThread);
begin
SocketThread := TReceiveThread.Create(False, ClientSocket);
end;
Und beim Client:
Delphi-Quellcode:
procedure TFormClient.EditHostChange(Sender: TObject);
begin
ButtonSend.Enabled := (EditHost.Text <> '') and FileExists(EditBitmap.Text);
end;
procedure TFormClient.ButtonOpenClick(Sender: TObject);
begin
with OpenDialog do
if Execute then
EditBitmap.Text := FileName;
end;
procedure TFormClient.ButtonSendClick(Sender: TObject);
begin
ClientSocket.Host :=EditHost.Text;
ClientSocket.Open;
end;
procedure TFormClient.ClientSocketError(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
MessageDlg('Fehler beim verbinden mit : ' + EditHost.Text + ' (' +
IntToStr(ErrorCode) + ')', mtError, [mbOk], 0);
ErrorCode := 0;
end;
procedure TFormClient.ClientSocketConnect(Sender: TObject;
Socket: TCustomWinSocket);
var
MS: TMemoryStream;
begin
MS := TMemoryStream.Create;
try
MS.LoadFromFile(EditBitmap.Text);
MS.Position := 0;
Socket.SendBuf(MS.Memory^, MS.Size);
finally
MS.Free;
end;
ClientSocket.Close;
end;
procedure TFormClient.FormDestroy(Sender: TObject);
begin
ClientSocket.Close;
end;
procedure TFormClient.FormCreate(Sender: TObject);
begin
SetBounds(10,10,Width,Height);
EditHostChange(Sender);
end;
end.
Vielen Danke nochmal an Marco!
|
|
Zitat
|