简体   繁体   English

Python Socket连接同一网络上的两个设备

[英]Python Socket connect two devices on same network

I am attempting to connect a simple server and client from two computers on the same network. 我试图从同一网络上的两台计算机连接一个简单的服务器和客户端。 Both the client and server cannot 'find' each other, as they do not move past .connect() and .accept() respectively. 客户端和服务器不能“找到”对方,因为他们不搬过去.connect().accept()分别。 What am I doing wrong? 我究竟做错了什么?

(Windows 10) (Windows 10)

Server: 服务器:

import socket

HOST = socket.gethostname()    #Returns: "WASS104983"
#I have also tried socket.gethostbyname(socket.gethostname)), returning: "25.38.252.147"
PORT = 50007

sock = socket.socket()
sock.bind((HOST, PORT))
sock.listen(5)

print("Awaiting connection... ")

(clnt, addr) = sock.accept()

print("Client connected")
…

and Client: 和客户:

import socket

HOST = "WASS104983"    #Or "25.38.252.147", depending on the servers setup
PORT = 50007

sock = socket.socket()

print("Attempting connection... ")

sock.connect((HOST, PORT))

print("Connected")
…

I have gotten this to work before so I am not sure why it's not now. 我之前已经开始工作,所以我不确定为什么现在不行。

I know there are a few questions of this calibre, but none seem to cover my problem. 我知道这个问题有几个问题,但似乎都没有解决我的问题。

Also, a wifi extender should not interfere with local transmissions should it? 另外,wifi扩展器不应该干扰本地传输吗?

I have always seen servers setup as such: 我一直看到服务器设置如下:

import socket
import threading

bind_ip = '0.0.0.0'
bind_port = 9999

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((bind_ip, bind_port))
server.listen(5)

print("[*] Listening on {}:{}".format(bind_ip, bind_port))


def handle_client(client_socket):
    request = client_socket.recv(1024)
    print('received: {}'.format(request))
    client_socket.send(b'ACK!')
    client_socket.close()


while True:
    client, addr = server.accept()
    print("[*] Accepted connection from: {}:{}".format(addr[0], addr[1]))
    client_handler = threading.Thread(target=handle_client, args=(client,))
    client_handler.start()*

Where I think an important distinction from your post may be that the server accepting connections is within an infinite loop. 我认为与您的帖子的重要区别可能是接受连接的服务器在无限循环内。 Have you tried this? 你试过这个吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM