Results 1 to 5 of 5
  1. #1
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Post 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:
    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;
    	}
    
    }
    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.

    Thank you.
    Last edited by PhQ; 04-26-2012 at 08:28 PM.

  2. #2
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: Creating a server

    bump

  3. #3
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

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

  4. #4
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: Creating a server

    Quote Originally Posted by doWhile View Post
    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.
    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.

  5. #5
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default 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

  1. Creating a basic proxy server
    By driiper in forum Networking
    Replies: 2
    Last Post: 04-27-2012, 01:04 AM
  2. Creating a server cache
    By salmontres in forum Advanced Java
    Replies: 0
    Last Post: 12-05-2011, 10:01 PM
  3. creating a server and client in java
    By krishforever in forum New To Java
    Replies: 25
    Last Post: 01-11-2011, 02:13 AM
  4. creating server
    By krishforever in forum New To Java
    Replies: 1
    Last Post: 01-10-2011, 07:28 AM
  5. problem in creating a class that should always run in the server
    By harsha.udupa2008 in forum New To Java
    Replies: 1
    Last Post: 07-14-2010, 02:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •