Example #1
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);
            }
        }
Example #2
0
            public void run()
            {
                try
                {
                    // FlashLogger.Info("Handling request from "+socket.);

                    byte[] readBuf = new byte[32768];

                    MemoryStream stream = new MemoryStream(readBuf);
                    //read file name info
                    int r = 0;
                    while (true)
                    {
                        r = socket.ReadData(readBuf);
                        if (r == 0)
                        {
                            Thread.Sleep(100);
                        }
                        else
                        {
                            break;
                        }
                    }
                    if (r == -1)
                    {
                        socket.Close();
                        return;
                    }
                    //how many bytes to read for the file name
                    byte[] len = new byte[4];
                    stream.Read(len, 0, 4);
                    if (verbose)
                    {
                        StringBuilder sb = new StringBuilder();
                        for (int i = 0; i < len.Length; i++)
                        {
                            sb.Append(len[i].ToString());
                            sb.Append(" ");
                        }
                        Console.WriteLine("[SendFile] name length data: " + sb.ToString());
                    }
                    long length = ApplicationCode.decode(len, 0);
                    if (verbose)
                    {
                        Console.WriteLine("[SendFile] name length: " + length);
                    }
                    byte[] fileName = new byte[(int)length];
                    stream.Read(fileName, 0, fileName.Length);
                    string file = Encoding.UTF8.GetString(fileName);
                    Console.WriteLine("[SendFile] File requested: '" + file + "'");

                    Thread.CurrentThread.Name = ("sendFile_" + file);
                    FileStream fis = new FileStream(file, FileMode.Open);
                    try
                    {
                        long size = fis.Length;
                        fis.Close();
                        Console.WriteLine("[SendFile] File size: " + size);
                        //send size info
                        socket.SendData(ApplicationCode.encode64(size));

                        long start = DateTime.Now.Ticks;
                        //                    //and send the file

                        ApplicationCode.CopySocketFile(file, socket, socket.GetDataStreamLen());

                        Console.WriteLine("[SendFile] Finished sending data.");
                        long end = DateTime.Now.Ticks;
                        //System.out.println(socket.getSession().getStatistics().toString());
                        double rate = 1000.0 * size / 1024 / 1024 / (end - start);
                        Console.WriteLine("[SendFile] Rate: " + rate.ToString(format) + " MBytes/sec. " + (8 * rate).ToString(format) + " MBit/sec.");
                        //                    if(Boolean.getBoolean("udt.sender.storeStatistics")){
                        //                        socket.getSession().getStatistics().writeParameterHistory(new File("udtstats-"+System.currentTimeMillis()+".csv"));
                        //                    }
                    }
                    finally
                    {
                        socket.Close();
                        if (fis != null)
                        {
                            fis.Close();
                        }
                    }
                    FlashLogger.Info("Finished request from " + socket.GetDestination());
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
            }