Cannot Assign Requested Address Using Serversocket.Socketbind

Cannot assign requested address using ServerSocket.socketBind

As other people have pointed out, it is most likely related to another process using port 9999. On Windows, run the command:

netstat -a -n | grep "LIST"

And it should list anything there that's hogging the port. Of course you'll then have to go and manually kill those programs in Task Manager. If this still doesn't work, replace the line:

serverSocket = new ServerSocket(9999);

With:

InetAddress locIP = InetAddress.getByName("192.168.1.20");
serverSocket = new ServerSocket(9999, 0, locIP);

Of course replace 192.168.1.20 with your actual IP address, or use 127.0.0.1.

java.net.BindException: Can't assign requested address on client's side

You're supplying a non-local IP address as the second argument, or 'port' is already in use locally. Why are you specifying a local address and port at all? Don't do that. Just remove those two parameters. Let the system decide them.

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.

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


Related Topics



Leave a reply



Submit