Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I want to connect multiple clients to one server, how do I do that? #20

Open
ducgiangx911 opened this issue Oct 1, 2023 · 2 comments
Open

Comments

@ducgiangx911
Copy link

I tried creating 2 clients to connect to the same server, but it only receives the first one, the second one is waiting for a connection.
image

@joe-at-startupmedia
Copy link

I am interested on how to do this as well. I have the same issue.

@joe-at-startupmedia
Copy link

joe-at-startupmedia commented May 8, 2024

The code has to be modified but this is how it works accepting multiple clients:

https://github.com/joe-at-startupmedia/golang-ipc/blob/91ac951aa624052216ded00999de5c3fffb19984/example/example.go

Notes:

  • due to how the net.Listener Accept function works, it's a blocking method that prevents new client connections until the previous one closes Client.Close(). As such, the Server can only handle one active client connection at a time.
  • in order to allow multiple Clients the Server has to manage multiple net.Listeners. I found the easiest way to do this is by managing multiple socket connections.
  • the socket connections are dynamically allocated per each new StartClient call. This way we aren't limited in how many Clients we can use or unnecessarily allocating unneeded resources.
  • in order to poll new Clients after call a server.Read call, a ServerManager must be used to loop through all of the latest client connections and poll the new Clients on the next server.Read iteration.
  • Because Read is blocking by default, I added a ReadTimed method that times out after the provided duration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants