You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Python 3.6客户端Socket报10061错:已存在服务端仍连接失败?

Troubleshooting ConnectionRefusedError with Python Socket Client

Let's tackle your questions step by step—this is a super common gotcha when starting out with sockets!

First Question: Is the error from missing a server, and do clients need a server to work?

Absolutely, that's exactly why you saw that initial error. TCP socket communication follows a client-server model—the client can't just connect to empty space. When your client tries to connect to 127.0.0.1:9090, there needs to be a server process actively listening on that exact IP and port to accept the connection. If there's no server running, your machine has no idea what to do with that connection request, so it actively refuses it (hence the ConnectionRefusedError: [WinError 10061]).

In short: Yes, you must use a client and server socket together for TCP communication. The client initiates the connection, the server accepts it, and then they can send data back and forth.

Second Question: Why the error persists even with the test.py server code?

Having the server function defined doesn't mean it's actually running—and even if it is, there are a few other possible issues. Let's break down the most likely causes:

  • The server function isn't being executed: The code you shared is a class method server(self), but just defining it won't make it run. You need to create an instance of that class and call the server() method before launching your client. For example, if the class is named TestServer, you'd need something like:

    if __name__ == "__main__":
        server_instance = TestServer()
        server_instance.server()
    

    Without this, no process is listening on port 9090.

  • Timing mismatch: If you run the client before the server finishes setting up, the server might not have reached the listen() or accept() call yet. Start the server first, wait a couple of seconds to let it bind the port and start listening, then run your client.

  • Hostname resolution mismatch: Your client uses 127.0.0.1 while the server binds to localhost. Usually these are the same, but in some setups, localhost might resolve to IPv6's ::1 instead of IPv4's 127.0.0.1. Try matching them—either change the client's hostname to 'localhost' or have the server bind to '127.0.0.1'.

  • Port stuck in TIME_WAIT: If a previous server run didn't close cleanly, the port might be held open temporarily. The server sets SO_REUSEADDR which should fix this, but you can check with netstat -ano | findstr :9090 on Windows to see if another process is using the port.

Quick note: Looking at the server code, once it accepts a connection, it enters a loop to receive data but never sends a response back. That would cause your client to hang on recv() eventually, but that's not the connection error you're seeing—so the main issue is definitely the server not running or not ready when the client connects.

Fix Steps to Try

  1. Ensure the server's server() method is actually being called (add the __main__ block if missing).
  2. Start the server first, wait a moment, then run the client.
  3. Verify client and server use the same IP/hostname and port.

内容的提问来源于stack exchange,提问作者Ahmed Shalaby

火山引擎 最新活动