日期:2014-05-17 浏览次数:20838 次
private Socket LocalSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); private int size = 1024*64; private Thread ListenThread; private byte[] bytData; /// <summary> /// 监听方法,用于监听远程发送到本机的信息 /// </summary> public void Listen() { ListenThread = new Thread(new ThreadStart(VideoListen)); ListenThread.Start(); } /// <summary> /// 监听线程 /// </summary> private void VideoListen() { bytData = new byte[size] while (true) { LocalSocket.BeginReceiveFrom(bytData, 0, bytData.Length, SocketFlags.None, ref ipeRemote, new AsyncCallback(ReceiveVideo), LocalSocket); } } /// <summary> /// 接收数据 /// </summary> /// <param name="iar"></param> private void ReceiveVideo(IAsyncResult iar) { int intRecv = 0; try { intRecv = LocalSocket.EndReceiveFrom(iar, ref ipeRemote); } catch { //throw new Exception(); } 第二次执行到LocalSocket.BeginReceiveFrom(bytData, 0, bytData.Length, SocketFlags.None, ref ipeRemote, new AsyncCallback(ReceiveVideo), LocalSocket);