# \[SOLVED\]Refuse the second client to connect the server

**URL:** <https://community.particle.io/t/solved-refuse-the-second-client-to-connect-the-server/21150>\
**Category:** Troubleshooting\
**Created:** [March 17, 2016, 8:46am UTC](https://community.particle.io/t/solved-refuse-the-second-client-to-connect-the-server/21150 "2016-03-17T08:46:16Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jackson\_lv](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/jackson_lv/32/8180_2.png) [@Jackson\_lv](https://community.particle.io/u/Jackson_lv)\
**Post date:** [March 17, 2016, 8:46am UTC](https://community.particle.io/t/solved-refuse-the-second-client-to-connect-the-server/21150/1 "2016-03-17T08:46:16Z")

</div>

I used a photon to test the socket function,when the first client connect to the server ,it can send(receive) the data to(from) the server.But after the second client connect to it,both clients could not send and get data.I want to know if there is some functions to stop another client to connect to the server after the first one is connected?

---

<div class="post-metadata">

**Author:** ![kennethlimcp](https://avatars.discourse-cdn.com/v4/letter/k/c0e974/32.png) [@kennethlimcp](https://community.particle.io/u/kennethlimcp)\
**Post date:** [March 17, 2016, 8:48am UTC](https://community.particle.io/t/solved-refuse-the-second-client-to-connect-the-server/21150/2 "2016-03-17T08:48:29Z")

</div>

You might be able to configure your server to only support 1 client?

---

<div class="post-metadata">

**Author:** ![Jackson\_lv](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/jackson_lv/32/8180_2.png) [@Jackson\_lv](https://community.particle.io/u/Jackson_lv)\
**Post date:** [March 17, 2016, 8:55am UTC](https://community.particle.io/t/solved-refuse-the-second-client-to-connect-the-server/21150/4 "2016-03-17T08:55:58Z")

</div>

I just don’t want another client to block me when I was connecting to the server.Are there any good advices?😄

---

<div class="post-metadata">

**Author:** ![kennethlimcp](https://avatars.discourse-cdn.com/v4/letter/k/c0e974/32.png) [@kennethlimcp](https://community.particle.io/u/kennethlimcp)\
**Post date:** [March 17, 2016, 8:55am UTC](https://community.particle.io/t/solved-refuse-the-second-client-to-connect-the-server/21150/5 "2016-03-17T08:55:59Z")

</div>

Your server will then have to handle the connections are serve them accordingly? I’m not an expert in server/client but that’s what i would do if i need to code one instead of having it not responding the server gets multiple connection 😄

---

<div class="post-metadata">

**Author:** ![rickkas7](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/rickkas7/32/8681_2.png) [@rickkas7](https://community.particle.io/u/rickkas7)\
**Post date:** [March 17, 2016, 9:16am UTC](https://community.particle.io/t/solved-refuse-the-second-client-to-connect-the-server/21150/6 "2016-03-17T09:16:06Z")

</div>

The TCPServer does support multiple client sockets, but not in the way it’s described in the docs. The server.write() method does not write to all connected clients, it actually writes to the last connected client.

What you need to do is:

In your loop, when you call server.available() you don’t store the TCPClient object in a global variable. You need to have some sort of array/list/whatever to hold the multiple clients you wish to support. If the client returned by server.available() then has client.connected() true, that means a new connection was established, and you should handle data on that connection.

In loop(), you will need to loop through all active connections, and do the reads and writes as you need to for your application. Make sure you use client.write() to write to that client, not server.write(), which doesn’t work right for multiple clients.

If client.connected() returns false on a previously active connection, the other side has disconnected and you should handle orderly shutdown of that connection and add it back into your free pool/mark as not active.

Also, you need to continue to call server.available() as above, even when you have connections, so new connections come in, unlike the example server code.

---

<div class="post-metadata">

**Author:** ![Jackson\_lv](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/jackson_lv/32/8180_2.png) [@Jackson\_lv](https://community.particle.io/u/Jackson_lv)\
**Post date:** [March 18, 2016, 2:44am UTC](https://community.particle.io/t/solved-refuse-the-second-client-to-connect-the-server/21150/7 "2016-03-18T02:44:01Z")

</div>

Hi @rickkas7:

It worked !! Thanks for your good suggestion !!

---

<div class="post-metadata">

**Author:** ![pamelafox](https://avatars.discourse-cdn.com/v4/letter/p/aeb1de/32.png) [@pamelafox](https://community.particle.io/u/pamelafox)\
**Post date:** [March 25, 2017, 3:50pm UTC](https://community.particle.io/t/solved-refuse-the-second-client-to-connect-the-server/21150/8 "2017-03-25T15:50:23Z")

</div>

> [@rickkas7](#):
>
> array/list/whatever

I'm implementing this solution right now, and I'm curious as to what data structure you ended up using. I haven't been in C/C++ land since college, so I'm not sure what the standard linked list data structure is for Photon/Arduino projects. Thanks for the help!

---

<div class="post-metadata">

**Author:** ![rickkas7](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/rickkas7/32/8681_2.png) [@rickkas7](https://community.particle.io/u/rickkas7)\
**Post date:** [March 25, 2017, 4:46pm UTC](https://community.particle.io/t/solved-refuse-the-second-client-to-connect-the-server/21150/9 "2017-03-25T16:46:30Z")

</div>

Here's an example of a TCP server that allows multiple connections. It's actually a tiny HTTP server, but it only serves a single file. The important part is that it shows how to handle multiple connections, which is kind of confusing.

> [@Active TCPServer / Client appears to crash Photon when WiFi disconnects](https://community.particle.io/t/active-tcpserver-client-appears-to-crash-photon-when-wifi-disconnects/27130/2):
>
> I’m not sure why your test is crashing, but here’s what I tried: The Photon code is a simple web server that returns an index.html file. The server side code makes a new HTTP request to the Photon every second. I would disconnect the Ethernet to the access point for about 5 seconds, long enough to stop incoming connections but not go to blinking cyan. Then wait for things to stabilize again, then disconnect for long enough to go to blinking cyan. I repeated this 5 times, which was about 330 con…

---

<div class="post-metadata">

**Author:** ![pamelafox](https://avatars.discourse-cdn.com/v4/letter/p/aeb1de/32.png) [@pamelafox](https://community.particle.io/u/pamelafox)\
**Post date:** [March 25, 2017, 5:53pm UTC](https://community.particle.io/t/solved-refuse-the-second-client-to-connect-the-server/21150/10 "2017-03-25T17:53:00Z")

</div>

Oh, fantastic! Thanks for that example, that’d work well for our use case as well.
