【发布时间】:2015-03-10 18:08:56
【问题描述】:
我对 Unity 还很陌生,但我创建了一个应用程序,单击按钮即可将简单的 TCP 消息发送到一个非常简单的 TCP 服务器控制台应用程序。该代码在我的 PC 上运行时运行良好,但是当我为 Android 构建它并通过手机对其进行测试时,消息无法到达服务器。我做错了吗?简单的 TCP 交互是否与 android 不同?这是我的 Unity 代码:
public Texture btnTexture;
public GUIStyle style;
TcpClient mySocket = new TcpClient();
public NetworkStream theStream;
public String Host = "my_ip_address";
public Int32 Port = 8888;
public bool sent = false;
StreamWriter theWriter;
StreamReader theReader;
void OnGUI()
{
if (GUI.Button (new Rect ((Screen.width / 2) - Screen.width/4, (Screen.height / 2) - Screen.height/4,
Screen.width/2, Screen.width/2), btnTexture))
{
if(!sent)
{
mySocket = new TcpClient(Host, Port);
theStream = mySocket.GetStream();
theWriter = new StreamWriter(theStream);
theReader = new StreamReader(theStream);
sent = true;
}
theWriter.Write("TCP Message" + " $");
theWriter.Flush();
//Application.LoadLevel ("Page1");
}
}
【问题讨论】:
标签: android networking tcp unity3d tcpclient