日期:2014-05-17  浏览次数:21084 次

socket分段发送大文件的方法
请大家踊跃发表一件和贴代码。大量分分送上,up的也有分。

------解决方案--------------------
将文件分段传送,每次传送8KB。

先定义一个单个数据包的最大长度
然后发送文件。如果大小超过单个包大小就拆分后发送。
接收段只需要把数据拼合起来就好了。
------解决方案--------------------
幫頂吧···
------解决方案--------------------
封包,定义包头包尾.
------解决方案--------------------
我自己收集的方法,没有真正用过
C# code
       private void SendFileStream(Socket socketStream, FileStream fs, byte[] byPack, string strFileName)
        {
            byte[] byHead = new byte[128];
            byHead[0] = byPack[0];
            byHead[1] = byPack[1];

            byte[] byFileName = encodingGB.GetBytes(strFileName);
            byHead[2] = Convert.ToByte(byFileName.Length);

            byFileName.CopyTo(byHead, 3);

            if (socketStream != null)
            {
                SocketSendMessage(socketStream, byHead);
            }
            else
            {
                MessageBox.Show("socket");
                return;
            }
            //socketStream.Send(byHead);

            byte[] byBody = new byte[PACKET_SIZE];            

            int nPackCount = Convert.ToInt32(byPack[0]) * Convert.ToInt32(byPack[1]);    

            int nCurrentPack = 0;        

            while (true)
            {
                if (socketStream != null)
                {
                    SocketReceiveMessage(socketStream);
                }              

               
                if (Convert.ToChar(byRec[0]) == 'E')
                {               
                    fs.Dispose();
                    fs.Close();
                    break;
                }
                else if (Convert.ToChar(byRec[0]) == 'S')
                {

                    nCurrentPack = Convert.ToInt32(byRec[1]) * 256 + Convert.ToInt32(byRec[2]);

                    fs.Seek((nCurrentPack - 1) * PACKET_SIZE, SeekOrigin.Begin);

                    byBody = new byte[PACKET_SIZE];
                    if ((nCurrentPack == nPackCount) && (nLastPackSize != 0))
                    {
                        fs.Read(byBody, 0, nLastPackSize);
                    }
                    else
                    {
                        fs.Read(byBody, 0, PACKET_SIZE);              

                    }

                    if (socketStream != null)
                    {
                        SocketSendMessage(socketStream, byBody);
                    }
                    else
                    {
                        MessageBox.Show("socket");
                        return;
                    }
                }


            }

        }

------解决方案--------------------
public struct FilePack
{
/// <summary>
/// 数据包大小
/// </summary>
public ushort PackSize;

/// <summary>
/// 文件GUID
/// </summary>
public byte[] FileGuid; //16

///// <summary>
///// 包数据SHA1验证 ... 
///// </summary>
//public byte[] PackSHA1;

/// <summary>
/// 包总数量
/// </summary>
public ushort PackCount;

/// <summary>
/// 包号
/// </summary>
public ushort PackIndex;

/// <summary>
/// 包数据
/// </summary>
public byte[] PackData;

}

这样发啊...
------解决方案--------------------