0

I´m building an app that uses sockets to send and request data between servers, right now I´m trying to send the time. I have a server socket in an Ubuntu VM on Virtualbox:

#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()

When I run the server

The client side is on an Ubuntu Azure VM that tries to connect to the Ubuntu VM but no luck:

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

This just stays waiting until it times out I tried using port forwarding but doesn´t seem to do anything. I disabled the firewall on both host Windows machine and the guest Ubuntu Virtualbox but nothing. What am I doing wrong?

  • Your server is listening on 127.0.0.1, which is the loopback interface address, but your client is trying to connect to 200.68.142.31. Instead, have your server listen on an interface that your client can connect to. – President James K. Polk Jun 08 '20 at 13:45
  • I tried finding an address that the client could connect to, but I couldn't really find a working one. I tried getting the interfaces but it only showed me 127.0.0.1 and another one that didn't work. I did a bit more research and noticed that I had to move things around regarding ISP as pointed in an answer here https://stackoverflow.com/questions/31785902/can-sockets-be-used-to-connect-multiple-computers-on-different-networks-in-pytho In the end I created another virtual machine. – A. Mendoza Jun 09 '20 at 06:45

0 Answers0