So I am making a simple script which just sends messages from a client to the server. When the client and the server is on the same computer, the script runs perfectly but when I use another laptop and try to send a message to the server it shows this error on the client laptop.
server script:
import threading
HEADER = 64
PORT = 5000
SERVER = '######'
ADDR = ('', PORT)
FORMAT = 'utf-8'
DISCONNECT_MSG = '//*DISCONNECT*//'
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server.bind(ADDR)
def handle_client(conn, addr):
print(f'[NEW CONNECTION] {addr}')
connected = True
while connected:
msg_len = conn.recv(HEADER).decode(FORMAT)
if msg_len:
msg_len = int(msg_len)
msg = conn.recv(msg_len).decode(FORMAT)
if msg == DISCONNECT_MSG:
connected = False
print(f'[{addr}] {msg}')
conn.close()
def start():
print(f'[LISTENING] listening on {SERVER}')
server.listen()
while True:
conn, addr = server.accept()
# hc for hand client
thread_hc = threading.Thread(target=handle_client, args=(conn, addr))
thread_hc.start()
print(f'[ACTIVE CONNECTIONS] {threading.activeCount() - 1}')
print('[STARTING] server is starting.....')
start()
client script:
HEADER = 64
PORT = 5000
FORMAT = 'utf-8'
DISCONNECT_MSG = '//*DISCONNECT*//'
SERVER = '######'
ADDR = (SERVER, PORT)
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(ADDR)
def send(msg):
message = msg.encode(FORMAT)
msg_length = len(message)
send_length = str(msg_length).encode(FORMAT)
send_length += b' ' * (HEADER - len(send_length))
client.send(send_length)
client.send(message)
try:
while True:
msg = input('message: ')
send(msg)
if msg == DISCONNECT_MSG:
break
except BrokenPipeError:
print('the connection was broken')
Can you tell me what is the problem in the code. I am new to sockets in python, so if I am making any rookie mistakes, please overlook it.
getaddrinfo failed
is an error that means the OS couldn't resolve an IP address for a DNS name.
For example, if SERVER
is a-name-with-no-ip.example.com and you try to connect to it, it will throw a getaddrinfo failed
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.