TL;DR:我在Unity3D客户端应用程序和服务器上的UDP Python监听器之间出现通信问题。
我试图通过Unity 5.x NetworkTransport LLAPI和Python 3.x socket模块从Unity3D游戏客户端获取低级调用和响应。
目标:将发送到服务器的消息反弹回客户端。
问题:
1. 当我运行Unity3d客户端时,套接字打开并且服务器每秒打印一个新的recvfrom数据,但Unity从未收到反弹的数据。 2. 大约10秒后,客户端收到超时错误以及一个DisconnectEvent。
状态:
客户端:Unity 5.4
服务器:Amazon AWS,端口8888打开
服务器端Python应用程序:
客户端Unity网络类:
我试图通过Unity 5.x NetworkTransport LLAPI和Python 3.x socket模块从Unity3D游戏客户端获取低级调用和响应。
目标:将发送到服务器的消息反弹回客户端。
问题:
1. 当我运行Unity3d客户端时,套接字打开并且服务器每秒打印一个新的recvfrom数据,但Unity从未收到反弹的数据。 2. 大约10秒后,客户端收到超时错误以及一个DisconnectEvent。
状态:
客户端:Unity 5.4
服务器:Amazon AWS,端口8888打开
服务器端Python应用程序:
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(('', 8888))
print ('Listening on port 8888')
while True:
data, addr = s.recvfrom(4096)
if data:
for i in range(0, len(data)):
print (data[i])
print (data, addr)
s.sendto(data, addr)
客户端Unity网络类:
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
public class NetworkExecutor : MonoBehaviour
{
const string addr = IP_ADDR;
const int port = PORT;
bool connected = false;
// Doing lots of error checks but no need to save them. Let's def this and hold onto it.
byte e;
void Start ()
{
// Some testing involves waiting for responses or repetitious send/receive calls.
// We do this in coroutines for both efficiency and human sanity.
StartCoroutine(TestNetwork());
}
IEnumerator TestNetwork()
{
// Define network configurations.
ConnectionConfig config = new ConnectionConfig();
int reliableChannel = config.AddChannel(QosType.Reliable);
int maxConnections = 10;
HostTopology hostTopo = new HostTopology(config, maxConnections);
// Initialize and connect network with config.
NetworkTransport.Init();
int hostId = NetworkTransport.AddHost(hostTopo);
int connectionId = NetworkTransport.Connect(hostId, addr, port, 0, out e);
Debug.Log("<b>Connect.</b> Host ID: " + hostId + " Connection ID: " + connectionId);
ErrStr(e);
// Send test message.
byte[] msg = System.Text.Encoding.UTF8.GetBytes("Send string");
NetworkTransport.Send(hostId, connectionId, reliableChannel, msg, 4096, out e);
Debug.Log("<b>Send.</b> Msg: " + msg);
ErrStr(e);
// Receive test message.
int recHostId;
int recConnectionId;
int recChannelId;
int recSize;
msg = System.Text.Encoding.UTF8.GetBytes("Unmodified byte buffer.");
NetworkEventType eventType = NetworkTransport.Receive(out recHostId, out recConnectionId, out recChannelId, msg, 4096, out recSize, out e);
Debug.Log("<b>Receive.</b> Type: " + eventType + " Msg: " + System.Text.Encoding.UTF8.GetString(msg));
Debug.Log("(hID:" + recHostId + " cID:" + recConnectionId + " chId:" + recChannelId + " " + recSize + ")");
ErrStr(e);
NetworkTransport.Disconnect(hostId, connectionId, out e);
ErrStr(e);
yield break;
}
string ErrStr(byte e)
{
switch ((NetworkError)e)
{
case NetworkError.Ok:
return "Ok";
case NetworkError.WrongHost:
return "<color=red>Wrong Host</color>";
case NetworkError.WrongConnection:
return "<color=red>Wrong Connection</color>";
case NetworkError.WrongChannel:
return "<color=red>Wrong Channel</color>";
case NetworkError.NoResources:
return "<color=red>No Resources</color>";
case NetworkError.BadMessage:
return "<color=red>Bad Message</color>";
case NetworkError.Timeout:
return "<color=red>Timeout</color>";
case NetworkError.MessageToLong:
return "<color=red>Message Too Long</color>";
case NetworkError.WrongOperation:
return "<color=red>Wrong Operation</color>";
case NetworkError.VersionMismatch:
return "<color=red>Version Mismatch</color>";
case NetworkError.CRCMismatch:
return "<color=red>CRC Mismatch</color>";
case NetworkError.DNSFailure:
return "<color=red>DNS Failure</color>";
default:
return "<color=red><b>Big problem, we don't know this error code.</b></color>";
}
}
}
** 请原谅混乱。为了理解Unity和Python的最基本的低级网络使用,我违背了自己的天性、许多编码规范和良好实践。当能够建立原始信号量时,这将被丢弃并重新正确编写。
tshark
这样的工具检查UDP数据包是否实际发送和接收? - Klaus D.