Jump to content

Please i really need help with tcp server

pwarrow88

Hi i need a little help writing a python program for a tcp server that allowed multiple client to connect and if one client sends a message to the server it will route that message to all connected clients also i can't figure out if there is a way to open lots of ports so that i wont be stuck only using one port if it possible can we open all ports i do not know programing so i have been at this issue for like weeks now Thank you anyone in advance that can help me you all are my only last hope at getting this project done.

Link to comment
Share on other sites

Link to post
Share on other sites

20 minutes ago, pwarrow88 said:

if there is a way to open lots of ports so that i wont be stuck only using one port

Why would you want to do that?

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to comment
Share on other sites

Link to post
Share on other sites

24 minutes ago, pwarrow88 said:

if there is a way to open lots of ports so that i wont be stuck only using one port if it possible

NOO Don't due that, that will make your internet more open to hackers or ddos attack.

Link to comment
Share on other sites

Link to post
Share on other sites

The answer you seek is called NAT.

 

26 minutes ago, pwarrow88 said:

...if it possible can we open all ports...

1) Not possible, some ports are hard reserved for vital services.

2) Even if you could you wouldn't want to. This is like smashing in your front door with a sledgehammer because you left your key at a mates house who only live 10 minutes down the road.

Main Rig:-

Ryzen 7 3800X | Asus ROG Strix X570-F Gaming | 16GB Team Group Dark Pro 3600Mhz | Corsair MP600 1TB PCIe Gen 4 | Sapphire 5700 XT Pulse | Corsair H115i Platinum | WD Black 1TB | WD Green 4TB | EVGA SuperNOVA G3 650W | Asus TUF GT501 | Samsung C27HG70 1440p 144hz HDR FreeSync 2 | Ubuntu 20.04.2 LTS |

 

Server:-

Intel NUC running Server 2019 + Synology DSM218+ with 2 x 4TB Toshiba NAS Ready HDDs (RAID0)

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Master Disaster said:

The answer you seek is called NAT.

I don't think OP is talking about port-forwarding here. The way I read it, OP wants their app, for some unfathomable reason, to listen on all the available ports on their system, which is both stupid and bad design. OP didn't say anything about opening ports on their router.

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, WereCatf said:

I don't think OP is talking about port-forwarding here. The way I read it, OP wants their app, for some unfathomable reason, to listen on all the available ports on their system, which is both stupid and bad design. OP didn't say anything about opening ports on their router.

That's actually worse than what I thought :D

 

At that point your encroaching on rootkit/malware routines.

Main Rig:-

Ryzen 7 3800X | Asus ROG Strix X570-F Gaming | 16GB Team Group Dark Pro 3600Mhz | Corsair MP600 1TB PCIe Gen 4 | Sapphire 5700 XT Pulse | Corsair H115i Platinum | WD Black 1TB | WD Green 4TB | EVGA SuperNOVA G3 650W | Asus TUF GT501 | Samsung C27HG70 1440p 144hz HDR FreeSync 2 | Ubuntu 20.04.2 LTS |

 

Server:-

Intel NUC running Server 2019 + Synology DSM218+ with 2 x 4TB Toshiba NAS Ready HDDs (RAID0)

Link to comment
Share on other sites

Link to post
Share on other sites

33 minutes ago, pwarrow88 said:

Hi i need a little help writing a python program for a tcp server that allowed multiple client to connect and if one client sends a message to the server it will route that message to all connected clients also i can't figure out if there is a way to open lots of ports so that i wont be stuck only using one port if it possible can we open all ports i do not know programing so i have been at this issue for like weeks now Thank you anyone in advance that can help me you all are my only last hope at getting this project done.

The typical way of handling multiple clients is:

  1. The service is listening on a specific port for connections
  2. Client connects to that port
  3. The service asks the OS for a new, random port it can use and starts to listen on that port
  4. The service replies to the client from that port and from there on out, the client and the service communicate over that port
  5. If the client disconnects, the service closes that port

Repeat steps 2-5 for every client that connects.

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to comment
Share on other sites

Link to post
Share on other sites

First you need to research about sockets. 

 

Once you learn a bit about it you'll have more insight into what you really want to do. 

Link to comment
Share on other sites

Link to post
Share on other sites

5 hours ago, WereCatf said:

The typical way of handling multiple clients is:

  1. The service is listening on a specific port for connections
  2. Client connects to that port
  3. The service asks the OS for a new, random port it can use and starts to listen on that port
  4. The service replies to the client from that port and from there on out, the client and the service communicate over that port
  5. If the client disconnects, the service closes that port

Repeat steps 2-5 for every client that connects.

Communication also typically happens on a new thread, to be able to accept additional incoming connections while communicating with client #1 over that random port. At least if you want to be able to communicate with multiple clients concurrently.

 

After accepting a client connection, you spawn a new thread which then communicates with that client, while the main thread goes back to listening for new incoming connections. Not sure I found a good example, but here you go:

https://www.geeksforgeeks.org/socket-programming-multi-threading-python/

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

14 minutes ago, Eigenvektor said:

Communication also typically happens on a new thread, to be able to accept additional incoming connections while communicating with client #1 over that random port. At least if you want to be able to communicate with multiple clients concurrently.

 

After accepting a client connection, you spawn a new thread which then communicates with that client, while the main thread goes back to listening for new incoming connections. Not sure I found a good example, but here you go:

https://www.geeksforgeeks.org/socket-programming-multi-threading-python/

Depends how you do it. 

 

I typically run 2 threads. 

1 is basically a nothing thread/text input thread for the server so the program is never "not responding"

 

Then I have another thread that handles the network traffic and iterates through all the connections to see what is being said to the server. 

 

I never really thought to do it with a thread for each client. That's the beauty of programming though. Many ways to do the same task. 

Link to comment
Share on other sites

Link to post
Share on other sites

9 minutes ago, fpo said:

I never really thought to do it with a thread for each client. That's the beauty of programming though. Many ways to do the same task. 

Sure, but depending on the number of concurrent clients and how long a connection can stay open you could run into timeouts if your server doesn't return to a listening state quickly enough. For example if I did this in Java:

ServerSocket serverSocket = new ServerSocket(portNumber);

while (serverIsRunning) {
    Socket clientSocket = serverSocket.accept();

    ... a download that takes 20 minutes
}

The server isn't going to return to the "accept" call until 20 minutes later meaning all other clients trying to connect to that port are going to run into TCP timeouts. Obviously if communication typically completes in (milli)seconds then this is not an issue.

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, Eigenvektor said:

Sure, but depending on the number of concurrent clients and how long a connection can stay open you could run into timeouts if your server doesn't return to a listening state quickly enough. For example if I did this in Java:


ServerSocket serverSocket = new ServerSocket(portNumber);

while (serverIsRunning) {
    Socket clientSocket = serverSocket.accept();

    ... a download that takes 20 minutes
}

The server isn't going to return to the "accept" call until 20 minutes later meaning all other clients trying to connect to that port are going to run into TCP timeouts. Obviously if communication typically completes in (milli)seconds then this is not an issue.

Ahh yeah. I haven't done that yet. 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×