1

Unity C# サーバーと python クライアントを同じネットワーク上でうまく再生できない理由について何か考えはありますか?

C# サーバー

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

using System.Threading;

using UnityEngine;

public class Wiiboard : MonoBehaviour
{
    public int port;

    public void StartClient()
    {
        TcpListener server = null;
        try
        {
            IPAddress localAddr = IPAddress.Parse("0.0.0.0");

            // TcpListener server = new TcpListener(port);
            server = new TcpListener(localAddr, port);

            // Start listening for client requests.
            server.Start();

            // Buffer for reading data
            Byte[] bytes = new Byte[256];
            String data = null;

            // Enter the listening loop.
            while (true)
            {
                Debug.Log("Waiting for a connection... ");

                // Perform a blocking call to accept requests.
                // You could also use server.AcceptSocket() here.
                TcpClient client = server.AcceptTcpClient();
                Debug.Log(String.Format("Connected!"));

                data = null;

                // Get a stream object for reading and writing
                NetworkStream stream = client.GetStream();

                int i;

                // Loop to receive all the data sent by the client.
                while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                {
                    // Translate data bytes to a ASCII string.
                    data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
                    Debug.Log(String.Format("Received: {0}", data));

                    // Process the data sent by the client.
                    data = data.ToUpper();

                    byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);

                    // Send back a response.
                    stream.Write(msg, 0, msg.Length);
                    Debug.Log(String.Format("Sent: {0}", data));
                }

                // Shutdown and end connection
                client.Close();
            }
        }
        catch (SocketException e)
        {
            Debug.LogError(String.Format("SocketException: {0}", e));
        }
        finally
        {
            // Stop listening for new clients.
            server.Stop();
        }

        Debug.Log(String.Format("\nHit enter to continue..."));
        Console.Read();
    }

    // Start is called before the first frame update
    void Start()
    {
        Thread t = new Thread(new ThreadStart(StartClient));
        t.Start();
    }
}

Python クライアント:

HOST = '192.168.0.38'  # The server's hostname or IP address
PORT = 25565        # The port used by the server

#https://realpython.com/python-sockets/
def client():
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        print(f"CLIENT >>> waiting to connect to {HOST}:{PORT}")
        s.connect((HOST, PORT))
        s.sendall(b'Hello, world!<EOF>')
        data = s.recv(1024)

    print('CLIENT >>> Received', repr(data))

[other, entirely irrelevant code. just stuff for the wiiboard]

client()

コンテキストとして、ラップトップに接続されたWiiバランスボードからデータをリレーしようとしています(聞いたところ、Windowsはボードでうまく機能せず、VRはLinuxでうまく機能しません)ソケットを介してデスクトップに団結へのつながり。ラップトップで Windows を実行しているときはソケットに問題はありませんでしたが、このためにラップトップで Linux を実行する必要があったため、正常に動作しなくなりました。

ubuntu 18.08 LTSを実行しているラップトップでpythonクライアントを実行しており、Windows 10デスクトップでC#サーバーをユニティで実行しています。

  • IPとポートに問題がないことを確認しました
  • Unity とビルドされたアプリケーションの両方にファイアウォールの例外があることを確認しました
  • デスクトップでローカルにpythonクライアントを実行し、動作させました
  • ラップトップで同じpythonクライアントを試しましたが、Windowsでも同じ結果が得られました

ここで明らかにコードに問題はありませんが、問題は何ですか?

4

1 に答える 1

1

私の問題の解決策は、この役立つリソースを使用してファイアウォールの例外を追加することでした

于 2021-09-11T19:35:21.020 に答える