/// <summary> /// 停止服务 /// </summary> public void Stop() { if (udpFileServer != null && udpFileServer.Listened) { udpFileServer.CloseSock(); } udpFileServer = null; }
/// <summary> /// 开始服务 /// </summary> public void Start() { if (udpFileServer == null) { udpFileServer = new SockUDP(); udpFileServer.IsAsync = true; udpFileServer.DataArrival += new SockUDP.UDPEventHandler(udpFileServer_DataArrival); udpFileServer.Listen(port); } }
/// <summary> /// 开始 /// </summary> public void Start() { if (sockUDP != null) { return; //如果已经初始化套接字 ,则退出 } if (sockUDP == null) { sockUDP = new SockUDP(); sockUDP.DataArrival += new SockUDP.UDPEventHandler(sockUDP_DataArrival); sockUDP.Listen(0);//随机侦听 } IPEndPoint myLocalEP = null; System.Net.IPAddress[] ips = System.Net.Dns.GetHostAddresses(System.Net.Dns.GetHostName());//获得本机局域网IPV4地址 for (int i = ips.Length - 1; i >= 0; i--) { if (ips[i].GetAddressBytes().Length == 4) { myLocalEP = new IPEndPoint(ips[i], sockUDP.ListenPort);//获得本机局域网地址 break; } } UDPFramePacket packet = new UDPFramePacket(); packet.type = (byte)TransmitType.getRemoteEP;//获得公网主机信息 packet.PacketCount = 1; sockUDP.Send(ServerEP, packet.BaseData); #region 主机信息获取 时间计数器 int TimeCount = 0; System.Timers.Timer timer1 = new System.Timers.Timer(); timer1.Interval = 500; timer1.Enabled = true; timer1.Elapsed += delegate(object sender, System.Timers.ElapsedEventArgs e) { if (myRemoteEP == null && TimeCount < 10) //如果还未获得本机远程主机信息 { sockUDP.Send(ServerEP, packet.BaseData); //每隔500毫秒发送一次请求 } else//如果三秒内已经获得主机信息或三秒钟后未获得主机信息 { //终止发送,并触发获得主机事件 timer1.Enabled = false; timer1.Dispose(); timer1 = null; if (myRemoteEP == null) //如果超时未获得本机远程主机信息 { myRemoteEP = myLocalEP; //假设本机的远程IP等于本地IP } OnGetIPEndPoint(this, myLocalEP, myRemoteEP); //触发获取本机主机事件 } TimeCount++; }; #endregion ///开始组包线程 videoThread = new System.Threading.Thread(new System.Threading.ThreadStart(VideoDecoderThread)); videoThread.Start(); }
/// <summary> /// 开始 /// </summary> /// <param name="AllowResume">是否断点续传文件</param> public void Start(bool AllowResume) { System.IO.DirectoryInfo dInfo = new System.IO.DirectoryInfo(System.Windows.Forms.Application.StartupPath + "\\FileCache"); if (!dInfo.Exists) { dInfo.Create(); } CacheFile = System.Windows.Forms.Application.StartupPath + "\\FileCache\\" + TFileInfo.MD5; if (AllowResume)//如果断点续传 { FileInfo finfo = new FileInfo(CacheFile); if (finfo.Exists) //如果缓存文件存在,则触发可断点续传事件 { currGetPos = finfo.Length; //设置断点续传接收文件位置 } } else { File.Delete(CacheFile);//删除缓存文件 System.Threading.Thread.Sleep(100); } if (sockUDP != null) { return; //如果已经初始化套接字 ,则退出 } if (sockUDP == null) { sockUDP = new SockUDP(); sockUDP.IsAsync = true; //异步通信 sockUDP.DataArrival += new SockUDP.UDPEventHandler(sockUDP_DataArrival); sockUDP.Listen(0); //随机侦听 } IPEndPoint myLocalEP = null; System.Net.IPAddress[] ips = System.Net.Dns.GetHostAddresses(System.Net.Dns.GetHostName());//获得本机局域网IPV4地址 for (int i = ips.Length - 1; i >= 0; i--) { if (ips[i].GetAddressBytes().Length == 4) { myLocalEP = new IPEndPoint(ips[i], sockUDP.ListenPort);//获得本机局域网地址 break; } } UDPPacket ftMsg = new UDPPacket(); ftMsg.type = (byte)TransmitType.getRemoteEP;//获得公网主机信息 sockUDP.Send(ServerEP, ftMsg.BaseData); #region 与服务器保持通信 //System.Timers.Timer timer = new System.Timers.Timer(); //timer.Interval =30000; //timer.Enabled = true; //timer.Elapsed += delegate(object sender, System.Timers.ElapsedEventArgs e) //{ // if (sockUDP != null) // { // sockUDP.Send(ServerEP, ftMsg.BaseData); // } //}; #endregion #region 主机信息获取 时间计数器 int TimeCount = 0; System.Timers.Timer timer1 = new System.Timers.Timer(); timer1.Interval = 500; timer1.Enabled = true; timer1.Elapsed += delegate(object sender, System.Timers.ElapsedEventArgs e) { if (myRemoteEP == null && TimeCount < 10) //如果还未获得本机远程主机信息 { sockUDP.Send(ServerEP, ftMsg.BaseData); //每隔500毫秒发送一次请求 } else//如果三秒内已经获得主机信息或三秒钟后未获得主机信息 { //终止发送,并触发获得主机事件 timer1.Enabled = false; timer1.Dispose(); timer1 = null; if (myRemoteEP == null) //如果超时未获得本机远程主机信息 { myRemoteEP = myLocalEP; //假设本机的远程IP等于本地IP } OnGetIPEndPoint(this, myLocalEP, myRemoteEP); //触发获取本机主机事件 } TimeCount++; }; #endregion }