TL; DR:我在Unity3D客户端应用程序和服务器上的UDP python侦听器之间进行通信时遇到问题。
我试图通过Unity 5.x NetworkTransport LLAPI和Python 3.x套接字模块从Unity3D游戏客户端获得一个低级别的呼叫和响应
目标: 将发送到服务器的消息退回到客户端。
问题:
状态:
客户:Unity 5.4
服务器:亚马逊AWS,端口8888打开
服务器端python app:
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最基本的低级网络使用。当可以建立原始信号量时,它将被丢弃并正确重写。