H
hhddff
Unregistered / Unconfirmed
GUEST, unregistred user!
看以下程序怎么解决,我是用ClientSocket和ServerSocket来实现传送抓屏数据。<br>我用ServerSocket往ClientSocket发送图象数据成功,但是<br>我用ClientSocket往ServerSocket发送图象数据但是有问题,请高手解决一下。<br>我的程序发送一半就出问题,显示 Error jpeg#52。<br>我很急!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!<br>Client数据<br>unit Clientfrm;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> Dialogs, ScktComp,jpeg, StdCtrls;<br><br>type<br> TfrmClient = class(TForm)<br> Memo1: TMemo;<br> Edit1: TEdit;<br> Button1: TButton;<br> ClientSocket1: TClientSocket;<br> procedure FormCreate(Sender: TObject);<br> procedure FormClose(Sender: TObject; var Action: TCloseAction);<br> procedure Button1Click(Sender: TObject);<br> procedure ClientSocket1Connect(Sender: TObject;<br> Socket: TCustomWinSocket);<br> procedure ClientSocket1Error(Sender: TObject; Socket: TCustomWinSocket;<br> ErrorEvent: TErrorEvent; var ErrorCode: Integer);<br> procedure ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);<br> procedure Button2Click(Sender: TObject);<br> private<br> { Private declarations }<br> public<br> { Public declarations }<br> end;<br><br>var<br> frmClient: TfrmClient;<br> c:longint;<br> m:tmemorystream;<br> m1:tmemorystream;<br><br>implementation<br><br>{$R *.dfm}<br><br><br>procedure TfrmClient.FormCreate(Sender: TObject);<br>begin<br> m:=tmemorystream.Create;<br>end;<br><br>procedure TfrmClient.FormClose(Sender: TObject; var Action: TCloseAction);<br>begin<br> m.free;<br> ClientSocket1.Close;<br>end;<br><br>procedure TfrmClient.Button1Click(Sender: TObject);<br>begin<br> try<br> clientsocket1.Close;<br> clientsocket1.Host := edit1.text;<br> clientsocket1.Open; //file://连接服务端<br> except<br> showmessage(edit1.text+#13#10+'未开机或未安装服务程序');<br> end;<br>end;<br><br>procedure TfrmClient.ClientSocket1Connect(Sender: TObject;<br> Socket: TCustomWinSocket);<br>begin<br> memo1.Lines.Add('ok'); <br>end;<br><br>procedure TfrmClient.ClientSocket1Error(Sender: TObject;<br> Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;<br> var ErrorCode: Integer);<br>begin<br> caption:='连接'+edit1.text+'失败';<br> showmessage(edit1.text+#13#10+'未开机或未安装服务程序');<br> errorcode:=0;<br>end;<br><br>procedure TfrmClient.ClientSocket1Read(Sender: TObject;<br> Socket: TCustomWinSocket);<br>var<br> s,s1:string;<br> desk:tcanvas;<br> bitmap:tbitmap;<br> jpg:tjpegimage;<br>begin<br> s:=socket.ReceiveText;<br> memo1.lines.add(s);<br> if s='gets' then //file://客户端发出申请<br> begin<br> try<br> m1:=tmemorystream.Create;<br> bitmap:=tbitmap.Create;<br> jpg:=tjpegimage.Create;<br> desk:=tcanvas.Create; //以下代码为取得当前屏幕图象<br> desk.Handle:=getdc(hwnd_desktop);<br> with bitmap do<br> begin<br> width:=screen.Width;<br> height:=screen.Height;<br> canvas.CopyRect(canvas.cliprect,desk,desk.cliprect);<br> end;<br> jpg.Assign(bitmap); //file://将图象转成JPG格式<br> jpg.CompressionQuality:=10;//文件压缩大小设置<br> //m1.clear;<br> jpg.SaveToStream(m1); //file://将JPG图象写入流中<br> jpg.free;<br> m1.Position:=0;<br> s1:=inttostr(m1.size);<br> ClientSocket1.Socket.SendText('size/'+s1); //file://发送图象大小<br> finally<br> bitmap.free;<br> desk.free;<br> end;<br> end;<br> if s='okok' then //file://客户端已准备好接收图象<br> begin<br> m1.Position:=0;<br> ClientSocket1.Socket.SendStream(m1); //file://发送JPG图象<br> end;<br> //end; //附加<br>end;<br><br>procedure TfrmClient.Button2Click(Sender: TObject);<br>begin<br> ClientSocket1.Socket.SendText('ok');<br>end;<br><br>end.<br>//Server数据<br>unit ServerFrm;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> Dialogs, StdCtrls, ScktComp, ExtCtrls,jpeg;<br><br>type<br> TfrnServer = class(TForm)<br> Image1: TImage;<br> Label1: TLabel;<br> Button2: TButton;<br> ServerSocket1: TServerSocket;<br> Memo1: TMemo;<br> procedure Button2Click(Sender: TObject);<br> procedure ServerSocket1ClientRead(Sender: TObject;<br> Socket: TCustomWinSocket);<br> procedure ServerSocket1ClientConnect(Sender: TObject;<br> Socket: TCustomWinSocket);<br> procedure FormCreate(Sender: TObject);<br> procedure FormClose(Sender: TObject; var Action: TCloseAction);<br> private<br> { Private declarations }<br> public<br> function GetMaskString(i:integer;s:string):string;<br> { Public declarations }<br> end;<br><br>var<br> frnServer: TfrnServer;<br> c:longint;<br> m:tmemorystream;<br> //m1:tmemorystream;<br> id: integer;<br><br>implementation<br><br>{$R *.dfm}<br><br>procedure TfrnServer.Button2Click(Sender: TObject);<br>var<br> Socket:TCustomWinSocket;<br>begin<br> Socket:=TCustomWinSocket.Create(id);<br> Socket.SendText('gets'); //file://发送申请,通知服务端需要屏幕图象<br>end;<br><br>procedure TfrnServer.ServerSocket1ClientRead(Sender: TObject;<br> Socket: TCustomWinSocket);<br>var<br> buffer:array [0..10000] of byte; //file://设置接收缓冲区<br> len:integer;<br> recvmsg,firstmsg,ll:string;<br> b:tbitmap;<br> j:tjpegimage;<br>begin<br> //if c=0 then //file://C为服务端发送的字节数,如果为0表示为尚未开始图象接收<br> //begin<br> len:=socket.ReceiveLength; //file://读出包长度<br> recvmsg := Socket.ReceiveText ;<br><br> firstmsg := GetMaskString(1,recvmsg);<br> if firstmsg = 'size' then<br> begin<br> ll:=GetMaskString(2,recvmsg);<br> memo1.Lines.Add('ll'+ll);<br> c:=strtoint(ll); //file://设置需接收的字节数<br> Socket:=TCustomWinSocket.Create(id);<br> Socket.SendText('okok'); //file://通知服务端开始发送图象<br> end<br> //end else<br> else begin //file://以下为图象数据接收部分<br> memo1.Lines.Add('len='+inttostr(len));<br> memo1.Lines.Add('c='+inttostr(c));<br> socket.ReceiveBuf(buffer,len); //file://接收数据包并读入缓冲区内<br> m.Write(buffer,len); //file://追加入流M中<br> if m.Size>=c then //file://如果流长度大于需接收的字节数,则接收完毕<br> begin<br> m.Position:=0;<br> b:=tbitmap.Create;<br> j:=tjpegimage.Create;<br> try<br> j.LoadFromStream(m); //file://将流M中的数据读至JPG图像对象J中<br> b.Assign(j); //file://将JPG转为BMP<br> Image1.Picture.Bitmap.Assign(b); //file://分配给image1元件<br> finally //file://以下为清除工作<br> b.free;<br> j.free;<br> Serversocket1.Active:=false;<br> Serversocket1.Active:=true;<br> m.Clear;<br> c:=0;<br> end;<br> end;<br> end;<br>end;<br>procedure TfrnServer.ServerSocket1ClientConnect(Sender: TObject;<br> Socket: TCustomWinSocket);<br>begin<br> id := Socket.SocketHandle ;<br> memo1.Lines.Add('id='+inttostr(id));<br>end;<br><br>function TfrnServer.GetMaskString(i: integer; s: string): string;<br>var<br> itm:Tstringlist; //字符串列表<br>begin<br> itm:=tstringlist.Create;<br> itm.Delimiter:='/';<br> itm.DelimitedText:=s ;<br><br> if (i>itm.Count) or (i<1 ) then<br> begin<br> result:='';<br> itm.Free ;<br> exit;<br> end;<br><br> Result:=itm.strings[i-1];<br> itm.Free;<br>end;<br><br>procedure TfrnServer.FormCreate(Sender: TObject);<br>begin<br> m:=tmemorystream.Create;<br> ServerSocket1.open;<br>end;<br><br>procedure TfrnServer.FormClose(Sender: TObject; var Action: TCloseAction);<br>begin<br> ServerSocket1.close;<br>end;<br><br>end.<br>