简体   繁体   English

通过局域网中的Python套接字在两台计算机之间进行通信失败

[英]communication between two computers via Python socket in a LAN failed

Two computers in a LAN connecting to a wireless router, one IP address is 192.168.1.106 (server), the other one is 192.168.1.107 (client), the gateway on both computer is 192.168.1.1 (the router itself). 局域网中的两台计算机连接到无线路由器,一个IP地址是192.168.1.106(服务器),另一台IP地址是192.168.1.107(客户端),两台计算机上的网关是192.168.1.1(路由器本身)。

The two computer can ping each in two directions which means there should be no problem with routing and the router itself. 两台计算机可以在两个方向上ping通,这意味着路由和路由器本身应该没有问题。 But I failed when I tried to use Python UDP socket, the server cannot get any information from the client, and same happened when I change the ip address. 但是,当我尝试使用Python UDP套接字时,我失败了,服务器无法从客户端获取任何信息,并且当我更改IP地址时也发生了同样的情况。 (But it works fine when server and client are on a same computer using local ip address, so the code is should be ok) (但是当服务器和客户端使用本地IP地址在同一台计算机上时,它可以正常工作,因此该代码应该可以)

I am using the following code: 我正在使用以下代码:

server: 服务器:

import socket

address = ('192.168.1.106', 5678) # the server listening on address 192.168.1.106
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(address)

while True:
    data, addr = s.recvfrom(2048)
    if data == "empty":
        print "no data from client"
    else:
        print "received:", data, "from", addr

s.close()

client: 客户:

import socket

address = ('192.168.1.106', 5678)  # the client send to address 192.168.1.106
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

while True:
    msg = raw_input()
    if not msg:
        msg = "empty"
    s.sendto(msg, address)

s.close()

您是否在两个comoutera的防火墙上都打开了UDP端口?

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

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