0

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

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

7 Comments

it IS directed . www.yourgetsignal.com shows its open. i have dyanamic ip, but without resetting, why doesn't it work ?
In this case, bind your server to its public address, not to its local. Or bind it to all available interfaces.
i am a noob, please explain more elaborately if i make host =<myIP> in server, it shows errors. i think the server is okay, coz it shows connected messages when i check for open port. i think the problem is with the client side
Try using host = '' (all interfaces) in your server script.
I am not much into Windows, but 10061 sounds like your router is translating the port, but on the destination machine no matching service is running.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.