python - 在 Python 中连接到本地服务器时遇到问题
问题描述
我目前正在关注如何使用 python 制作多人游戏的本教程:https ://www.youtube.com/watch?v=McoDjOCb2Zo
我目前正在尝试使用网络文件连接到服务器文件。运行服务器文件会打印出正确的信息,但是一旦我尝试使用网络文件连接到它,什么也没有发生。
这是我的服务器代码。当它运行时,它会打印出“等待连接,服务器已启动(我已经删除了我的 IP 地址,但我知道当我运行我的代码时我有正确的地址)
import socket
from _thread import *
server = "***.***.*.**"
port = 5555
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.bind((server, port))
except socket.error as e:
str(e)
s.listen(2)
print("Waitng for a connection, Server Started")
def threaded_client(conn):
conn.send(str.encode("Connected"))
reply = ""
while True:
try:
data = conn.recieve(2048)
reply = data.decode("utf-8")
if not data:
print("Disconnected")
break
else:
print("Received", reply)
print("Sending: ", reply)
conn.sendall(str.encode(reply))
except:
break
print("Lost Connection")
conn.close()
while True:
conn, addr = s.accept()
print("Conneced to: ", addr)
start_new_thread(threaded_client, (conn,))
这是我的网络的代码
import socket
class Network:
def __init__(self):
self.client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server = "***.***.*.**"
self.port = 5555
self.addr = (self.server, self.port)
self.id = self.connect()
print(self.id)
def connect(self):
try:
self.client.connect(self.addr)
return self.client.recv(2048).decode()
except:
pass
n = Network()
当我在初始化服务器后运行此代码时,它应该打印出“已连接”
解决方案
推荐阅读
- file - 是否可以将 VichUploader 集成到 CKEditor 中?
- angularjs - HyperLedger Composer:未处理的错误
- android - 使用什么库与 Android TV Remote 服务集成?
- cmake - CMake 3.11 链接 CUBLAS
- android - 在我的手机上运行应用程序时不显示垂直线视图
- c# - 用户输入确认
- php - 无法在 wordpress 主题上加载自定义 CSS
- scala - 如何找到 Spark RDD 的平均值?
- angular - 通过 highchart-angular 模板调用方法
- c++ - 从引用类型在 lambda 中捕获的值的类型,不使用广义捕获