TL; DR:Unity3Dクライアントアプリケーションとサーバ上のUDP pythonリスナー間の通信に問題があります。私は単にユニティ5.xのNetworkTransport LLAPIとPython 3.xのソケットモジュールを介してUnity3Dゲームのクライアントからの低レベルのコール&レスポンスを取得しようとしているMinimalist Python Server for Unity3d 5.x
目標:送信されたメッセージをバウンス サーバーに戻してクライアントに戻します。
問題:
- ソケットが開いて、私はUnity3dクライアントを実行すると、サーバーは、毎秒新しいのrecvfromデータを印刷しますが、統一はバウンスデータを受け取ることはありません。
- 〜10秒後、クライアントはDisconnectEventと共にTimeoutエラーを受け取ります。
ステータス:
クライアント:ユニティ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)
クライアント側のユニティネットワーキングクラス:
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パッケージが 'tshark'のようなツールで実際に送受信されているかどうかチェックしましたか? –
WireSharkのようなツールでパッケージを受け取っているかどうか確認してください。パッケージが表示された場合は、考えられる原因の1つにレスポンスが必要な場合があります。単純にバックストリングに返信すると、有効な応答ではない可能性があります。 – Joshua