Socket Bind Failed Errno = 99

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

Stripping things down to basics this is what you would want to test with:

import socket
server = socket.socket()
server.bind(("10.0.0.1", 6677))
server.listen(4)
client_socket, client_address = server.accept()
print(client_address, "has connected")
while True:
recvieved_data = client_socket.recv(1024)
print(recvieved_data)

This works assuming a few things:

  1. Your local IP address (on the server) is 10.0.0.1 (This video shows you how)
  2. No other software is listening on port 6677

Also note the basic concept of IP addresses:

Try the following, open the start menu, in the "search" field type cmd and press enter.
Once the black console opens up type ping www.google.com and this should give you and IP address for google. This address is googles local IP and they bind to that and obviously you can not bind to an IP address owned by google.

With that in mind, you own your own set of IP addresses.
First you have the local IP of the server, but then you have the local IP of your house.
In the below picture 192.168.1.50 is the local IP of the server which you can bind to.
You still own 83.55.102.40 but the problem is that it's owned by the Router and not your server. So even if you visit http://whatsmyip.com and that tells you that your IP is 83.55.102.40 that is not the case because it can only see where you're coming from.. and you're accessing your internet from a router.

Sample Image

In order for your friends to access your server (which is bound to 192.168.1.50) you need to forward port 6677 to 192.168.1.50 and this is done in your router.
Assuming you are behind one.

If you're in school there's other dilemmas and routers in the way most likely.

Bind error (99): Cannot assign requested address

You can only bind a socket to an address of a local interface.

Errno 99 - Cannot assign requested address

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

Python sockets: [Errno 99] when binding to ip on local network

First, let's deal with the error you are seeing. .bind() names the local end of the socket, not the remote. So the host part must refer to the local machine (e.g., 'localhost', '127.0.0.1, '192.168.1.194', or '' (wildcard for all local interfaces).) So, when you specify an address that isn't local to the machine running .bind(), you get an error.

Second, there is no way to "configure my socket to only receive packets from a specific address." As an alternative, you can use the returned address from .recvfrom() to ignore data you don't care about.

data, addr = s.recvfrom(1024)
if addr != '192.168.1.130':
continue


Related Topics



Leave a reply



Submit