Python Socket connection over 2 different networks

787 views Asked by At

I know there are tons of questions like this but none of them helped me. I am trying to connect computer and vm that are on 2 different networks.

Server:

import socket

HOST = '0.0.0.0'  
PORT = 1080  

server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

server_socket.bind((HOST, PORT))

server_socket.listen(5)  
print("\n Listening on port " +str(PORT)+ ", waiting for client to connect...")


client_socket, (client_ip, client_port) = server_socket.accept()
print("[+] Client " +client_ip+ " connected!\n")


while True:
    try:
        command = raw_input(client_ip+ ">")
        if(len(command.split()) != 0):
            client_socket.send(command)
        else:
        continue
    except Exception,e:
        print("[-]Something went wrong")
                continue

    if(command == "stop"):
        break

    data = client_socket.recv(1024)
    print(data + "\n")

client_socket.close()

As you see,i opened a server that listens on port 1080 and accepts all connections.I turned off the windows firewall and the SPI Firewall. I also forwarded the port 1080( nmap scan finds it and says its open).

Client:

import socket
import platform
HOST = "output of whatismyipaddress.com when run on server computer" 
PORT = 1080 # port(same as on server.py)

connection_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
connection_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
connection_socket.connect((HOST, PORT))
print("\n[*] Connected to " +HOST+ " on port " +str(PORT)+ ".\n")

while True:

    command = connection_socket.recv(1024)
    split_command = command.split()
    print("Received command : " +command)

    if (command == "stop"):
        break
    elif (command == "info"):
        connection_socket.send(("Info: " + str(tuple(platform.uname()))))
connection_socket.close()

So client is sending data to server and server is sending commands to client.

I assume client doesn't need to have any port forwarded.

The client(vm) was connected to Android HotSpot. The server was connected to router that had port forwarded. I've choose bridged connection for vm and connected my external wifi card for it.

Server started listening and client started. I get error that the server did not properly respond after period of time when trying to connect. I also tried connecting using default gateway but no success.

I am a little confused about it since Wikipedia says:

A default gateway in computer networking is the node that is assumed to know how to forward packets on to other networks. Typically, in a TCP/IP network, nodes such as servers, workstations and network devices each have a defined default route setting, (pointing to the default gateway), defining where to send packets for IP addresses for which they can determine no specific route.

Which I understand as it is used to communicate over 2 different networks? Anyways, does anyone know how can I fix this issue and what am I doing wrong?

0

There are 0 answers