首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法让C#服务器在同一网络上的不同计算机上很好地处理Python

无法让C#服务器在同一网络上的不同计算机上很好地处理Python
EN

Stack Overflow用户
提问于 2021-09-11 15:56:01
回答 1查看 121关注 0票数 1

对于为什么我不能让一个统一的C#服务器和python客户端在同一个网络上很好地发挥作用,有什么想法吗?

C#服务器

代码语言:javascript
复制
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客户端:

代码语言:javascript
复制
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,所以它已经停止了良好的运行。

我正在我的笔记本上运行python客户机,运行ubuntu18.08LTS,在我的windows 10桌面上统一运行C#服务器。

  • 我检查了IP和端口是否正常
  • 我检查了统一应用程序和构建的应用程序是否都有防火墙异常
  • 我已经在桌面上本地运行了python客户机,并让它正常工作
  • 我在笔记本电脑上尝试了相同的python客户机,但是在windows上使用了相同的结果

显然,这里的代码不是错误的,但这就留下了一个问题:什么是?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-09-11 19:35:21

我的问题的解决方案是使用这个有用的资源添加防火墙异常。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69144570

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档