从客户端向服务器发送图片时出现问题 [英] problems sending picture from client to server

查看:81
本文介绍了从客户端向服务器发送图片时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用服务器和客户端套接字将图片从'C:\ picture.bmp'发送到'c:\ temp \ picture.bmp' 客户端onconnect事件处理程序如下:

i am trying to send a picture from 'C:\picture.bmp' to 'c:\temp\picture.bmp' using server and client socket clients onconnect event handler is as follow:

procedure TForm2.ClientSocket1Connect(Sender: TObject;
  Socket: TCustomWinSocket);
 var
  fs : tfilestream;
  begin
     fs := TFileStream.create('C:\picture.bmp', fmOpenRead);//picture allready exists
     socket.SendStream(fs);
     fs.free;             
end;

和客户机上的服务器读为:

and servers onclientread as :

procedure TForm2.ServerSocket1ClientRead(Sender: TObject;
  Socket: TCustomWinSocket);
var
  fmm : tfilestream;
  iLen: Integer;
    Bfr: Pointer;
  begin

     iLen := Socket.ReceiveLength;
  GetMem(Bfr, iLen);
  fmm := TFileStream.Create('c:\temp\picture.bmp', fmCreate or 

fmShareDenyWrite);
  try
    Socket.ReceiveBuf(Bfr^, iLen);
    fmm.Write(Bfr^, iLen);
  finally
    FreeMem(Bfr);
    fmm.Free;
  end;

end;

图片已接收/创建,但是由于tfilestream.create方法而从未被接收到(即已创建)损坏? 请帮忙!我在做什么错了?

picture is recieved/created but is either corrupt on was never recieved i.e created because of tfilestream.create method? please help!what am i doing wrong?

推荐答案

尽管其名称为SendStream(),但不能保证发送整个流(尤其是在使用非阻塞套接字的情况下).它的返回值返回实际发送的字节数.如果一次调用发送的流的大小小于流的全部大小,则必须再次调用SendStream()(可能多次),以完成整个流的发送(SendText()也存在相同的问题).

Despite its name, SendStream() is NOT guaranteed to send the entire stream (especially if you are using a non-blocking socket). Its return value returns how many bytes are actually sent. If less than the full size of the stream are sent in one call, you have to call SendStream() again, potentially many times, to finish sending the entire stream (the same problems exists with SendText() as well).

另一方面,ReceiveLength()仅报告套接字当时有多少字节可用.这可能小于发送的完整流(同样,ReceiveText()可能也没有收到完整的发送字符串,因为它在内部使用了ReceiveLength()).

On the other side, ReceiveLength() only reports how many bytes are available on the socket AT THAT MOMENT. That is likely to be less than the full stream being sent (likewise, ReceiveText() may not receive a full sent string either because it uses ReceiveLength() internally).

发送流(或一般任意数据)的最佳方法是先发送数据大小,然后再发送实际数据.继续调用SendBuf/Stream/Text(),直到达到该大小为止(如果无阻塞套接字返回-1而不引发异常,则必须等待套接字的OnWrite事件触发,然后套接字才能再次接受更多数据).在接收端,先读取尺寸,然后继续读取直到达到指定尺寸.在获取所有数据之前,您可能必须读入OnRead事件的多次触发.

The best way to send a stream (or any arbitrary data in general) is to send the data's size first, then send the actual data afterwards. Keep calling SendBuf/Stream/Text() until that size is reached (if -1 is returned by a non-blocking socket without raising an exception, you have to wait for the socket's OnWrite event to trigger before the socket can accept more data again). On the receiving end, read the size first, then keep reading until the specified size is reached. You may have to read in multiple triggering of the OnRead event before you get all of the data.

转到 http://www.deja.com http://forums.embarcadero.com 来搜索Borland/CodeGear/Embarcadero新闻组/论坛档案.我已经多次发布示例代码.

Go to http://www.deja.com and http://forums.embarcadero.com to search the Borland/CodeGear/Embarcadero newsgroup/forum archives. I have posted example code many times before.

这篇关于从客户端向服务器发送图片时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆