0

我正在构建一个使用套接字在服务器之间发送和请求数据的应用程序,现在我正在尝试发送时间。我在 Virtualbox 上的 Ubuntu VM 中有一个服务器套接字:

#ip = '0.0.0.0'
#ip = '127.0.0.1'
ip = '127.0.1.1'
#ip = '10.0.2.15'
port = 8080
data_connection = (ip, port)
max_connections = 3

time_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
time_server_socket.bind(data_connection)
time_server_socket.listen(max_connections)
print("Waiting on %s:%s" %(ip, port))
print(socket.gethostname())
print(socket.gethostbyname(socket.gethostname()))
while True:
    time_st = time.gmtime(time.time())
    client, address = time_server_socket.accept()
    print("Connection %s:%s" %(address[0], address[1]))
    data = client.recv(4096)
    print("Direccion: " + data)
    current_hour = hour_format(time_st);
    client.send(current_hour)
    client.close()

当我运行服务器

客户端位于尝试连接到 Ubuntu VM 但没有运气的 Ubuntu Azure VM 上:

def get_time():
ip_time_server = '200.68.142.31'
port_time_server = 8080 
time_server = (ip_time_server, port_time_server)
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
    client.connect(time_server)
    string_time = "GetTime"
    client.sendto(socket.gethostbyname(socket.gethostname()).encode(), time_server)
    time_response = client.recv(4096)
except socket.error:
    client.close()
    return "error"
return time_response

这只是一直等到超时 我尝试使用端口转发但似乎没有做任何事情。我禁用了主机 Windows 机器和来宾 Ubuntu Virtualbox 上的防火墙,但没有。我究竟做错了什么?

4

0 回答 0