Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (535)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  2 way server <-> client communication  (Read 1829 times)
0 Members and 1 Guest are viewing this topic.
Offline Kova

Senior Member





« Posted 2006-04-12 02:26:57 »

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
Offline CommanderKeith
« Reply #1 - Posted 2006-04-12 05:57:05 »

Hello.
So for my game I have a server thread (S) and packet sender thread (PS).

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).

Offline Kova

Senior Member





« Reply #2 - Posted 2006-04-12 10:44:02 »

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.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Riven (6 views)
2014-07-29 18:09:19

Riven (4 views)
2014-07-29 18:08:52

Dwinin (7 views)
2014-07-29 10:59:34

E.R. Fleming (23 views)
2014-07-29 03:07:13

E.R. Fleming (9 views)
2014-07-29 03:06:25

pw (39 views)
2014-07-24 01:59:36

Riven (39 views)
2014-07-23 21:16:32

Riven (26 views)
2014-07-23 21:07:15

Riven (28 views)
2014-07-23 20:56:16

ctomni231 (59 views)
2014-07-18 06:55:21
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!