Пример #1
0
 /**
  * 读取数据
  * 返回接收的字节大小
  */
 public int ReadData(byte[] data)
 {
     if (getCloseState())
     {
         return(-1);
     }
     try {
         int r = socket.GetInputStream().Read(data);
         //readLen+=r;
         return(r);
     } catch (IOException e) {
         Console.WriteLine(e);
     }
     return(-1);
 }
Пример #2
0
        static void Main(string[] args)
        {
            UDTServerSocket socket   = new UDTServerSocket(7777, "127.0.0.1");
            UDTSocket       dTSocket = socket.Accept();

            byte[] buffer = new byte[1470];
            while (!dTSocket.IsClose())
            {
                int r = dTSocket.GetInputStream().Read(buffer);
                Console.WriteLine(Encoding.Default.GetString(buffer, 0, r));
            }
        }
Пример #3
0
        /**
         * 启动接收
         */
        public bool Start()
        {
            if (!isStart || !isSucess)
            {
                FlashLogger.Warn("已经关闭的监听或监听端口不能使用");
                return(false);
            }
            Thread serverThread = new Thread(() =>
            {
                while (isStart)
                {
                    try
                    {
                        UDTSocket csocket = server.Accept();
                        try
                        {
                            csocket.GetInputStream().SetLargeRead(islagerRead);
                            csocket.GetInputStream().ResetBufMaster(isRWMaster);
                        }
                        catch (IOException e)
                        {
                            Console.WriteLine(e);
                        }

                        SocketControls.GetInstance().AddSocket(csocket);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                    }
                }
            });

            serverThread.IsBackground = true;
            serverThread.Name         = ("judpServer_" + DateTime.Now.Ticks / 10000);
            serverThread.Start();
            return(true);
        }
Пример #4
0
            public void Run()
            {
                try
                {
                    FlashLogger.Info("Handling request from " + socket.GetSession().Destination);
                    UDTInputStream  intStream = socket.GetInputStream();
                    UDTOutputStream outStream = socket.GetOutputStream();
                    byte[]          readBuf   = new byte[32768];

                    //read file name info
                    while (intStream.Read(readBuf) == 0)
                    {
                        Thread.Sleep(100);
                    }

                    //how many bytes to read for the file name
                    byte[] len = new byte[4];
                    //bb.get(len);

                    Array.Copy(readBuf, 0, 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 = Decode(len, 0);
                    if (verbose)
                    {
                        Console.WriteLine("[SendFile] name length     : " + length);
                    }
                    byte[] fileName = new byte[(int)length];
                    //读取文件名称
                    Array.Copy(readBuf, 4, fileName, 0, fileName.Length);
                    FileInfo file = new FileInfo(Encoding.UTF8.GetString(fileName));//兼容java
                    Console.WriteLine("[SendFile] File requested: '" + file.FullName + "'");

                    FileStream fis = null;
                    try
                    {
                        long size = file.Length;
                        Console.WriteLine("[SendFile] File size: " + size);
                        //send size info
                        outStream.Write(Encode64(size)); //先写入大小
                        outStream.Flush();               //传输完成

                        long start = DateTime.Now.Ticks;
                        fis = new FileStream(file.FullName, FileMode.Open);

                        CopyFile(fis, outStream, size, false);
                        Console.WriteLine("[SendFile] Finished sending data.");
                        long end = DateTime.Now.Ticks;
                        Console.WriteLine(socket.GetSession().Statistics.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.");
                    }
                    finally
                    {
                        socket.GetSender().Stop();
                        if (fis != null)
                        {
                            fis.Close();
                        }
                    }
                    FlashLogger.Info("Finished request from " + socket.GetSession().Destination);
                }
                catch (Exception ex)
                {
                    FlashLogger.Error(ex.Message);
                }
            }