Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
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  
  Have i implemented this right?  (Read 1147 times)
0 Members and 1 Guest are viewing this topic.
Offline Embedded

Junior Member


Medals: 1



« Posted 2011-11-16 18:10:59 »

Ive added some networking to a 2D game of mine and basically read a simple serversocket and socket tutorial and just went from there without any other knowledge, i just wanted to make sure im not doing it a really long badly drawn out way.

The server loops round with the ol' somesocket = serversocket.accept(); in a while loop waiting for clients, once its caught one it stores it in a static array of sockets then creates a new thread to communicate with the client.
This Thread then creates a BufferedReader and PrintStream for the socket that was passed to it sends the map data to client once then for the rest of its life it then just listens for x,y of player to be sent to that socket and reads it when it is and stores it in a static array of xy integers in another class(the one this all started from).
In the main server Thread its in a while loop which sleeps 12 milli everytime in this while loop it repaints the window to display some info, its a JFrame btw and also calls a sendToAll() meathod which sends all xy's, out to all players using the static arrays of xy cords and sockets mentioned earlier, it has to create a new printstream everytime it does this.

Client side when the game loads the static meathod load in the loadMap class is called which creates a new static socket and trys to connect to the server, then it creates a bufferedReader and gets ye olde map. Then the game continues running.
Everytime player.move() is called in the main gameLoop it creates a new threads which creates a printstream from the static socket in the loadMap class and sends the xy to server.
Then every time the gameloops a getPlayerPostitions meathod is called directly from the loop which gets the x,y's of all players.

Are the static sockets a good thing? should i be doing this a diffrent way? seems to be working atm and not lagging but thats on local host and i havent actually added anything to the clients yet to draw the other players with the recived x, y's.

tl;dr good server architecture to 2D game
Offline ra4king

JGO Kernel


Medals: 347
Projects: 3
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2011-11-18 05:15:31 »

Sounds about right. If you have any real doubts, post some code since that is more helpful that a vague description Smiley

Offline counterp

Senior Member


Medals: 11



« Reply #2 - Posted 2011-11-18 22:45:44 »

Well I have no idea what kind of game this is - moving in a board game is a lot different than moving a character in a 2D platformer for example.

Quote
Everytime player.move() is called in the main gameLoop it creates a new threads which creates a printstream from the static socket in the loadMap class and sends the xy to server.

that sounds a little off
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 347
Projects: 3
Exp: 5 years


I'm the King!


« Reply #3 - Posted 2011-11-18 22:51:06 »

Oh yeah, I must have overlooked that part.
@OP
Don't created a lot of threads, what you do is create 1 thread per client and let it run in a loop receiving commands and replying.

Online Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Reply #4 - Posted 2011-11-20 19:00:58 »

Oh yeah, I must have overlooked that part.
@OP
Don't created a lot of threads, what you do is create 1 thread per client and let it run in a loop receiving commands and replying.
Now, dont punch me for suggesting NIO if you have more than 50 players. There is libraries that is simpler to use than standard I/O, and uses nio. Kryonet, for instance.

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.

Pippogeek (38 views)
2014-09-24 16:13:29

Pippogeek (29 views)
2014-09-24 16:12:22

Pippogeek (18 views)
2014-09-24 16:12:06

Grunnt (42 views)
2014-09-23 14:38:19

radar3301 (24 views)
2014-09-21 23:33:17

BurntPizza (61 views)
2014-09-21 02:42:18

BurntPizza (31 views)
2014-09-21 01:30:30

moogie (36 views)
2014-09-21 00:26:15

UprightPath (49 views)
2014-09-20 20:14:06

BurntPizza (53 views)
2014-09-19 03:14:18
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!