Unity3d 5.x的极简Python服务器
本文关键字:Python 服务器 Unity3d | 更新日期: 2023-09-27 17:59:07
TL;DR:我在Unity3D客户端应用程序和服务器上的UDP python侦听器之间通信时遇到问题。
我正试图通过Unity 5.x NetworkTransport LLAPI和Python 3.x套接字模块从Unity3D游戏客户端简单地获得低级别的调用和响应
目标:将发送到服务器的消息弹回到客户端。
问题:
- 当我运行Unity3d客户端时,套接字打开,服务器每秒打印一个新的recvfrom数据,但unity从未接收到反弹的数据
- 大约10秒后,客户端会收到一个Timeout错误和一个DisconnectEvent
状态:
客户:Unity 5.4
服务器:亚马逊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最基本的底层网络使用。当一个基元信号量可以建立时,它将被丢弃并正确重写。
UDP
和TCP
都是标准协议。这意味着,无论您使用什么编程语言,它们都应该能够相互通信。
您的python代码使用标准UDP代码。您的Unity代码是而不是。您使用的NetworkTransport
API仅用于两个Unity应用程序之间的通信。它是一个LLAPI库,它是建立在UDP之上的锡层。同样,它不是用于Unity和标准UDP连接之间的连接,而是用于两个Unity程序之间的连接。
要与python UDP代码通信,必须在C#代码中使用System.Net.Sockets
命名空间中的UdpClient
类。以下是Unity中UDP代码的一个示例。