public void Start() { Task.Factory.StartNew(() => { Configure(); try { UDTReceiver.connectionExpiryDisabled = true; UDTServerSocket server = new UDTServerSocket(serverPort, localIP); while (true) { UDTSocket socket = server.Accept(); Thread.Sleep(1000); Task.Factory.StartNew(() => { new RequestRunner(socket).Run(); }); } } catch (Exception ex) { // throw new RuntimeException(ex); } }); }
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)); } }
public void Run() { try { //create an UDTServerSocket on a free port UDTServerSocket server = new UDTServerSocket(0); // do hole punching to allow the client to connect IPAddress clientAddress = IPAddress.Parse(clientIP); IPEndPoint point = new IPEndPoint(clientAddress, clientPort); //发送一字节确认端口 Util.DoHolePunch(server.getEndpoint(), point); int localPort = server.getEndpoint().LocalPort; //获取真实端口 //output client port writeToOut("OUT: " + localPort); //now start the send... UDTSocket socket = server.Accept(); UDTOutputStream outStream = socket.GetOutputStream(); FileInfo file = new FileInfo(localFilename); if (!file.Exists) { Console.WriteLine("没有文件:" + localFilename); socket.Close(); server.ShutDown(); return; } FileStream fis = new FileStream(localFilename, FileMode.Open); try { //send file size info long size = fis.Length; PacketUtil.Encode(size); outStream.Write(PacketUtil.Encode(size)); long start = DateTime.Now.Ticks; //and send the file Util.CopyFileSender(fis, outStream, size, false); long end = DateTime.Now.Ticks; Console.WriteLine(socket.GetSession().Statistics); float mbRate = 1000 * size / 1024 / 1024 / (end - start); float mbitRate = 8 * mbRate; Console.WriteLine("Rate: " + (int)mbRate + " MBytes/sec. " + mbitRate + " mbit/sec."); } finally { fis.Close(); socket.Close(); } } catch (Exception ex) { } }
public void RunSend() { 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; // } // } //} UDTServerSocket server = new UDTServerSocket(serverPort); ThreadPool.SetMaxThreads(1, 1); while (true) { //启动监听(第一次),及获取已经存在的连接信息 UDTSocket socket = server.Accept(); Thread.Sleep(1000); TaskInfo ti = new TaskInfo(socket, verbose); ThreadPool.QueueUserWorkItem(new WaitCallback(run), ti); } } catch (Exception ex) { Log.Write(this.ToString(), ex); } }
/** * 启动接收 */ 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); }