示例#1
0
        /**
         * 读取数据
         * 只和split发送对应
         * @return
         */
        public byte[] ReadALL()
        {
            byte[] result = null;
            if (client != null)
            {
                byte[] readBytes = new byte[bufSize];//接收区
                int    r         = 0;
                try {
                    while (true)
                    {
                        if (client.isClosed())
                        {
                            return(null);
                        }
                        r = client.GetInputStream().Read(readBytes);
                        if (r == -1)
                        {
                            result = pack.GetData();
                            break;
                        }
                        else
                        {
                            if (r == 0)
                            {
                                try {
                                    Thread.Sleep(1000);

                                    continue;
                                } catch (Exception e) {
                                    Console.WriteLine(e);
                                }
                            }
                            //
                            byte[] buf = new byte[r];
                            Array.Copy(readBytes, 0, buf, 0, r);
                            if (pack.AddData(buf))
                            {
                                result = pack.GetData();
                                break;
                            }
                        }
                    }
                } catch (Exception e) {
                    Console.WriteLine(e);
                }
            }

            return(result);
        }
示例#2
0
        public void Run()
        {
            try{
                //open the UDPEndpoint
                UDTClient client    = new UDTClient();
                int       localPort = client.GetEndpoint().LocalPort;
                //write the port to output
                WriteToOut("OUT: " + localPort);

                //read peer port from input file or stdin
                string peerPortS  = ReadFromIn();
                int    serverPort = int.Parse(peerPortS);

                //connect...
                client.Connect(serverIP, serverPort);
                var inStream = client.GetInputStream();

                //read file size info (an 4-byte int)
                byte[] sizeInfo = new byte[4];
                inStream.Read(sizeInfo);
                long size = BitConverter.ToInt32(sizeInfo, 0);

                //now read file data
                FileStream fos = new FileStream(localFile, FileMode.Append);
                try{
                    Util.CopyFileReceiver(fos, inStream, size, false);
                }finally{
                    fos.Close();
                }
            }catch (Exception ex) {
            }
        }
示例#3
0
        public void Run()
        {
            Configure();
            verbose = true;
            try{
                UDTReceiver.connectionExpiryDisabled = true;

                UDTClient client = new UDTClient(localPort, localIP);
                client.Connect(this.serverHost, this.serverPort);
                UDTInputStream  inStream  = client.GetInputStream();
                UDTOutputStream outStream = client.GetOutputStream();
                Console.WriteLine("[ReceiveFile] Requesting file " + remoteFile);
                byte[] fName = Encoding.UTF8.GetBytes(remoteFile);//兼容java
                //send file name info
                byte[] nameinfo = new byte[fName.Length + 4];
                Array.Copy(Encode(fName.Length), 0, nameinfo, 0, 4);
                Array.Copy(fName, 0, nameinfo, 4, fName.Length);
                outStream.Write(nameinfo);
                outStream.Flush();
                //pause the sender to save some CPU time
                outStream.PauseOutput();

                //read size info (an 64 bit number)
                byte[] sizeInfo = new byte[8];

                int total = 0;
                while (total < sizeInfo.Length)
                {
                    int r = inStream.Read(sizeInfo);
                    if (r < 0)
                    {
                        break;
                    }
                    total += r;
                }
                //读取文件长度
                long size = Decode(sizeInfo, 0);

                FileInfo file = new FileInfo(localFile);
                Console.WriteLine("[ReceiveFile] Write to local file <" + file.FullName + ">");
                FileStream fos = new FileStream(file.FullName, FileMode.Append);     //准备写入文件

                try{
                    Console.WriteLine("[ReceiveFile] Reading <" + size + "> bytes.");
                    long start = DateTime.Now.Ticks;
                    //and read the file data
                    //Util.copy(in, os, size, false);
                    CopyFile(fos, inStream, 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.");

                    client.Shutdown();

                    if (verbose)
                    {
                        Console.WriteLine(client.GetStatistics());
                    }
                }finally{
                    fos.Close();
                }
            }catch (Exception ex) {
                //throw new RuntimeException(ex);
            }
        }