Skip to content
← Back to Community
Socket not connecting
Profile icon
AtharvGoel

I'm trying to get two repls to connect using socket. I created some code using a tutorial, but for some reason, I can't get it to work. Here are the repls:
https://repl.it/@AtharvGoel/ServerTest
https://repl.it/@AtharvGoel/ClientTest

Answered by abc3354 [earned 5 cycles]
View Answer
Voters
Profile icon
AtharvGoel
Comments
hotnewtop
Profile icon
abc3354

I'm sorrry but I don't think replit supports tcp servers
You can try to ask for the feature at repl.it/feedback

There is a way to do it using ngrok, I can explain it quickly if you are interested

Profile icon
AtharvGoel

@abc3354 If you can explain it to me or refer me to something, that would be great.

Profile icon
AtharvGoel

@abc3354 I tried using pip install ngrok but got an error

Profile icon
abc3354

@AtharvGoel I created something to help you
It is based on https://dashboard.ngrok.com/get-started/setup

  • create a file called install-ngrok.sh and paste that
rm -f ngrok-stable-linux-amd64.zip rm -f ngrok wget https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip unzip ngrok-stable-linux-amd64.zip rm ngrok-stable-linux-amd64.zip
  • open shell (ctrl - shift - s) and type
chmod u+x install-ngrok.sh ./install-ngrok.sh
  • you need to create a ngrok account at ngrok.com
    copy the command with your authtoken
    image
    paste it on the shell (shift - insert to paste)

  • type ./ngrok tcp 5000 (5000 is the port of your server)
    image
    The intersting thing is the forwarding part (2.tcp.ngrok.io:15792 for me)

  • you can use this to connect to the server
    For example in the client with my address it will be

TCP_IP = '2.tcp.ngrok.io' TCP_PORT = 15792

You can see a working repl here
https://repl.it/@abc3354/ngrok
https://repl.it/@abc3354/ngrok-client

Hope this will help :)

PS: if the repl is public, your authtoken could be exposed (I'm not sure) , go on this page to reset it

Profile icon
AtharvGoel

@abc3354 I ran your example but I got an error on ngrok-client
Traceback (most recent call last):
File "main.py", line 10, in
s.connect((TCP_IP, TCP_PORT))
ConnectionRefusedError: [Errno 111] Connection refused

Profile icon
AtharvGoel

@abc3354 I GOT IT TO WORK!!! It didn't save the stuff in the shell which was the problem. Is it possible to get this to work over different devices?

Profile icon
abc3354

@AtharvGoel did you run the server first ?
I think you will have to fork it and use your own ngrok authtoken to make it work

Profile icon
AtharvGoel

@abc3354 Yeah, I got my own to work using your help, do you know if this works over different devices.

Profile icon
abc3354

@AtharvGoel congrats ! :D
It should actually work over different devices :)

Profile icon
AtharvGoel

@abc3354 Ok thanks

Profile icon
AtharvGoel

@abc3354 I'm having some problems. When I try to decode the message on the client side, nothing shows up. On the server side, the message won't show up at all.

Profile icon
abc3354

@AtharvGoel I have the solution !

On the client I replaced s.send(message) by s.send(message.encode('utf-8')), to have a byte array

And in the server I added a print (to actually show the message received)
image

Profile icon
Nefomemes

@abc3354 You can use the .env file.

Profile icon
AnonymOus148

@AtharvGoel How did u get it to save ?

Profile icon
WassimTahraoui

@abc3354 is it possible to use ngrok with java? or is it python-specific?

Profile icon
abc3354

@WassimTahraoui ngrok should work with any language ! Note that if the repl is public, someone could steal the auth token replace your tcp server with an evil one !

Profile icon
abc3354

@WassimTahraoui
I just tried my repls again and it seems that you cannot connect to ngrok with tcp anymore
That means that the server repl stills work but the client repl does not

Profile icon
Froggo-School

don't know what language it was written in, but for future reference, an example using python's socket library:

import socket tcp_connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM) udp_connection = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # otherwise, functions are the same.
Profile icon
Coder100

Your server needs an HTTP socket server.

Profile icon
AtharvGoel

@Coder100 How do I do that?

Profile icon
abc3354

@AtharvGoel @Coder100 This is not websocket, you don't need an HTTP server