0

Here is my Pi code:

socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ip = "PC's ip address"
port = 5005
serverAddress = (ip, port)
socket.bind(serverAddress)
socket.listen(1)
print("Waiting for connection")
connection, add = socket.accept()
data = connection.recv(2048)

Here is my PC code

socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ip = "PC's ip address"
port = 5005
serverAddress = (ip, port)
socket.connect(serverAddress)
message = "Hello".encode()
socket.send(message)

When I run the pi code (first), I get the following error "Bind Failed" and then it just waits. I then run the PC code, and I get the following error "[Winerror 10061] No connection could be made because the target machine actively refused it"

Has anyone ever run into this problem before? I have spent hours trying to figure this out and I have no idea what is wrong. I am guessing it has something to do with what IP address I am using and/or the configuration of Ethernet, but I really don't know. Any help would be appreciated. Thanks!

  • @Milliways What vital information am I omitting? And why do you need my IP? – John T Bradford Mar 31 at 22:37
  • This question does not belong to Raspberry Pi. You should better ask at stackoverflow.com – Ingo Apr 1 at 11:11
  • Welcome -- but general programming questions, including network programming in python, belong on our larger parent site, Stack Overflow. Please take the tour to understand better how the site works. – goldilocks Apr 1 at 15:57
1

On the Pi, you have to bind the socket to an IP address of one of the Pi's interfaces. You cannot bind it to the PC's IP address.
If you want the socket to accept connections from all of the Pi's interfaces, you can use the IP address 0.0.0.0 (or :: in IPv6).

On the PC, the server IP should be an IP address that the Pi is bound to.

Let's say the Pi is connected to two separate networks: one using WiFi (interface wlan0), and one using Ethernet (interface eth0).

In this case the Pi has (at least) two different IP addresses, one for each interface. Let's say that wlan0 has address 192.168.1.2/24 and eth0 has 192.168.2.2/24.

You can check the IP addresses of your Pi using the command ip a.

On the Pi, you can now bind to either 192.168.1.2 or to 192.168.2.2. This will determine from which of the two networks the socket can accept connections.
If you want to accept connections from both networks, you could use bind address 0.0.0.0.

Now let's say that your computer is connected to just the WiFi network, at an address 192.168.1.3. In that case, you first bind the Pi to address 192.168.1.2 (the address of its own interface that's connected to the same network as the PC). Then on the PC, you connect to the same address, 192.168.1.2.


The problem with the code you posted is that 1. the Pi tries to bind to the address of the PC, which is impossible because it's not the address of one of the Pi's interfaces, and 2. the PC tries to connect to itself, on a port that doesn't have a listening socket, so the connection is refused.

| improve this answer | |

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