您的位置:首页 > 其它

脱坑之路-车站项目问题总汇

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: