harveyslash
harveyslash

Reputation: 6012

Server/Client Connection (Python)

This is my code for server :

import socket

serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = "192.168.1.3"
port = 8000
print (host)
print (port)
serversocket.bind((host, port))

serversocket.listen(5)
print ('server started and listening')
while 1:
    (clientsocket, address) = serversocket.accept()
    print ("connection found!")
    data = clientsocket.recv(1024).decode()
    print (data)
    clientsocket.send("data is sent".encode())

and for Client :

import socket

s = socket.socket()
host ="59.93.199.XXX"
port =8000
s.connect((host,port))
s.send('randomData'.encode())
data = ''
data = s.recv(1024).decode()
print (data)
s.close

I want this to work across two computers across the internet. when I put host="192.168.1.3" in client, it works fine, but that's only for computers connected to the same network. The thing is, that while server is running, and i check for open port online,the server shows 'connection found', but it does not connect to server via client. what am i doing wrong ? PORT 8000 is open.

192.168.1.3 is my computer's IP and 192.168.1.1 is the gateway ERROR THAT I GET WHEN I RUN CLIENT :

Traceback (most recent call last): File "C:\Users\Harsh's\Desktop\aaa.py", line 6, in s.connect((host,port)) ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it

Upvotes: 0

Views: 560

Answers (1)

Hyperboreus
Hyperboreus

Reputation: 32429

across two computers across the internet

In this case, your server either needs a public, static address, or you have to configure your router to route incoming connections for port 8000 to be redirected (translated) to your server (NATting, PATting, etc).

Whose address is "59.93.199.XXX"? Your router's? Then there you need to do the translating/redirecting/chaining.

After some tests, the problem showed to be:

s = socket.socket()

instead of

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

Upvotes: 1

Related Questions