TCP connection refused using Ngrok in python

  Kiến thức lập trình

I am new to the ngrok and I was trying to create a socket server on python then use ngrok to expose the server over the internet. So far, when I use the url generate from ngrok on my own computer, the connection is successful and I can interact with the server. But when I use computers from other networks, the connection failed. I tried changing the DNS to 8.8.8.8, double checked if the firewall is deactivated etc, but nothing seems to wrong. I sincerely ask anyone who have dealt with similar problems to share their wisdom please. Here is my code:

import threading
import socket
from pyngrok import ngrok
import os

host = "localhost"
port = 3000

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

server_address = ("", port)
server.bind(server_address)
server.listen() # start listening mode

clients = []

# broadcast method
def broadcast(message):
    for client in clients:
        client.send(message)

# Receive messages from client then broadcast
def handle(client, nickname):
    while True:
        try:
            msg = message = client.recv(1024)
            decoded_msg = msg.decode('ascii')
            if msg != '':
                print("{nickname}: {msg}".format(nickname = nickname, msg = decoded_msg))
        except socket.error:
            print("error")

def receive():
    global clients
    while True:
        client, address = server.accept()
        print("Connected by", str(address))

        # ask for nickname
        client.send('Nickname'.encode("ascii"))
        nickname = client.recv(1024).decode('ascii')
        clients.append(client)
        broadcast(f'{nickname} ({address}) joined the Chatn'.encode('ascii'))
        print(f'{nickname} ({address}) joined the Chatn')
        client.send('Connected to the Server!n'.encode('ascii'))

        # Handling Multiple Clients Simultaneously
        thread = threading.Thread(target=handle, args=(client,nickname))
        thread.start()

receive()

and the client code

import socket
import threading
import json
import os

def enter_server():
    os.system("cls||clear") # clear interpreter output
    global nickname
    nickname = input("Choose Your Nickname:")

    host = os.environ.get("HOST")
    port = int(os.environ.get("PORT"))
    global client
    client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    # Connect to a host
    client.connect((host, port))
    nickname_query = client.recv(1024).decode('ascii')
    print(nickname_query)
    if nickname_query == 'Nickname':
        client.send(nickname.encode('ascii'))
    msg = client.recv(1024).decode('ascii')
    print(msg)

def receive():
    while True:
        try:
            message = client.recv(1024).decode('ascii')
        except socket.error:
            print('Error Occured while Connecting')
            client.close()
            break

def write():
    while True:
        message = input("")
        client.send(message.encode('ascii'))

enter_server()
receive_thread = threading.Thread(target=receive)
receive_thread.start()
write_thread = threading.Thread(target=write)
write_thread.start()

I changed the DNS and took down the firewall, rewrote the code a few times.

New contributor

monte-carlo-pricer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT