无法将 thrift c# 客户端连接到 python 服务器

Can not connect thrift c# client to python server

我有 Thrift 客户端-服务器应用程序 C# 客户端和 Python 服务器。都在同一台机器上-Windows7。调试 Thrift 代码我看到客户端的套接字无法连接到服务器的本地服务器。
顺便说一句,同一个 C# 客户端连接到 C+ 服务器,python 和 c++ 客户端连接到同一个 python 服务器。只是 C#-->Python 组合失败。

问题看起来类似于 。我尝试按照上面 link 中的答案修改代码,但 C# 套接字仍然抛出 "No connection could be made because the target machine actively refused it 127.0.0.1:9091"


客户端(C#):

   IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
   IPAddress ipAddress = ipHostInfo.AddressList[1];
   IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
   Socket client = new Socket(ipAddress.AddressFamily,SocketType.Stream, 
   ProtocolType.Tcp);
   TcpClient tcpClient = new TcpClient(AddressFamily.InterNetwork);
   tcpClient.Client = client;
   tcpClient.Connect(ip, port);

为什么ipHostInfo.AddressList[1]?此选择采用 IPv4 适配器来自 配置。我也尝试其他指数。

服务器(Python,里面 到节俭服务器库):

res0 = socket.getaddrinfo(self.host,
                                          self.port,
                                          self._socket_family,
                                          socket.SOCK_STREAM,
                                          0,
                                          socket.AI_PASSIVE | 
    socket.AI_ADDRCONFIG)
    ...
    self.handle = socket.socket(res[0], res[1])
    self.handle.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    if hasattr(self.handle, 'settimeout'):
     self.handle.settimeout(None)
    self.handle.bind(res[4])
    self.handle.listen(self._backlog)
    client, addr = self.handle.accept()


如果是 C# 客户端

,它永远不会从 accept 退出

您的重复代码过多。尝试以下:

            //client
            IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
            IPAddress ipAddress = ipHostInfo.AddressList[1];
            IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
            TcpClient tcpClient = new TcpClient();
            tcpClient.Connect(remoteEP);

            //Server
            IPEndPoint localEP = new IPEndPoint(IPAddress.Any, port);
            TcpListener listener = new TcpListener(localEP);
            listener.Start();