2 way server <-> client communication

Hello.
So for my game I have a server thread (S) and packet sender thread (PS). First I was gonna ask about the problem I had with client not geting servers packets, but during writing I figured out what I missed and corrected it.
Now I ask for opinions, is this good way to program networking or there are better ways? Like most stuff beyond connecting and receiving data on one side, I did this myself couse there are no tutorials or books on how to do server - client thing right way (beyond trivial connecting to server, sending data and server responding to received data), at least I couldn’t find them. Tell me what do you think of this logic:

S waits for connection in non-blocking mode. Client connects and S accepts. On clients side channel is register for OP_READ, set to non-blocking mode and added to clients list after establishing the connection to server, and on server side all that is done when accepting connection. Both sides have now S that listens for that channel’s data, and a PS that sends data through the channel on the other side every X seconds. If error occurs or connection is closed then channel is removed from clients list and closed (thus deregistering OPs as I understood). If more clients connect to server they will be added to the clients list and PS on server side goes through the list, sending everybody data. So S and PS on both ends are the same except they have IF statements do process server’s or client’s packets differently.

That’s that.
Oh yeah I find the biggest problem is actually synchronizing the data so threads won’t access it at the same time… and I’ve got a lot of data shared between 3 threads (animation, S, PS). It’s really annoying and I was thinking of restructuring things so they share less data. For example, for client to send players stats and position, PS needs to access animation thread and take the data… same thing about processing received data where S needs to set animation’s variables and so on… don’t get me started how many data S i PS share… If there is a good book or tutorial who would describe the best logic for this I would be gratefull. Thanks to all for reading this huge post :wink:

If you are using non-blocking channels there’s no need to have a thread per connection to send packets - that was only needed for old IO when you had to block. It would be better to have one thread for the server which sends packets to all clients. For listening to connections you can do that in the same thread or have a separate one that blocks - I do the latter since then you can pause the game loop thread without stopping incoming connections - i don’t think that there’s any threading issues with doing this either (at least I haven’t had any).

you missunderstood, I said I have a server thread and packet sender thread. That’s 2 threads. Didn’t mentioned nothing about thread per connection.
About having server thread that listens and sends data, how do you do that? How would I implement sending every X ms? … for now I sleep in packet sender thread for X ms.