I would like to reach my Raspberry Pi 2 from my PC or from PC to Raspberry Pi 2 via Ethernet cable. I aim to send/receive simple data(like "Hello world") in that way. I`ve tried some python code below. When I determine PC as server, I get " OSError: [WinError 10049] The requested address is not valid in its context " error message and when I determine Raspberry Pi as server I receive " error: [Errno 99] Connot assign requested address " error message.

Obliviously I don`t receive any error when I determine local IP but code is broken on " conn, addr = s.accept() " line.

If I mention to Client side of this code, I do not get any error message for both but I realized that code is broken on "s.send(MESSAGE)" line.

I also tried this code on the link Server and client between PC and Raspberry Pi then same issue occur, I get same errors. I`ve been working to solve this for a long time. I hope some one can help me. Thank you so much for your help.


import socket

TCP_IP = '' # this IP of my pc. When I want raspberry pi 2`s as a client, I replace it with its IP ''
TCP_PORT = 5005
MESSAGE = "Hello, World!"

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
data = s.recv(BUFFER_SIZE)

print ("received data:", data)


import socket

TCP_IP = '' # this IP of my pc. When I want raspberry pi 2`s as a server, I replace it with its IP ''
TCP_PORT = 5005
BUFFER_SIZE = 20 # Normally 1024, but I want fast response

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((TCP_IP, TCP_PORT))

conn, addr = s.accept()
print ('Connection address:', addr)
while 1:
    data = conn.recv(BUFFER_SIZE)
    if not data: break
    print ("received data:", data)
    conn.send(data)  # echo

2 Answers 2


I tested your code, it is working perfectly. The problem lies in your setup

1st issue: "OSError: [WinError 10049] The requested address is not valid in its context" and "error: [Errno 99] Connot assign requested address" means that you have mixed up the addresses.
Fix: Try swapping the ip addresses (changes have to be done on both client and server code)

2nd issue: There might be a firewall blocking the communications
If running RPi as server: Use iptables -A INPUT -p tcp --dport 5005 -j ACCEPT
If running PC as server: Configure windows firewall to allow incoming TCP 5005 connections

  • Thank you for your help. I realized I get s.send(MESSAGE) TypeError: a bytes-like object is required, not 'str error when I run client side. And I solved it with modify code line in client side like this s.send((MESSAGE).encode()) Now when I test code in local IP ( It works pretty good. Aug 14, 2017 at 15:08
  • what do you mean by swap the IP addresses Feb 15, 2023 at 20:50

Make sure you write your client-server code correctly then in the Raspberry put the IP address of your PC or Linux machine (obtain it with ipconfig or ifconfig) and leave the severHost at host = 'nothing here'.

Then, the firewall needs to be authorized to allow it to be used. Use sudo install ufw if you're on Linux then use the PORT 5901, or to allow other ports for communication.

Look at this website: How To Set Up a Firewall with UFW on Ubuntu 18.04

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.