Beispiel #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) {
            }
        }
Beispiel #2
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();
            }
        }
Beispiel #3
0
        public bool Connect(string ip, int port)
        {
            bool isSucess = false;

            if (client != null)
            {
                try
                {
                    client.Connect(ip, port);
                    isSucess = true;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            }

            return(isSucess);
        }
Beispiel #4
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);
            }
        }