求助:我的 scoket 为什么不能把图片传过去,我是错在哪里?谢谢! ( 积分: 5 )

  • 主题发起人 主题发起人 lsh998
  • 开始时间 开始时间
L

lsh998

Unregistered / Unconfirmed
GUEST, unregistred user!
请教:
服务器端:
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ScktComp;

type
TForm1 = class(TForm)
ServerSocket1: TServerSocket;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
ServerSocket1.Open;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
filename:string; //ini 文件路径
mystream: TMemoryStream; //流
buffer : array of byte;
len:longint;
begin
filename:=ExtractFilePath(paramstr(0))+'2.bmp';
mystream :=TMemoryStream.Create;
mystream.LoadFromFile(filename);
len:=mystream.Size;
setlength(buffer,len);
mystream.Seek(0,soFromBeginning);
mystream.Position:=0;
mystream.Write(buffer[0],len);
ServerSocket1.Socket.Connections[0].SendBuf(buffer[0],len);
end;

end.


客户端:
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ScktComp, ExtCtrls;

type
TForm1 = class(TForm)
Image1: TImage;
ClientSocket1: TClientSocket;
procedure FormCreate(Sender: TObject);
procedure ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
procedure ClientSocket1Error(Sender: TObject; Socket: TCustomWinSocket;
ErrorEvent: TErrorEvent; var ErrorCode: Integer);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
ClientSocket1.Open;
end;

procedure TForm1.ClientSocket1Read(Sender: TObject;
Socket: TCustomWinSocket);
var
len:longint;
rev : Longint;
mystream:TMemoryStream;
buffer : array of byte;
b:tbitmap;
begin
b:=tbitmap.Create;
len:=Socket.ReceiveLength;
setlength(buffer,len);
rev:=Socket.ReceiveBuf(buffer[0],len);
mystream:=TMemoryStream.Create;
mystream.Seek(0, soFromBeginning);
mystream.Position:=0;
mystream.Write(buffer[0],len);
b.LoadFromStream(mystream);
Image1.Picture.Bitmap.Assign(b);
b.Free;
end;

procedure TForm1.ClientSocket1Error(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
ErrorCode:=0;
end;

end.


单击 Button1 ,客户端 没有图片显示出来
我的图片是小于 64KB 的,我传字符串是可以传的
各位大哥,我是错在哪里啊?

谢谢!
 

Similar threads

I
回复
0
查看
732
import
I
I
回复
0
查看
704
import
I
I
回复
0
查看
699
import
I
后退
顶部