0

I am currently trying to learn how servers and clients work by making a trojan using python and sockets import, my client and server work perfectly on my computer but the moment I send the client to my other laptop the server does not connect. This happens even when i am on the same wifi network.

Server:

import socket

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = ''
port = 1234

server.bind((host, port))
server.listen(5)

run = True
client, addr = server.accept()
print('Got connection from',addr)

while run:
    try:
        data = input('>>>')

        client.send(data.encode('UTF-8'))

        msg = client.recv(1024)
        print(msg.decode('UTF-8'))
    except ConnectionResetError:
        print('Client lost server connection')
        print('Trying to connect . . .')
        client, addr = server.accept()
        print('Got connection from',addr)


Client:

import socket
import os
server = socket.socket()
host = '127.0.0.1'
port = 1234

run = True
server.connect((host,port))
while run:


    msg = server.recv(1024)
    os.popen(msg.decode('UTF-8'))

    server.send('Client online . . .'.encode('UTF-8'))
Jigsaw
  • 294
  • 1
  • 3
  • 14

1 Answers1

2

Your client is connecting to IP 127.0.0.1 (the IPv4 loopback address), which will work only when the server is on the same machine as the client.

When the client and server are on different machines, but still on the same LAN network, the client needs to connect to the server's LAN IP instead. Use netstat or similar tool on the server machine to find its LAN IP. Or, simply have your server code print out its local IPs.

When the client is on another network, it needs to connect to the public WAN IP of the server's LAN router, and that router needs to have port forwarding configured on it to route incoming connections from its WAN IP/Port to the server's LAN IP/port. To get the WAN IP, you will have to look at your router's config, or simply query an external site, like https://api.ipify.org, https://api.my-ip.io/ip, etc from a machine on the LAN, like your server.

Update your client to take in the target host/IP from user input, then it will be able to handle all of these scenarios without having to use different code each time.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770