I am trying to establish the connection between my pc (windows11) and rpi 4 (buster v10). I have set rpi as an access point so that it has a fixed ip address, for example, and connected my pc to it. I wrote a script to test socket communcation:

import socket, pickle, sys

class RPiClient:
  def __init__(self, host, port):
      self.host, self.port = host, port
      self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

  def connect(self):
      self.socket.connect((self.host, self.port))

  def send_message(self, obj):

  def close(self):

class RPiServer:
  def __init__(self, host, port):
      self.host, self.port = host, port
      self.socket = self.socket()
      self.__data = []
      self.conn, self.address = None, None

  def start(self):
      print(f"creating server at {self.host}:{self.port}")
      self.socket.bind((self.host, self.port))
      self.conn, self.address = self.socket.accept()
  def recieve_data(self):
      if not self.conn or not self.address:
        assert 0, f"conn: {self.conn}, address: {self.address}"
      with self.conn:
        while True:
          data = self.conn.recv(1024)
          if nto data:
      return pickle.loads(b' '.join(self.__data))

  def close(self):
      print("Closing server")

def main():
  server = RPiServer("", 4160)
  print("waiting for pc connection...")
  except Exception as e:
  print("success connection")
  host = server.address[0]


The output is as follows:

waiting for pc connection...
creating server at
sever start listening for connection...

The program is stuck here due to socket.accept(). I have checked in my pc that the connection state to rpi is SYN_SENT, while rpi state is LISTENING without receiving any SYN attempt. I have also done the following steps:

  1. Check firewall rules

My rpi allows all connections and has no restrictions.

  1. Ping rpi and pc ip

I tried to ping on my pc and also ping (my pc ip) on rpi, both of them can ping each other successfully.

  1. Reboot rpi

Still do not work. I also try to connect different device to rpi, the socket connection is still failed.

It seems that the server, i.e., rpi cannot find client. Is it caused by port that I set as 4160? I use netstat -ano to check port on my pc and only found that connections to rpi via port 22 and 445 are established.

  • self.socket = self.socket() looks very wrong. There is no such method defined (presumably the second self. is a typo). Also, you should be using as the server's self.host address; this will bind to any interface with an IPv4 address and is the normal thing to do rather than hard coding a specific address. You still have to use the correct IP with the client. You can check if the server is really listening with lsof -i TCP:4160 on the Pi.
    – goldilocks
    Aug 25, 2023 at 16:29

2 Answers 2


As goldilocks has suggested you are not creating the socket correctly.
As a matter of coding style it is less error prone to use distinct names e.g. I use lsock to hold local socket.
I didn't try your code but the following is a fragment I use to create a socket server:-

# host = ""  # This is the standard loopback interface address (localhost)
host = ""  # accept connections on all available IPv4 interfaces
port = 31425  # Defines the port to listen on

lsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)   # create a TCP socket
lsock.bind((host, port))
print(f"Listening on {(host, port)}")

Try: your_socket.settimeout(3)

while True:
        client_socket, client_address = your_socket.accept()

        # ... your code

    except socket.timeout:

In any condition, the timeout is 3 seconds. While not accepted, it will be raised every 3 seconds if there is no client trying to connect.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.