1

What I am looking for is for my python server, which is just a private server that responds to client input, for when it starts to send it's IP address to every IP on a network on port 4005. I do not know a way to figure out exactly which IPs are valid to send to on a network.

Here is the code that I thought would work, but raises an exception:

File "E:\Python\server client comms\messageEveryIP.py", line 11, in <module>
    s.bind((curIP, listeningPort))
OSError: [WinError 10049] The requested address is not valid in its context

In my case it errors out on 192.168.1.2 because there is no machine on that IP.

import socket
host = socket.gethostbyname(socket.gethostname())
listeningPort = 4005

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

i = 1
while i < 255:
    curIP = '192.168.1.' + str(i)
    listeningAddress = (curIP, listeningPort)
    s.bind((curIP, listeningPort))
    s.sendto(host.encode('utf-8'), listeningAddress)
    s.close()
    i += 1
7
  • 1
    try and except the OSError and the code will run. might not be the best solution. Commented Aug 24, 2020 at 1:39
  • @echo It does remove the exception, but now the code just sends the data to 192.168.1.1 (my router) 255 ish times. Commented Aug 24, 2020 at 1:43
  • Exception: OSError, IP not valid in context 192.168.1.1 Exception: OSError, IP not valid in context Traceback (most recent call last): File "E:\Python\server client comms\messageEveryIP.py", line 13, in <module> s.bind((curIP, listeningPort)) OSError: [WinError 10049] The requested address is not valid in its context I added the exception print line myself. Commented Aug 24, 2020 at 1:46
  • you have to create new socket for every IP Commented Aug 24, 2020 at 1:53
  • bind() is used to assign socket to local network card (LAN/WiFi) in your computer - it is not used to connect to other computer. And you have to use IP of your network card or 0.0.0.0 to use all network cards in your computer. You should use '192.168.1.' + str(i) in sendto() Commented Aug 24, 2020 at 1:55

1 Answer 1

1

You had some mistakes and very unreadable names of variables.

  • bind() is used to assign server to local network card - not to client IP - and use it only once - before loop

  • don't close socket because (as I rember) it will need to create socket again


import socket

#server_ip = socket.gethostbyname(socket.gethostname()) # this gives me `127.0.1.1` because I have it in `/etc/hosts`
server_ip = '192.168.1.13'  # <-- IP of my WiFi card on server
server_port = 4005

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

#s.bind( (server_ip, server_port) ) # assign server to one local network card
s.bind( ('0.0.0.0', server_port) )  # assign server to all local network cards

text = f'{server_ip}:{server_port}'
print(text)

# --- loop ---

for i in range(1, 255):
    client_ip = f'192.168.1.{i}'
    client_port = 4005

    print(f'{client_ip}:{client_port}')

    s.sendto(text.encode('utf-8'), (client_ip, client_port))

# --- after loop ---

s.close()  # only if you will no use this socket any more
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.