Exemple #1
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) {
            }
        }
Exemple #2
0
 public judpClient(int port)
 {
     try {
         client = new UDTClient(port);
     } catch (SocketException e) {
         Console.WriteLine(e);
     } catch (Exception e) {
         Console.WriteLine(e);
     }
 }
Exemple #3
0
 public judpClient(string lcoalIP, int port)
 {
     try {
         client = new UDTClient(port, lcoalIP);
     } catch (SocketException e) {
         Console.WriteLine(e);
     } catch (Exception e) {
         Console.WriteLine(e);
     }
 }
Exemple #4
0
        static void Main(string[] args)
        {
            UDTClient client = new UDTClient();

            client.Connect("127.0.0.1", 7777);
            while (true)
            {
                client.send(System.Text.Encoding.Default.GetBytes(DateTime.Now.ToString()));
                Console.ReadLine();
            }
        }
Exemple #5
0
        public void RunReceive()
        {
            verbose = true;
            try
            {
                UDTReceiver.connectionExpiryDisabled = true;
                //IPAddress myHost = null;
                //if (localIP != "")
                //{
                //    myHost = IPAddress.Parse(localIP);
                //}
                //else
                //{
                //    string hostname = Dns.GetHostName();
                //    IPHostEntry hostip = Dns.GetHostEntry(hostname);
                //    foreach (IPAddress ipaddress in hostip.AddressList)
                //    {
                //        if (ipaddress.ToString().IndexOf(':') < 0)//存在IPV6的地址,所以要判断
                //        {
                //            myHost = ipaddress;
                //            break;
                //        }
                //    }
                //}
                UDTClient client = new UDTClient(localPort);
                client.connect(serverHost, serverPort);
                UDTInputStream  inputStream  = client.getInputStream();//报错未将对象引用设置到对象的实例。
                UDTOutputStream outputStream = client.getOutputStream();

                Log.Write(this.ToString(), "[ReceiveFile] Requesting file " + remoteFile);

                byte[] fName = Encoding.Default.GetBytes(remoteFile);

                //send file name info
                byte[] nameinfo = new byte[fName.Length + 4];
                System.Array.Copy(Util.encode(fName.Length), 0, nameinfo, 0, 4);
                System.Array.Copy(fName, 0, nameinfo, 4, fName.Length);

                outputStream.Write(nameinfo, 0, nameinfo.Length);
                outputStream.Flush();
                //pause the sender to save some CPU time
                outputStream.pauseOutput();

                //存放文件长度信息
                byte[] sizeInfo = new byte[8];

                int total = 0;
                while (total < sizeInfo.Length)
                {
                    int r = inputStream.Read(sizeInfo, 0, sizeInfo.Length);
                    if (r < 0)
                    {
                        break;
                    }
                    total += r;
                    Thread.Sleep(50);
                }
                long size = Util.decode(sizeInfo, 0);

                if (File.Exists(localFile))
                {
                    File.Delete(localFile);
                }

                Log.Write(this.ToString(), "[ReceiveFile] Write to local file <" + localFile + ">");

                FileStream os = new FileStream(localFile, FileMode.Create, FileAccess.Write);
                try
                {
                    Log.Write(this.ToString(), "[ReceiveFile] Reading <" + size + "> bytes.");
                    //( System.DateTime.UtcNow.Ticks - new DateTime(1970, 1, 1, 0, 0, 0).Ticks)/10000;
                    //如果要得到Java中 System.currentTimeMillis() 一样的结果,就可以写成 上面那样,也可以这样写:
                    // TimeSpan ts=new TimeSpan( System.DateTime.UtcNow.Ticks - new DateTime(1970, 1, 1, 0, 0, 0).Ticks);
                    //(long)ts.TotalMilliseconds;
                    TimeSpan ts_start = new TimeSpan(System.DateTime.UtcNow.Ticks - new DateTime(1970, 1, 1, 0, 0, 0).Ticks);
                    long     start    = (long)ts_start.TotalMilliseconds;

                    //接收文件数据
                    Util.copy(inputStream, os, size, false);

                    client.shutdown();

                    TimeSpan ts_end = new TimeSpan(System.DateTime.UtcNow.Ticks - new DateTime(1970, 1, 1, 0, 0, 0).Ticks);
                    long     end    = (long)ts_end.TotalMilliseconds;

                    double rate = 1000.0 * size / 1024 / 1024 / (end - start);    //接收所用时间

                    Log.Write(this.ToString(), "[ReceiveFile] Rate: " + Math.Round(rate, 3) + " MBytes/sec. " + Math.Round(8 * rate, 3) + " MBit/sec.");



                    if (verbose)
                    {
                        Log.Write(this.ToString(), client.getStatistics().toString());
                    }
                }
                finally
                {
                    os.Close();
                }
            }
            catch (Exception ex)
            {
                Log.Write(this.ToString(), "RunReceive", ex);
            }
        }
Exemple #6
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);
            }
        }