如何在同一网络上的两台计算机之间进行通信 [英] How do I communicate between two computers on the same network

查看:304
本文介绍了如何在同一网络上的两台计算机之间进行通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我是网络编程的新手,我正在尝试使用Python中的套接字在同一网络上的两台计算机之间进行通信。

我已经了解了套接字以及如何使用他们但由于某种原因,我无法使这件事发挥作用。

我尝试在同一台计算机上使用两个不同的Python IDE进行此操作并且它有效,但当我尝试在两个不同的地方执行此操作时计算机它没有。



我尝试过:



服务器端

Hello, I'm pretty new to network programming and I'm trying to communicate between two computers on the same network using sockets in Python.
I've learned about sockets and how to use them but for some reason, I couldn't make this thing work.
I tried doing this on the same computer with two different Python IDEs and it worked, but when I tried to do this on two different computers it didn't.

What I have tried:

Server side

import socket

s = socket.socket()
host, port = socket.gethostname(), 12345
s.bind((host, port))
s.listen(5)
c, addr = s.accept()

c.send("Thank you for connecting to my server")
c.close()





客户端





Client side

import socket

s = socket.socket()
host, port = socket.gethostname(), 12345
s.connect((host, port))

print(s.recv(1024)





请事先帮助我解决这个问题。



Please help me to solve this, thanks in advance.

推荐答案

不确定我是否正确理解了这个问题,但是看起来你正在使用客户端和服务器端的当前python主机名。因此,您似乎总是尝试连接到运行代码的同一台计算机。相反,您应该使用您尝试连接的主机的名称,并使用例如 gethostbyname 来解析地址。



另一件事是您使用的端口应该是开放的,以确保防火墙不会阻止通信。
Not sure if I understand the question correctly, but it looks like you're using the current python host name for both client and server side. So it seems that you always try to connect to the same computer where the code is running. Instead, you should probably use the name of the host you're trying to connect to and use for example gethostbyname to resolve the address.

Another thing is that the port you use should be open so ensure that a firewall isn't blocking the communication.


这篇关于如何在同一网络上的两台计算机之间进行通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆