Пример #1
0
        /*
         * 接收数据
         * 拷贝数据
         */
        internal static void CopySocketFile(FileStream fos, judpClient target, long size, bool flush)
        {
            byte[] buf  = new byte[bufsize];
            int    c    = 0;
            long   read = 0;

            while (true)
            {
                c = target.read(buf);
                if (c < 0)
                {
                    break;
                }
                try
                {
                    read += c;

                    fos.WriteAsync(buf, 0, c);
                    if (flush)
                    {
                        fos.Flush();
                    }
                    if (read >= size && size > -1)
                    {
                        break;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
            if (!flush)
            {
                try {
                    fos.Flush();
                } catch (IOException e) {
                    Console.WriteLine(e.Message);
                }
            }
        }
Пример #2
0
        /// <summary>
        /// 开始接收
        /// </summary>
        public void Start()
        {
            try
            {
                UDTReceiver.connectionExpiryDisabled = true;
                //UDTClient client=localPort!=-1?new UDTClient(myHost,localPort):new UDTClient(myHost);
                judpClient client = new judpClient(localIP, localPort);
                client.Connect(serverHost, serverPort);
                Console.WriteLine("[ReceiveFile] Requesting file " + remoteFile);
                string reqFile = remoteFile;
                string rspFile = localFile;
                Thread recfile = new Thread(() =>
                {
                    byte[] fName = Encoding.UTF8.GetBytes(remoteFile);

                    //send file name info
                    byte[] nameinfo = new byte[fName.Length + 4];
                    Array.Copy(ApplicationCode.Encode(fName.Length), 0, nameinfo, 0, 4);
                    Array.Copy(fName, 0, nameinfo, 4, fName.Length);
                    client.SendData(nameinfo);
                    client.PauseOutput();
                    //read size info (an 64 bit number)
                    byte[] sizeInfo = new byte[8];

                    int total = 0;
                    while (total < sizeInfo.Length)
                    {
                        int r = client.read(sizeInfo);
                        if (r < 0)
                        {
                            break;
                        }
                        total += r;
                    }
                    long size = ApplicationCode.decode(sizeInfo, 0);

                    //  File file = new File(new string(rspFile));
                    Console.WriteLine("[ReceiveFile] Write to local file <" + rspFile + ">");
                    FileStream fos = null;
                    try
                    {
                        fos = new FileStream(rspFile, FileMode.Append);

                        Console.WriteLine("[ReceiveFile] Reading <" + size + "> bytes.");
                        long start = DateTime.Now.Ticks;
                        ApplicationCode.CopySocketFile(fos, client, size, false);
                        long end    = DateTime.Now.Ticks;
                        double rate = 1000.0 * size / 1024 / 1024 / (end - start);
                        Console.WriteLine("[ReceiveFile] Rate: " + rate.ToString(format) + " MBytes/sec. "
                                          + (8 * rate).ToString(format) + " MBit/sec.");
                        Console.WriteLine("接收文件完成:" + rspFile);
                        client.Close();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex);
                    }
                    finally
                    {
                        try
                        {
                            fos.Close();
                        }
                        catch (IOException e)
                        {
                            Console.WriteLine(e);
                        }
                    }
                });
                recfile.IsBackground = true;
                recfile.Name         = ("文件接收_" + localFile);
                recfile.Start();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }