Errno 99 - Cannot assign requested address

Isaac Sekamatte

I am trying to connect to IP address to receive data. The IP belongs to an AWS ec2 instance and we opened a UDP port on the server. Every time I try to bind, I get this error [Errno 99] Cannot assign requested address

UDP_IP = "XX.XXX.XX.XX"
UDP_PORT = YYY

sock = socket.socket(socket.AF_INET,  # Internet
                     socket.SOCK_DGRAM)  # UDP

sock.bind((UDP_IP, UDP_PORT)) # this is the line throwing the error

# sock.setblocking(0)
while True:
    data, address = sock.recvfrom(4096)
    print("received message:", data)

I have been to goggle around, and some answers suggested that the error message meant that IP should be local, other answers suggested using connect instead of bind which works but I don't receive any messages.

Mark Tolonen

Generally, sock.bind(('',port)) is all you need to receive messages on a port. It means "listen for incoming packets that port on all interfaces" You don't give an idea what 'XX.XXX.XX.XX' refers but you don't have to be specific unless you only want to listen to a specific interface. You don't bind when sending to a port, you just sock.sendto(msg,(serverip,port)). The host receiving the packet also gets the address it was sent from and can .sendto() that address for a reply.

Here's an example client/server interaction:

server.py

import socket

HOST = '' # receive on all interfaces
PORT = 5000

server = socket.socket(type=socket.SOCK_DGRAM)
server.bind((HOST,PORT))
while True:
    data,(ip,port) = server.recvfrom(4096)
    msg = data.decode()
    print(f'from {ip}:{port}> {msg}')
    if msg == 'quit': break # terminate server
    server.sendto(f'Response: <{msg}>'.encode(), (ip,port))
print('SERVER EXIT')

client.py

import socket

SERVER = 'localhost' # or specific IP of host accessible by client
PORT = 5000

client = socket.socket(type=socket.SOCK_DGRAM)
while True:
    msg = input('Client> ')
    client.sendto(msg.encode(), (SERVER,PORT))
    if not msg or msg == 'quit': break # empty message to quit client only
    response,addr = client.recvfrom(4096)
    print(response.decode())
print('CLIENT EXIT')

Demo (client):

C:\> client
Client> test1
Response: <test1>
Client> test2
Response: <test2>
Client>                       # empty message only quits client
CLIENT EXIT

C:\> client                   # 2nd client
Client> test3
Response: <test3>
Client> 你好!
Response: <你好!>
Client> quit                  # quit client and server
CLIENT EXIT

Demo (server):

C:\> server
from 127.0.0.1:49164> test1
from 127.0.0.1:49164> test2
from 127.0.0.1:49164>
from 127.0.0.1:49165> test3   # note client port change for 2nd client
from 127.0.0.1:49165> 你好!
from 127.0.0.1:49165> quit
SERVER EXIT

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

OSError: [Errno 99] Cannot assign requested address

Raspberry Pi TCP socket - [Errno 99] Cannot assign requested address

Nginx on Kubernetes (99: Cannot assign requested address)

Bind error (99): Cannot assign requested address

Docker + django_extensions + jupyter: OSError: [Errno 99] Cannot assign requested address

socket.error:[errno 99] cannot assign requested address and namespace in python

socket.error:[errno 99] cannot assign requested address : flask and python

Mesos bind error : Cannot assign requested address [99]

PHP - memcache randomly fails with - Cannot assign requested address (99)

Cannot connect to redis://localhost:6379/0: Error 99 connecting to localhost:6379. Cannot assign requested address

Flask, cannot assign requested address

Docker Cannot assign requested address

RedisInsight on Docker and Redis on Docker: Could not connect: Error 99 connecting to localhost:6379. Cannot assign requested address

CentOS8 nginx: [emerg] bind() failed (99: Cannot assign requested address)

redis.exceptions.ConnectionError: Error 99 connecting to localhost:6379. Cannot assign requested address

Puma Error: Cannot assign requested address - bind(2) for "10.0.2.2" port 3000 (Errno::EADDRNOTAVAIL)

Cannot assign requested address (when using Docker)

How to fix 'Cannot assign requested address?'

gin-gonic cannot assign requested address

Netty SocketIO - Cannot assign requested address in AWS

requests, cannot assign requested address, out of ports?

Docker for Windows: cannot assign requested address

socket.error: [Errno 49] Can't assign requested address

Set MAC address fails - RTNETLINK answers: Cannot assign requested address

bind: cannot assign requested address on UDPclient in local network

Server binding results in error "Cannot assign requested address"?

"Cannot assign requested address" when trying to set TUN interface netmask

Cannot Assign requested Address in AWS - JAVA Server Socket

MongoDB Replica Set setup is giving - SocketException: Cannot assign requested address