working examples
This commit is contained in:
58
check_connection.py
Normal file
58
check_connection.py
Normal file
@@ -0,0 +1,58 @@
|
||||
import socket
|
||||
import threading
|
||||
|
||||
PORT = 54000
|
||||
BROADCAST = "255.255.255.255"
|
||||
|
||||
udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
|
||||
udp_socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
|
||||
|
||||
interfaces = socket.getaddrinfo(host=socket.gethostname(), port=None, family=socket.AF_INET)
|
||||
for idx, ip in enumerate(interfaces):
|
||||
print(f"{idx}: {ip[-1][0]}")
|
||||
|
||||
interface = input("Który interfejs: ")
|
||||
ip_addr = interface[int(interface)][-1][0]
|
||||
udp_socket.bind((ip_addr, PORT))
|
||||
|
||||
def recive_bradcast():
|
||||
print("Nasluchuje....")
|
||||
t = threading.currentThread()
|
||||
while getattr(t, "do_run", True):
|
||||
data, addr = udp_socket.recvfrom(1024)
|
||||
print(f"from: {addr}, recive: {data}")
|
||||
|
||||
print("Przestalem nasluchiwac!")
|
||||
|
||||
recv_thread = threading.Thread(target=recive_bradcast)
|
||||
recv_thread.start()
|
||||
|
||||
|
||||
counter = 0
|
||||
while True:
|
||||
choice = input("Wyslac dane?[t/n] ")
|
||||
if choice.lower() == "n":
|
||||
run = False
|
||||
break
|
||||
elif choice.lower() != "t":
|
||||
continue
|
||||
|
||||
counter = counter + 1
|
||||
udp_socket.sendto(f"test connection, counter: {counter}".encode("utf8"), (BROADCAST, PORT))
|
||||
|
||||
recv_thread.do_run = False
|
||||
udp_socket.close()
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# print(interfaces[int(interface)][-1][0])
|
||||
# allips = [ip[-1][0] for ip in interfaces]
|
||||
# print(allips)
|
||||
|
||||
# print(socket.gethostbyname(socket.gethostname()))
|
||||
# print(socket.if_nameindex())
|
||||
# print(socket.if_nametoindex(socket.gethostname()))
|
||||
# print(interfaces)
|
||||
22
client.py
Normal file
22
client.py
Normal file
@@ -0,0 +1,22 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import socket
|
||||
|
||||
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) # UDP
|
||||
|
||||
# Enable port reusage so we will be able to run multiple clients and servers on single (host, port).
|
||||
# Do not use socket.SO_REUSEADDR except you using linux(kernel<3.9): goto https://stackoverflow.com/questions/14388706/how-do-so-reuseaddr-and-so-reuseport-differ for more information.
|
||||
# For linux hosts all sockets that want to share the same address and port combination must belong to processes that share the same effective user ID!
|
||||
# So, on linux(kernel>=3.9) you have to run multiple servers and clients under one user to share the same (host, port).
|
||||
# Thanks to @stevenreddie
|
||||
# client.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
|
||||
|
||||
# Enable broadcasting mode
|
||||
client.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
|
||||
|
||||
client.bind(("192.168.1.101", 37020))
|
||||
while True:
|
||||
# Thanks @seym45 for a fix
|
||||
data, addr = client.recvfrom(1024)
|
||||
# print("received message: %s" % data.decode("utf8"))
|
||||
print(f"from: {addr}, recive: {data}")
|
||||
30
server.py
Normal file
30
server.py
Normal file
@@ -0,0 +1,30 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
from re import T
|
||||
import socket
|
||||
import time
|
||||
|
||||
server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
|
||||
|
||||
# Enable port reusage so we will be able to run multiple clients and servers on single (host, port).
|
||||
# Do not use socket.SO_REUSEADDR except you using linux(kernel<3.9): goto https://stackoverflow.com/questions/14388706/how-do-so-reuseaddr-and-so-reuseport-differ for more information.
|
||||
# For linux hosts all sockets that want to share the same address and port combination must belong to processes that share the same effective user ID!
|
||||
# So, on linux(kernel>=3.9) you have to run multiple servers and clients under one user to share the same (host, port).
|
||||
# Thanks to @stevenreddie
|
||||
# server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
|
||||
|
||||
# Enable broadcasting mode
|
||||
server.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
|
||||
server.bind(("192.168.1.101", 5555))
|
||||
# Set a timeout so the socket does not block
|
||||
# indefinitely when trying to receive data.
|
||||
server.settimeout(0.2)
|
||||
message = "count: "
|
||||
count = 0
|
||||
while True:
|
||||
count = count + 1
|
||||
# msg = message.join(str(count)).encode("utf8")
|
||||
msg = f"{message} {count}".encode("utf8")
|
||||
server.sendto(msg, ("255.255.255.255", 37020))
|
||||
# print("message sent!", flush=True)
|
||||
time.sleep(1)
|
||||
Reference in New Issue
Block a user