我正在开发一个需要连接到服务器才能接收消息的应用。我正在使用TCP / IP协议,因为我需要建立以前的连接。由于System.Net.Sockets库的问题,我使用的是Windows.Networking.Socket库。
使用上述协议的代码如下:
using System;
using System.IO;
using HoloToolkit.Unity;
using UnityEngine;
#if !UNITY_EDITOR
using System.IO;
using System.Threading.Tasks;
using Windows.Networking;
using Windows.Networking.Sockets;
#endif
/// <summary>
/// TcpNetworkClientManager class provides methods to
/// send and receive messages asynchronously
/// </summary>
public class TcpNetworkClientManager : Singleton<TcpNetworkClientManager>
{
#region PRIVATE_MEMBERS_VARIABLES
[SerializeField]
[Tooltip("The Ipv4 address of the machine to connect")]
private string ip = "127.0.0.1";
[SerializeField]
[Tooltip("The connection port on the machine to use")]
private int port = 9999;
/// <summary>
/// The message we receive
/// </summary>
private string message;
/// <summary>
/// Tracks if a client is connected
/// </summary>
private bool clientConnected;
/// <summary>
/// Flag to receive message
/// </summary>
private bool receiveFlag;
#if !UNITY_EDITOR
/// <summary>
/// Supports network communication
/// </summary>
StreamSocket socket;
/// <summary>
/// Stream of data to write
/// </summary>
private StreamWriter writer = null;
/// <summary>
/// Stream of data to read
/// </summary>
private StreamReader reader = null;
#endif
#endregion // PRIVATE_MEMBERS_VARIABLES
#region PUBLIC_MEMBERS_VARIABLES
public string Ip
{
get
{
return ip;
}
set
{
ip = value;
}
}
public int Port
{
get
{
return port;
}
set
{
port = value;
}
}
public string Message
{
get
{
return message;
}
set
{
message = value;
}
}
public bool ClientConnected
{
get
{
return clientConnected;
}
}
#endregion // PUBLIC_METHODS
#region MONOBEHAVIOUR_METHODS
protected override void Awake()
{
base.Awake();
Message = string.Empty;
clientConnected = false;
}
#endregion // MONOBEHAVIOUR_METHODS
#region PUBLIC_METHODS
/// <summary>
/// Connects to a tcp server using TCP/IP protocol
/// </summary>
public void Connect()
{
receiveFlag = true;
#if !UNITY_EDITOR
Task.Run(async () => {
try
{
socket = new StreamSocket();
await socket.ConnectAsync(new HostName(Ip), Port.ToString());
clientConnected = true;
writer = new StreamWriter(socket.OutputStream.AsStreamForWrite());
reader = new StreamReader(socket.InputStream.AsStreamForRead());
await Receive();
}
catch(Exception)
{
Debug.Log("Unable to connecto to server");
}
});
#endif
Debug.LogWarningFormat("Tcp connect not supported in editor");
}
/// <summary>
/// Sends asynchronously a message using TCP/IP protocol
/// </summary>
/// <param name="data"></param>
public void Send(string messageToSend)
{
#if !UNITY_EDITOR
if (writer != null) Task.Run(async () =>
{
try
{
await writer.WriteAsync(messageToSend);
await writer.FlushAsync();
Debug.Log("Sent: " + messageToSend);
}
catch(Exception)
{
Debug.Log("Unable to send message to server");
}
});
#endif
Debug.LogWarningFormat("Tcp send not supported in editor");
}
/// <summary>
/// Frees resources both reader and writer
/// </summary>
public void Close()
{
#if !UNITY_EDITOR
receiveFlag = false;
if (writer != null) writer.Dispose();
writer = null;
if (reader != null) reader.Dispose();
reader = null;
#endif
Debug.LogWarningFormat("Tcp close not supported in editor");
}
#endregion // PUBLIC_METHODS
#region PRIVATE_METHODS
/// <summary>
/// Receives asynchronously a message using TCP/IP protocol
/// </summary>
#if !UNITY_EDITOR
private async Task Receive()
{
if (reader != null)
{
while (receiveFlag)
{
try
{
if (clientConnected)
{
string messageToReceive = await reader.ReadToEndAsync();
if (messageToReceive != null)
{
Message = messageToReceive;
Debug.Log("Received: " + message);
}
}
}
catch (Exception)
{
Debug.Log("Unable to receive from server");
}
}
}
}
#endif
#endregion // PRIVATE_METHODS
}
虽然我可以连接到服务器,但我什么都收不到。我知道Receive()函数到达
string messageToReceive = await reader.ReadToEndAsync();
但是没有调用下一个语句,也就是说,线程正在等待数据,但是数据永远不会被接收。
有谁知道我遇到了什么麻烦?
提前致谢, 克里斯蒂安。