脱坑之路-车站项目问题总汇
2016-03-17 22:12
375 查看
using System.Collections; using System.Collections.Generic; using System; using System.Net; using System.Net.Sockets; using System.Threading; using System.Text; using UnityEngine; public class UdpState { public UdpClient udpClient = null; public IPEndPoint ipEndPoint = null; public const int BufferSize = 1024; public byte[] buffer = new byte[BufferSize]; public int counter = 0; } public class UdpManager { public static bool messageSent = false; public static int listenPort = 13050; public static int remotePort = 15080; private IPEndPoint localEP = null; private IPEndPoint remoteEP = null; private UdpClient udpReceive = null; private UdpClient udpSend = null; private UdpState udpSendState = null; private UdpState udpReceiveState = null; Thread receiveThread = null; private int counter = 0; private static UdpManager udpManager; public byte[] msgBuffer =null; private bool sendDone = true; private bool receiveDone = true; private Device[] devices; public static UdpManager Instance { get{ if(udpManager == null){ Debug.Log("udpManager == null"); udpManager = new UdpManager(); } // Debug.Log("udpManager == null----if wai"); return udpManager; } } private UdpManager(){ // DataManager.msgbyteQueue.Clear (); // Create ("127.0.0.1", remotePort); } public void Create(string ip, int port) { Close (); localEP = new IPEndPoint(IPAddress.Any, listenPort); remoteEP = new IPEndPoint(IPAddress.Parse(ip), port); udpReceive = new UdpClient(localEP); udpSend = new UdpClient(); udpSendState = new UdpState(); udpSendState.ipEndPoint = remoteEP; udpSendState.udpClient = udpSend; udpReceiveState = new UdpState(); udpReceiveState.ipEndPoint = remoteEP; udpReceiveState.udpClient = udpReceive; receiveThread = new Thread(ReceiveMessages); receiveThread.Start(); } public void Close(){ receiveDone = true;//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! if (udpReceive != null) { udpReceive.Close (); } if (udpSend != null) { udpSend.Close (); } if (receiveThread != null) { receiveThread.Abort (); } } public void SendMsg(byte[] bytes) { udpSend.Connect(remoteEP); udpSend.BeginSend(bytes, bytes.Length, new AsyncCallback(SendCallback), udpSendState); } public void SendCallback(IAsyncResult iar) { Debug.Log("SendCallback ---- "); UdpState udpState = iar.AsyncState as UdpState; if (iar.IsCompleted) { Console.WriteLine("number of bytes sent: {0}", udpState.udpClient.EndSend(iar)); } } public void ReceiveMessages() { while (true) { if (receiveDone) { receiveDone = false; udpReceive.BeginReceive(new AsyncCallback(ReceiveCallback), udpReceiveState); //receiveDone.WaitOne(); } Thread.Sleep(10); } } public void ReceiveCallback(IAsyncResult iar) { UdpState udpState = iar.AsyncState as UdpState; if (iar.IsCompleted) { Debug.LogError("收到消息-1"); Byte[] receiveBytes = udpState.udpClient.EndReceive(iar, ref udpReceiveState.ipEndPoint); Debug.LogError("收到消息-2"); //string receiveString = Encoding.Unicode.GetString(receiveBytes); DataManager.msgbyteQueue.Enqueue(receiveBytes); Debug.LogError("收到消息-3"); receiveDone = true; } } }
<span style="white-space:pre"> </span>UdpManager.Instance.Create (strServerIP, UdpManager.remotePort); <span style="white-space:pre"> </span>UdpManager.Instance.SendMsg (TestDataManager.Make101Package ("127.0.0.1#"+UdpManager.listenPort));
<span style="white-space:pre"> </span>UdpManager.Instance.Create (strServerIP, UdpManager.remotePort); <span style="white-space:pre"> </span>UdpManager.Instance.SendMsg (TestDataManager.Make101Package ("127.0.0.1#"+UdpManager.listenPort));
当第二次建立udp并发送时,返回数据收不到,卡在了<span style="font-family: Arial, Helvetica, sans-serif;">ReceiveCallback函数中的endreceive中,原来是少写了感叹号那条语句,导致线程并没有立即终止。</span>
相关文章推荐
- Lost in the City
- 丑数
- zjnu 1739 PŠENICA()
- linux 目录或文件相关的命令
- Matplotlib 图例中文显示(大小修改)
- 用户使用时的权限问题 && 网卡图标出现“device not managed”
- iOS警示框—判断是否有空格和回车
- OPENWRT ON ARM-BASED PLATFORM (RASPBERRY PI 2)
- PHP中的命名空间
- 82.动态规划:背包问题
- javascript模块化详解
- 82.动态规划:背包问题
- Rotating Sentences
- 连续子数组的最大和
- react、redux什么的都用起来 【2】异步action和redux中间件
- 0317总结
- 多重背包-POJ 2392 Space Elevator
- 如何将代码上传到github
- CodeForce 168 C——Wizards and Trolleybuses
- 协方差的意义和计算公式