UDP能象TCP那样在本地缓冲组装传送过来的数据吗?

解决方案 »

  1.   

    传文件一般用tcp吧~~缓冲区不够用。。应该不会吧。。一般都是缓冲区有点数据就读过来了。。怎么可能不够。。只有cpu跑不过网卡的时候才会
      

  2.   

    你要用Udp应该也可以啊。传送数据,然后再一点一点的拼接起来。
      

  3.   

    to:lye2000000_super 怎么拼起来啊,能给个例子吗
      

  4.   

    有人说这样分解大的文件,
    FileStream   fs   =   new   FileStream(XMLfilepath,   FileMode.Open,   FileAccess.Read); 
    BinaryReader   r   =   new   BinaryReader(fs); 
    byte[]   buffer   =   r.ReadBytes((int)fs.Length); 
    sendSocket.Send(buffer);//sendSocket   为前面定义的用于发送文件的Socket! 可在接收后如何把分解的给拼起来呢?
    byte[] 可以相加成一个大的byte[] 吗?
      

  5.   

    合并 byte[]的方法:
      byte[]   ba=new   byte[]   {1,2,3};   
      byte[]   bb=new   byte[]   {4,5,6,7,8,9};   
      byte[]   bc;   
        
      //方法1:用MEMORYSTREAM   
      System.IO.MemoryStream   stream=new   System.IO.MemoryStream();   
      stream.Write(ba,   0,   ba.Length   );   
      stream.Write(bb,   0,   bb.Length   );   
      stream.Close();   
      bc=stream.GetBuffer();   
        
      //方法2:复制   
      bc=new   byte[ba.Length   +bb.Length   ];   
      ba.CopyTo(bc,   0);   
      bb.CopyTo(bc,   ba.Length);   下面是一个用如上方法写的一个用Socket的Tcp传送文件的方法,UDP也应该是一样的,只要保证数据都接收到
    private Thread myThread;
    private Socket ListenSock;
    public Form1()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent();
    myThread=new Thread(new ThreadStart(ListenTcp));
    myThread.Start(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if(myThread!=null){myThread.Abort();}
    if(ListenSock!=null){ListenSock.Close();}
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    }
    ...
    private void btnSend_Click(object sender, System.EventArgs e)
    {
    string filepath=this.txtBSourceAddress.Text; //分解大的文件
    FileStream fs=new FileStream(filepath,FileMode.Open,FileAccess.Read);   
    BinaryReader r=new BinaryReader(fs);   
    byte[] buffer=r.ReadBytes((int)fs.Length); 
      
    IPEndPoint ipep=new IPEndPoint(IPAddress.Parse(this.txtBDestinationIP.Text),9050);
    Socket SendSocket=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);

    SendSocket.Connect(ipep);
    SendSocket.Send(buffer);//sendSocket       为前面定义的用于发送文件的Socket!   
    SendSocket.Close();
    fs.Close();
    } //写byte[]到fileName
    private bool writeFile(byte[] pReadByte, string fileName)
    {
    FileStream pFileStream = null;
    try
    {
    pFileStream = new FileStream(fileName, FileMode.OpenOrCreate);
    pFileStream.Write(pReadByte, 0, pReadByte.Length);
    }
    catch
    {
    return false;
    }
    finally
    {
    if (pFileStream != null)
    pFileStream.Close();
    }
    return true;
    } public void ListenTcp()
    {
    IPEndPoint ipep=new IPEndPoint(IPAddress.Any,9050);
    ListenSock=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
    ListenSock.Bind(ipep); while(true)//循环监听
    {
    ListenSock.Listen(10);
    Socket client=ListenSock.Accept(); byte[] data=null;
    byte[] buffers=null;
    int recv;
    Console.WriteLine("---------------------------------------------");
    while(true)//循环接收数据
    {
    data=new byte[1048576];
    recv=client.Receive(data);
    Console.WriteLine("接收到的字节:"+recv.ToString());
    if(recv<=0)
    break;
    //合并data,对于小于data的数据转成一个recv大小的byte[]数组
    if(recv<1048576)
    {
    byte[] datanew=new byte[recv];
    for(int i=0;i<recv;i++){datanew[i]=data[i];}
    buffers=incorporateByte(datanew,buffers);
    }
    else
    {
    buffers=incorporateByte(data,buffers);
    }
    Console.WriteLine("合并后buffers的长度:"+buffers.Length);
    }
    Console.WriteLine("合并后buffers的总长度:"+buffers.Length.ToString());

    string DestinationPath=this.txtBDestinationAddress.Text;
    writeFile(buffers,DestinationPath);
    client.Close();
    }
    } //合并byte[]方法: 输入一个byte[]:inByte ,把它合并到自己的byte[] ownByte中去,再把它返回
    public byte[] incorporateByte(byte[] inByte,byte[] ownByte){
    int inByteLength=0;
    if(inByte!=null){inByteLength=inByte.Length;} int ownByteLength=0;
    if(ownByte!=null){ownByteLength=ownByte.Length;}

    byte[] buffer=new byte[ownByteLength+inByteLength]; if(ownByte!=null){ownByte.CopyTo(buffer,0);}
    if(inByte!=null){inByte.CopyTo(buffer,ownByteLength);}
    return buffer;
    }
      

  6.   

    合并   byte[]的方法: 
        byte[]       ba=new       byte[]       {1,2,3};       
        byte[]       bb=new       byte[]       {4,5,6,7,8,9};       
        byte[]       bc;       
            
        //方法1:用MEMORYSTREAM       
        System.IO.MemoryStream       stream=new       System.IO.MemoryStream();       
        stream.Write(ba,       0,       ba.Length       );       
        stream.Write(bb,       0,       bb.Length       );       
        stream.Close();       
        bc=stream.GetBuffer();     
    你这一个方法合并byte[]得到bc是会变大的,
    应该把    bc=stream.GetBuffer();     改成:bc=stream.ToArray(); 才能得到正确的ba+bb的数据