脱坑之路-车站项目问题总汇
来源:互联网 发布:Mac版犀牛授权码 编辑:程序博客网 时间:2024/05/17 03:56
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>
0 0
- 脱坑之路-车站项目问题总汇
- cocos2dx 3.2+ 项目创建与问题总汇
- 车站调度问题
- 问题 G: 车站调度
- 问题总汇
- 问题总汇
- 海外项目访问facebook和twitter问题总汇
- 项目EasyUi和JS中遇到的问题总汇
- 车站
- 车站
- iOS开发之开源项目库总汇
- Maven项目错误总汇
- Jstvu毕业设计问题总汇
- eclipse问题总汇
- Jcreator问题总汇
- UltraEdit问题总汇
- FLEX问题总汇
- FLEX问题总汇(1)
- 二叉搜索树的后序遍历序列
- OPENWRT ON ARM-BASED PLATFORM (RASPBERRY PI 2)
- 意图与逻辑——STL
- 《leetCode》:Remove Linked List Elements
- Java流程控制语句
- 脱坑之路-车站项目问题总汇
- Hibernate 缓存机制
- Android架构图
- 二叉树中和为某一值的路径
- redo log & archive
- Matlab的regionprops详解
- leetcode112.PathSum
- 二叉搜索树与双向链表
- Toolbar完整封装