#๐Ÿ”’ How to make a simple connection between a host and client on the same internet provider ?

40 messages ยท Page 1 of 1 (latest)

rare cloud
#
import socket as S

HOST = "0.0.0.0"
PORT = 10

def server():
    server = S.socket(S.AF_INET, S.SOCK_STREAM)
    server.bind((HOST, PORT))
    server.listen()
    running = True
    while running:
        client, adress = server.accept()
        print("connecte avec", str(adress))
        client.send("hello".encode("utf-8"))
        
def client():
    cli = S.socket(S.AF_INET, S.SOCK_STREAM)
    while True:
        print("connect")
        cli.connect((HOST, PORT))
        cli.send("yo je suis client".encode("utf-8"))
        sleep(1)
        
        
if __name__ == "__main__":
    server()
    # client(), for client

Hi, using Socket, i want to make a connection between two machine by the simlpest way possible. I made the python script above, my problem is that nothing happend. Is it a python issue ? machine issue ? how should i make this ?
Thanks.

fast driftBOT
#

@rare cloud

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

rare cloud
#

when running the client script, i input my public IPv4 adress as HOST

hybrid swift
rare cloud
#

for now, i'm running two scripts on the same machine

lucid steppe
#

How do you know nothing happens? Both sides send, but neither receives.

hybrid swift
lucid steppe
#

0.0.0.0 is ok for the server to listen on everything. But you need a specific IP address for the client to use. Though you said you were doing that.

rare cloud
lucid steppe
#

If you're using the same machine you could use 127.0.0.1 (localhost). Get things working. Then use public addresses.

hybrid swift
#

you probably want to start with 127.0.0.1 which is local host if you are running both functions on the same machine to start with

lucid steppe
#

When you say public, do you mean the LAN address (oten 192.168.* or 172.16.*) or a genuine public address routable from the public internet?

hybrid swift
#

i'm guessing your computer doesn't have your public ip (out on the internet) directly attached, right?

lucid steppe
#

Hey, is this on a UNIX machine? Or Linux? You won't be allowed to use port 10. You will need a number over 1024. Probably well over 1024, since many low ports are in use.

#

Like 9999.

rare cloud
#

i'm on windows

lucid steppe
#

You may be fine with 10 then. Unsure.

#

Provided it's not already in use.

hybrid swift
#

yeah, port 10 can be an issue

#

unless running the script with administrator privileges on the system (which you shouldn't unless absolutely necessary)

lucid steppe
#

I assume your script is silent? Not raising an exception.

rare cloud
#

cli.connect(("127.0.0.1", PORT))
ConnectionRefusedError: [WinError 10061] No connection could be established because the target computer specifically refused it

what does this error mean ? is this a windows issue ?

#

hmm my bad, the server was'nt running

hybrid swift
#

most systems will not let you bind the server process to a port under 1024 without administrator privileges

#

so pick a port over that if you can

rare cloud
#

ok it works using the same machine

hybrid swift
#

and then it's the issue if the server isn't running or hasn't been able to bind to the port that you ask it to bind to (either because of missing privileges or that something else is already using that port, as each port can only be used by one process at a time)

#

great!

#

just know that you can also have issues with the windows firewall stopping connections

#

or if you have any other security software that runs its own firewall that can also become a problem

rare cloud
#

how do i make it to work on different machine using diferent internet providers ?

hybrid swift
#

first if you are on the same local network you would use your LAN (Local Area Network) internal ip address to reach the server machine instead of 127.0.0.1

#

then if we are talking about connectivity over the internet, then you need to either have the server directly on the public ip address or forward a port from a firewall/router to the machine on the internal network running the server software

#

or you will need to use a service such as ngrok or similar to expose the service to the internet through this service

#

if the server should not be exposed to the whole internet you can setup a priivate proxy or network service such as tailscale or twingate or similar

rare cloud
#

ok, i will try this later (i'm in CS course), thanks !!!

hybrid swift
#

good luck with your little project

fast driftBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.