Python 套接字未连接

Python sockets are not connecting

我正在构建一个消息传递应用程序,我可以让两个脚本在同一台机器上连接和通信,但是在同一局域网上的两台不同的机器上,它们根本不连接。我已经为该程序在两台计算机上设置了防火墙例外,但它没有帮助。

主机只是在侦听来自客户端的连接时挂起,而客户端在等待一分钟后最终超时,说没有响应或响应不正确。

import socket
from threading import Thread
import tkinter as tk

default_port = 43777
self_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

def client_link():
    self_socket.connect((join_ip_input.get("1.0", "end-1c"), default_port))
    create_messenging_screen()
    self_socket.sendall(("hi to you too").encode("utf-8"))
    while True:
        try:
            print(self_socket.recv(1024))
        except:pass

def host_link():
    self_socket.bind((socket.gethostbyname(socket.gethostname()), default_port))

    self_socket.listen(1)

    global client_conn
    client_conn, client_addr = self_socket.accept()
    create_messenging_screen()

    client_conn.sendall(("hi").encode("utf-8"))
    print(client_addr)

    while True:
        try:
            print(client_conn.recv(1024))
        except:pass

def continue_setup(route):
    if route == None:
        Thread(target=host_link).start()
    else:

        Thread(target=client_link).start()


def create_messenging_screen():
   #clear the window and create the messaging GUI

window = tk.Tk()
#IM app connection screen

#host a chatroom
button(window, command=lambda: continue_setup(None))
#join a chatroom
join_ip_input = tk.Text(window)
button(window, command=lambda: continue_setup(1))

window.mainloop()

原来我的电脑有两个网络适配器,套接字模块使用的默认一个不工作,所以我指定另一个使用这个:socket.gethostbyname_ex(socket.gethostname())[2][1]