Results 1 to 5 of 5
Thread: Creating a server
- 04-26-2012, 06:28 PM #1
Making Code Efficient and Easy to Understand
Hi,
I am trying to make a server for my card game, but as I do it it gets more confusing. And when I do it the code becomes very messy.
I happens to the client as well.
For example this is my some of the code from my server:
How can I make my servers more organised and more efficient? My code confuses me and if I forget what gets sent where I have to look at the code and try to simulate it all in my head.Java Code:import java.io.*; import java.net.*; public class client extends Player implements Runnable { public PlayerHandler handler; private Socket mySock; private BufferedReader in; private BufferedWriter out; private boolean isActive; public client(Socket s, int _playerId) { super(_playerId); mySock = s; try { in = new BufferedReader(new InputStreamReader(mySock.getInputStream())); out = new BufferedWriter(new OutputStreamWriter(mySock.getOutputStream())); } catch (java.io.IOException ioe) { misc.println("!! CRITICAL ERROR !!"); ioe.printStackTrace(); } } String name = ""; @Override public void run() { isActive = false; long serverSessionKey = 0, clientSessionKey = 0; try { if (this.playerId != -1) { name = in.readLine(); if(checkUsername(name) == 0){ System.out.println("Connection rejected. Username '" + name + "' already connected."); sendLine(1); disconnect(); }else{ this.username = name; sendLine(2); sendLine(PlayerHandler.maxPlayers-1); } } else { System.out.println("SERVER FULL !"); sendLine(0); disconnect(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public boolean initialized = false; private void initializeGame() { while(server.playerHandler.getConnections() != PlayerHandler.maxPlayers-1){ sendLine("wait"); System.out.println(server.playerHandler.getConnections()); try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } for(int i = 0; i < PlayerHandler.maxPlayers;i++){ if(PlayerHandler.players[i] != null){ if(PlayerHandler.players[i].playerId != playerId){ sendLine(PlayerHandler.players[i].username); } } } sendLine("done"); initialized = true; server.game.ready = true; } public void startGame(){ } public void sendLine(Object line){ try { out.write(line + "\n"); out.flush(); misc.println(name + " | " + line); } catch (IOException e) { if(e.getMessage().contains("Connection reset by peer")){ disconnect(); }else if(e.getMessage().contains("Socket closed")){ disconnect(); }else{ e.printStackTrace(); } } } public void disconnect(){ try { if(playerId != -1) PlayerHandler.players[playerId] = null; else PlayerHandler.players[playerId].disconnected = true; mySock.close(); misc.println(name + " disconnected."); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public int checkUsername(String username) { for (Player p : PlayerHandler.players) { if (p != null && p.username.equals(username)) { return 0; } } return 1; } @Override public boolean process() { // TODO Auto-generated method stub return false; } @Override public boolean packetProcess() { if(!initialized){ initializeGame(); }else{ server.game.ready = true; } return false; } }
Thank you.Last edited by PhQ; 04-26-2012 at 08:28 PM.
- 04-26-2012, 08:03 PM #2
Re: Creating a server
bump
- 04-26-2012, 08:42 PM #3
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
Re: Creating a server
Java is object oriented - why not make use of the power of Object Oriented programming?
And FWIW, there is absolutely no need to bump a thread that less than 2hrs old...in fact in most cases it will hurt your chances of receiving help.
- 04-26-2012, 09:13 PM #4
Re: Creating a server
Sorry, I thought that if the thread will go down, no one will be able to see the thread.
I know how to use object oriented proramming in java. I am just not sure how to order the code when creating servers. The way I am doing it right now is confusing me.
I am not sure where to put classes in what packeges.
I don't understand the need of creating interfaces in my projects.
In my server, I don't understand how to code it so I can easily control what is going in and out of the server.
- 04-26-2012, 10:40 PM #5
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
Re: Creating a server
a) You state in your first post "code from my server", but the class is named 'client'...confusing both in naming and code conventions. b) There are so many ways to implement and clean your code it is quite difficult to lend guidance c) some guidance in one direction: I have no clue what Player is, but presume it contains program logic...why extend this class for IO? You may be better off implementing a listener notification scheme - when the Player changes it notifies listeners - one of which can be the Client/Server Communication.
Similar Threads
-
Creating a basic proxy server
By driiper in forum NetworkingReplies: 2Last Post: 04-27-2012, 01:04 AM -
Creating a server cache
By salmontres in forum Advanced JavaReplies: 0Last Post: 12-05-2011, 10:01 PM -
creating a server and client in java
By krishforever in forum New To JavaReplies: 25Last Post: 01-11-2011, 02:13 AM -
creating server
By krishforever in forum New To JavaReplies: 1Last Post: 01-10-2011, 07:28 AM -
problem in creating a class that should always run in the server
By harsha.udupa2008 in forum New To JavaReplies: 1Last Post: 07-14-2010, 02:56 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks