Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (475)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (529)
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  
  [SOLVED] Best Practise, two way initiated socket communication  (Read 3927 times)
0 Members and 1 Guest are viewing this topic.
Offline Mike

JGO Ninja


Medals: 71
Projects: 1
Exp: 5 years


Java guru wanabee


« Posted 2010-09-06 00:22:02 »

Hi there fellow Javaers,

Background story, skip if you want:
After migrating most code from using php as a "server" I moved over to servlets to reuse some checks I already have in java instead of rewriting them in php. Now I took another step and moved over to sockets (mostly so that the server can initiate communication with the clients instead of the clients needing to poll constantly).

As I like to write everything from scratch (I do this mostly to challenge myself anyway and I want to be in control of my code) I already spent something like 15 hours to get sockets up and running to a point where I can send either clear or compressed code to the clients, I have some error handling for socket interruptions and no code is blocking the CPU. My initial tests are extremely satisfying and the performance of the server is great, even with several connected clients.


Sometimes I want the client to send information to the server and await a response and sometimes I want the server to send information to the applet and await a reply.

What is the best way around this? What if the client sends something to the server and goes into waiting mode, but before the server recieved the request the server initiates a send itself, therefore making the client think that what the client recieves from the server is the response to to the client's request, while it actually is the request from the server?

I guess I could create two socket connections with the server and bind the server to two ports (one for client initiated and one for server), but there must be a smarter way to solve this, or?

Thanks in advance,
Mike

My current game, Minecraft meets Farmville and goes online Smiley
State of Fortune | Discussion thread @ JGO
Offline Nate

JGO Kernel


Medals: 145
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #1 - Posted 2010-09-06 02:13:25 »

You can pass something to the server that it passes back so you can correlate what you receive to what you sent. When you receive you shouldn't be expecting anything specific. Read the bytes, probably create an object from the bytes, then use instanceof to run logic on the object.

Offline Mike

JGO Ninja


Medals: 71
Projects: 1
Exp: 5 years


Java guru wanabee


« Reply #2 - Posted 2010-09-06 08:21:00 »

Interesting, that'll throw most of my code upside down :-D

So a thread that always listens to everything the server has to say and if it has to do with what I requested in a different thread (which will always be the case) it sends the response back to that thread (and class) based upon the identifier sent to the server?

Sounds quite complicated to me for something that in theory is quite simple Smiley Isn't it easier to have the server listen to two ports and use the different ports for the differently initiated communications?

Kind regards,
Mike

My current game, Minecraft meets Farmville and goes online Smiley
State of Fortune | Discussion thread @ JGO
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Matzon

JGO Knight


Medals: 19
Projects: 1


I'm gonna wring your pants!


« Reply #3 - Posted 2010-09-06 10:08:24 »

1) two threads per connection (reader / writer)
2) use the selection api (nio)

I'd pair both solutions with a queue in a producer/consumer pattern.

oh, and always let the client connect to the server initially - anything else is error prone.

Offline Mike

JGO Ninja


Medals: 71
Projects: 1
Exp: 5 years


Java guru wanabee


« Reply #4 - Posted 2010-09-06 11:16:35 »

1) two threads per connection (reader / writer)
2) use the selection api (nio)

I'd pair both solutions with a queue in a producer/consumer pattern.

I heard nio was really difficult (especially as I never programmed anything in the network layer before) but I will have a look at it and see if it can solve the question in one way or another.

I guess I'll have to go down the path of two threads indeed and pass some identifier with the connection, It'll take a wrapper class though and some rethinking of my data structure but it'll save me opening two sockets (which still feels like a good alternative to me :-P

If anyone has anything else to throw in I'd appreciate the input, otherwise it's time for me to start coding Smiley

Thanks to Matzon and Nate! Smiley

Kind regards,
Mike

My current game, Minecraft meets Farmville and goes online Smiley
State of Fortune | Discussion thread @ JGO
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 741
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #5 - Posted 2010-09-06 15:49:05 »

1) two threads per connection (reader / writer)
2) use the selection api (nio)

I'd pair both solutions with a queue in a producer/consumer pattern.

how would you combine NIO ((probably) one thread per process) with two threads per connection?

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Matzon

JGO Knight


Medals: 19
Projects: 1


I'm gonna wring your pants!


« Reply #6 - Posted 2010-09-06 16:05:30 »

I was going for 1 OR 2 - not both Smiley

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 741
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #7 - Posted 2010-09-06 16:11:01 »

Ah...

Quote
I'd pair either solution with a queue in a producer/consumer pattern.

... then I'd have understood persecutioncomplex silly me.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Mike

JGO Ninja


Medals: 71
Projects: 1
Exp: 5 years


Java guru wanabee


« Reply #8 - Posted 2010-09-06 16:46:07 »

Haha, I did exactly the same Riven and actually thought of the same question but didn't dare to ask all mighty Matzon Wink

I started looking into NIO, noticed it was way above my head and read that it didn't perform as fast so I'm going down the route of a read and a write thread.

Kind regards,
Mike

My current game, Minecraft meets Farmville and goes online Smiley
State of Fortune | Discussion thread @ JGO
Offline Nate

JGO Kernel


Medals: 145
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #9 - Posted 2010-09-06 20:55:20 »

Thar be dragons in NIO. Stick with the simpler old IO classes or use a framework that hides the NIO crap.

Not sure why you need to restructure your code so much? First you write something to identify what class is being sent (eg, a String class name or, better, an int or short ID), then you write the bytes for that object. On the other side you read the identifier, create an instance of that class, then read the bytes and populate your instance. Then you have code like this:

1  
2  
3  
4  
5  
6  
7  
if (object instanceof SetPosition) {
   SetPosition setPosition = (SetPosition)object;
   // ...
} else if (object instanceof AddCharacter) {
   AddCharacter addCharacter = (AddCharacter)object;
   // ...
}

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Mike

JGO Ninja


Medals: 71
Projects: 1
Exp: 5 years


Java guru wanabee


« Reply #10 - Posted 2010-09-06 23:04:29 »

Rewrite completed and server initiated communication is in place. It is SO cool to be able to build roads and change the landscape and so on on one computer and it changes instantly on another computer :-D

Thanks guys!
Mike

My current game, Minecraft meets Farmville and goes online Smiley
State of Fortune | Discussion thread @ JGO
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.

ctomni231 (31 views)
2014-07-18 06:55:21

Zero Volt (27 views)
2014-07-17 23:47:54

danieldean (23 views)
2014-07-17 23:41:23

MustardPeter (24 views)
2014-07-16 23:30:00

Cero (39 views)
2014-07-16 00:42:17

Riven (41 views)
2014-07-14 18:02:53

OpenGLShaders (28 views)
2014-07-14 16:23:47

Riven (28 views)
2014-07-14 11:51:35

quew8 (25 views)
2014-07-13 13:57:52

SHC (61 views)
2014-07-12 17:50:04
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!