Page 2 of 2 FirstFirst 12
Results 21 to 32 of 32

Thread: H3lp

  1. #21
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,544
    Rep Power
    23

    Default

    It's hard to explain...
    And harder to understand.
    Your talking about a server and clients all getting the info needed to keep in synch.

    Sounds like you need to do some design work to figure this whole app out.

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

    Default

    Quote Originally Posted by Norm View Post
    Sounds like you need to do some design work to figure this whole app out.
    What kind of design? You mean I need to make a gui? (if yes, then I've already have one)

    Do you know any good java books about networking?
    Last edited by PhQ; 08-08-2010 at 02:12 PM.

  3. #23
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,544
    Rep Power
    23

    Default

    Sorry, I have no books to recommend.

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

    Default

    Quote Originally Posted by Norm View Post
    Sorry, I have no books to recommend.
    Ok, so what kind of design should I make?

  5. #25
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,544
    Rep Power
    23

    Default

    What is your app supposed to do?
    How do the clients and server interact?
    When one client does something, how does that become known to the rest of the clients?

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

    Default

    Quote Originally Posted by Norm View Post
    What is your app supposed to do?
    It's like a game, you run around with a square.
    Quote Originally Posted by Norm View Post
    How do the clients and server interact?
    Every time a person clicks up/down/left/right the client will send the x and y and the color to the server, and server will send everything to the other clients.

    Quote Originally Posted by Norm View Post
    When one client does something, how does that become known to the rest of the clients?
    I'm not sure how to do that.

    Here is the source code, could you tell me if I could improve it?

    Client:
    Java Code:
    package org.square.Client;
    
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.io.*;
    import java.net.*;
    
    import javax.swing.JApplet;
    
    import org.square.Server.SquareDetails;
    
    public class CreateApplet extends JApplet implements MouseListener, KeyListener{
    	
    	public int[] playerIDs = {};
    	public boolean loggedIn = false;
    	public String color;
    	
    	private static final long serialVersionUID = 1L;
    	int x;
    	int y;
    	
    	public void init(){
    		setSize(500, 500);
    		this.addMouseListener(this);
    		this.addKeyListener(this);
    	}
    	
    	public void paint(Graphics g){
    		if(loggedIn == false){
    			g.setFont(new Font("Arial", Font.PLAIN, 24));
    			
    			g.drawString("Select a color", 200, 200);
    			// COLORS
    			g.setColor(Color.black);
    			g.drawString("Black", 240, 230);
    			g.setColor(Color.red);
    			g.drawString("Red", 240, 250);
    			g.setColor(Color.yellow);
    			g.drawString("Yellow", 240, 270);
    			g.setColor(Color.blue);
    			g.drawString("Blue", 240, 290);
    			
    			//COLOR BOXES
    			g.setColor(Color.black);
    			g.drawRect(240, 210, 75, 85);
    			g.drawLine(240, 231, 315, 231);
    			g.drawLine(240, 251, 315, 251);
    			g.drawLine(240, 271, 315, 271);
    			
    		}
    		if(loggedIn == true){
    			g.setColor(Color.WHITE);
    			g.fillRect(0, 0, 500, 500);
    			CreateSquare(g);
    		}
    	}
    	
    	public enum colors{
    		black, red, yellow, blue, noColor;
    	}
    	
    	public colors getColors(){
    		if(color == null)
    			return colors.noColor;
    		if(color.equals("black")){
    			return colors.black;
    		}
    		if(color.equals("red")){
    			return colors.red;
    		}
    		if(color.equals("yellow")){
    			return colors.yellow;
    		}
    		if(color.equals("blue")){
    			return colors.blue;
    		}
    		
    		return colors.noColor;
    	}
    	
    	public void CreateSquare(Graphics g){
    		switch(getColors()){
    		case noColor:
    			break;
    			
    		case black:
    			g.setColor(Color.black);
    			g.fillRect(x, y, 50, 50);
    			break;
    			
    		case red:
    			g.setColor(Color.red);
    			g.fillRect(x, y, 50, 50);
    			break;
    			
    		case yellow:
    			g.setColor(Color.yellow);
    			g.fillRect(x, y, 50, 50);
    			break;
    			
    		case blue:
    			g.setColor(Color.blue);
    			g.fillRect(x, y, 50, 50);
    			break;
    		}
    	}
    	
    	public void mouseClicked(MouseEvent e) {
    		int x = e.getX();
    		int y = e.getY();
    		int MinX = e.getX();
    		int MinY = e.getY();
    		int MaxX = e.getX();
    		int MaxY = e.getY();
    		if(loggedIn == false){
    //				xMin      xMax        yMin      yMax
    			if(MinX > 240 && MaxX < 315 && MinY > 210 && MaxY < 230){
    				System.out.println("Clicked on BLACK");
    				color = "black";
    				loggedIn = true;
    				repaint();
    			}
    			if(MinX > 240 && MaxX < 315 && MinY > 230 && MaxY < 250){
    				System.out.println("Clicked on RED");
    				color = "red";
    				loggedIn = true;
    				repaint();
    			}
    			if(MinX > 240 && MaxX < 315 && MinY > 250 && MaxY < 270){
    				System.out.println("Clicked on YELLOW");
    				color = "yellow";
    				loggedIn = true;
    				repaint();
    			}
    			if(MinX > 240 && MaxX < 315 && MinY > 270 && MaxY < 295){
    				System.out.println("Clicked on BLUE");
    				color = "blue";
    				loggedIn = true;
    				repaint();
    			}
    		}
    		
    		
    		System.out.println();
    		System.out.println("X >" + x + "<");
    		System.out.println("Y >" + y + "<");
    	}
    
    	@Override
    	public void mouseEntered(MouseEvent arg0) {
    		// NEVER USED
    		
    	}
    
    	@Override
    	public void mouseExited(MouseEvent arg0) {
    		// NEVER USED		
    	}
    
    	@Override
    	public void mousePressed(MouseEvent arg0) {
    		// NEVER USED		
    	}
    
    	@Override
    	public void mouseReleased(MouseEvent arg0) {
    		// NEVER USED		
    	}
    
    	@Override
    	public void keyPressed(KeyEvent ke) {
    		switch (ke.getKeyCode()) {
            case KeyEvent.VK_UP:
            	if(y > 0){
            		y--;
            		repaint();
            	}
            	if(y > 0){
            		y--;
            		repaint();
            	}
            	break;
            case KeyEvent.VK_DOWN:
            	if(y < 450){
            		y++;
            		repaint();
            	}
            	if(y < 450){
            		y++;
            		repaint();
            	}
            	break;
            case KeyEvent.VK_LEFT:
            	if(x > 0){
            		x--;
            		repaint();
            	}
            	if(x > 0){
            		x--;
            		repaint();
            	}
            	break;
            case KeyEvent.VK_RIGHT:
            	if(x < 450){
            		x++;
            		repaint();
            	}
            	if(x < 450){
            		x++;
            		repaint();
            	}
            	break;
    		}
    		
    	}
    
    	@Override
    	public void keyReleased(KeyEvent arg0) {
    		// NOT USED	
    	}
    
    	@Override
    	public void keyTyped(KeyEvent arg0) {
    		// NOT USED
    	}
    	
    	/* ================================== CONNECTION ============================	 */
    	
    	private Socket socket;
    	private ObjectOutputStream out;
    	private ObjectInputStream in;
    	
    	public void connectToServer(){
    		try {
    			socket = new Socket("localhost", 44444);
    			out = new ObjectOutputStream(socket.getOutputStream());
    			in = new ObjectInputStream(socket.getInputStream());
    			
    		} catch (UnknownHostException e) {
    			System.out.println("Cannot connect to the server. UnknownHost.");
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    		
    		
    		
    	}
    	
    	public void updateSquares(){
    		try {
    			SquareDetails SQ = (SquareDetails) in.readObject();
    			
    			
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (ClassNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		
    		
    	}
    }
    Server:

    Java Code:
    package org.square.Server;
    
    import java.net.*;
    import java.io.*;
    
    public class Server implements Runnable {
    
    	static Socket csocket;
    
    	Server(Socket csocket) {
    		this.csocket = csocket;
    	}
    
    	public static void main(String args[]) throws Exception {
    		ServerSocket ssock = new ServerSocket(1234);
    		System.out.println("Listening");
    		while (true) {
    			Socket sock = ssock.accept();
    			String test = ssock.getInetAddress().getHostName();
    			System.out.println("Connected from " + test);
    			new Thread(new Server(sock)).start();
    		}
    	}
    
    	public void run() {
    		try {
    			ObjectOutputStream out = new ObjectOutputStream(csocket.getOutputStream());
    			ObjectInputStream in = new ObjectInputStream(csocket.getInputStream());
    
    			for (int i = 100; i >= 0; i--) {
    				out.writeObject(new SquareDetails(10, 10, "red"));
    		        out.flush();
    		        out.reset();
    		        
    			}
    			out.close();
    			in.close();
    			csocket.close();
    		} catch (IOException e) {
    			System.out.println(e);
    		}
    	}
    
    }
    SquareDetails:
    Java Code:
    package org.square.Client;
    
    
    import java.io.Serializable;
    
    public class SquareDetails implements Serializable {
    	
    	private int x;
    	private int y;
    	private String color;
    	public SquareDetails(int x, int y, String color) {
    		this.x = x;
    		this.y = y;
    		this.color = color;
    	}
    	public int getX() {
    		return x;
    	}
    	public void setX(int x) {
    		this.x = x;
    	}
    	public int getY() {
    		return y;
    	}
    	public void setY(int y) {
    		this.y = y;
    	}
    	
    	public String getColor() {
    		return color;
    	}
    	public void setColor(String color) {
    		this.color = color;
    	}
    }
    Last edited by PhQ; 08-08-2010 at 02:30 PM.

  7. #27
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,544
    Rep Power
    23

    Default

    Your second answer answers your last question at a high level. Now how does the server do that? What kind of tables/collections will the server need to keep track of its clients?
    How many threads will the server need to handle incoming traffic and to be able to send changes to the clients.

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

    Default

    Quote Originally Posted by Norm View Post
    Your second answer answers your last question at a high level. Now how does the server do that? What kind of tables/collections will the server need to keep track of its clients?
    How many threads will the server need to handle incoming traffic and to be able to send changes to the clients.
    I'm confused.
    I don't know how to make my server do that.

    I have posted my sourcecode in my last post.
    Last edited by PhQ; 08-08-2010 at 03:04 PM.

  9. #29
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,544
    Rep Power
    23

    Default

    A way to start would be to have only one client and get that to work. The server would only save the client's info and for debug print it out.

    Then figure out how to send a client's info to a group of clients.
    And how each client would display the info about the other clients.

    Break this up into separate projects with hardcoding as needed to work out the details before trying to merge each technique into the big program.

    By hardcoding I mean have the server send a client info package to a client where the info is hardcoded in server. That's all that would happen in that test: start the server, start a client, have the server send the hardcoded client info to the client. The client would display that info.
    End of test.

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

    Default

    Quote Originally Posted by Norm View Post
    A way to start would be to have only one client and get that to work. The server would only save the client's info and for debug print it out.

    Then figure out how to send a client's info to a group of clients.
    And how each client would display the info about the other clients.

    Break this up into separate projects with hardcoding as needed to work out the details before trying to merge each technique into the big program.

    By hardcoding I mean have the server send a client info package to a client where the info is hardcoded in server. That's all that would happen in that test: start the server, start a client, have the server send the hardcoded client info to the client. The client would display that info.
    End of test.
    Ok, I will start doing that tomorrow,
    could you check my code to see if I can improve it?

    Thank you for your help :)

  11. #31
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,544
    Rep Power
    23

    Default

    Its hard to make comments on code that is not documented. How does anyone know if the code does what the design says it should be doing.

    For example, in the server there is a loop:
    for (int i = 100; i >= 0; i--) {

    Why does it iterate for 100 times?

    Another thing: what are the hardcoded literals in the following:
    if(MinX > 240 && MaxX < 315 && MinY > 210 && MaxY < 230){

    Better to replace the literals with final ints with selfdefining names like:
    final static int MaxX = 240;

    Your variable names don't follow standards. Variables should start with lowercase letters.

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

    Default

    Quote Originally Posted by Norm View Post
    Its hard to make comments on code that is not documented. How does anyone know if the code does what the design says it should be doing.

    For example, in the server there is a loop:
    for (int i = 100; i >= 0; i--) {

    Why does it iterate for 100 times?

    Another thing: what are the hardcoded literals in the following:
    if(MinX > 240 && MaxX < 315 && MinY > 210 && MaxY < 230){

    Better to replace the literals with final ints with selfdefining names like:
    final static int MaxX = 240;

    Your variable names don't follow standards. Variables should start with lowercase letters.
    Ok, thank you VERY much !

Page 2 of 2 FirstFirst 12

Posting Permissions

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