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

Thread: Movement

  1. #21
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default Re: Movement

    is the way correct how im doing it now?
    Are you showing all of the code involved? Without code that compiles and executes (a SSCCE) it is hard to say.

  2. #22
    elamre is offline Member
    Join Date
    Jan 2012
    Posts
    23
    Rep Power
    0

    Default Re: Movement

    [Host - NetworkSend.java]
    Java Code:
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    
    public class NetworkSend extends Thread{
    	int tempX,tempY,weapon,shooting,x,y;
    	char direction;
    	String[] line;
    	String conLine;
    	public boolean connected;
        ServerSocket Server = null;
        PrintStream os;
        BufferedReader in;
        Socket Client = null;
        
    	NetworkSend(int port){
            try {
                Server = new ServerSocket(port);
             }
             catch (IOException e) {
                System.out.println(e);
             } 
    	}
    	public void connect(){
    	    try {
    	    	   Client = Server.accept();
    	    	   camera.connected = true;
    	           os = new PrintStream(Client.getOutputStream());
    	           in = new BufferedReader(new InputStreamReader(Client.getInputStream()));
    	           connected = true;
    	        }   
    	    catch (IOException e) {
    	           System.out.println(e);
    	        }
    	}
    	public void run(){
    		System.out.println("Host started!");
    		while(camera.running){
    			System.out.println("Host sending...");
    			Send(camera.player_o.getX(),camera.player_o.getY(),camera.player_o.getDirection(),camera.player_o.selection,1);
    			System.out.println("Host send!");
    			update();
    			System.out.println("Host recieved!");
    		}
    	}
    	public void Send(int x, int y, char direction, int weaponType, int shoot){
    		os.println(","+x+","+y+","+direction+","+weaponType+","+shoot);
    		os.flush();
    	}
       	public void update(){
    		try {conLine = in.readLine();} 
    		catch (IOException e) {e.printStackTrace();}		
    		
    		line = conLine.split(",");
    		x = Integer.parseInt(line[1]); 
    		y = Integer.parseInt(line[2]);
    		direction = line[3].charAt(0);
    		weapon = Integer.parseInt(line[4]);
    		shooting = Integer.parseInt(line[5]);
    		System.out.println("X: "+x+" Y: "+y+" D: "+direction+" W: "+weapon+" S: "+shooting);
    	}
    
    }
    [Client - Client.java]
    Java Code:
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.Socket;
    import java.net.UnknownHostException;
    
    //P,x,y,direction,weapon,shooting:
    //P,50,50,'n',3,1
    public class Client extends Thread {
    	int tempX, tempY, weapon, shooting, x, y;
    	char direction;
    	String[] line;
    	String conLine;
    	int port;
    	boolean running;
    	String ip;
    	String name;
    	Socket echoSocket;
    	PrintWriter out;
    	BufferedReader in;
    	int id;
    	String sendString;
    	String recieveString;
    	
    	Client(String host, int port) {
    		camera.connected = true;
    		this.ip = host;
    		line = new String[6];
    		this.port = port;
    		x = 0;
    		y = 0;
    
    	}
    
    	public void run() {
    		System.out.println("Client thread started! connecting...");
    		while (camera.running) {
    			update();
    			System.out.println("Client info recieved!");
    			Send(camera.player_o.getX(), camera.player_o.getY(),
    					camera.player_o.getDirection(), camera.player_o.selection,
    					1);
    			System.out.println("Client info send!");
    		}
    	}
    
    	public void Send(int x, int y, char direction, int weaponType, int shoot) {
    		out.println(","+x + "," + y + "," + direction + "," + weaponType + ","+ shoot);
    	}
    
    	public void Connect() {
    		System.out.println("Attemping to connect to host " + ip + " on port " + port + ".");
    		try {
    			echoSocket = new Socket(ip, port);
    			out = new PrintWriter(echoSocket.getOutputStream(), true);
    			in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
    		} catch (UnknownHostException e) {
    			System.err.println("Don't know about host: " + ip);
    			System.exit(1);
    		} catch (IOException e) {
    			System.err.println("Couldn't get I/O for " + "the connection to: " + ip);
    			System.exit(1);
    		}
    		//out.println(name);
    	}
    
    	public void update() {
    		try {conLine = in.readLine();}
    		catch (IOException e) {e.printStackTrace();}
    		try{
    		line = conLine.split(",");
    		x = Integer.parseInt(line[1]);
    		y = Integer.parseInt(line[2]);
    		direction = line[3].charAt(0);
    		weapon = Integer.parseInt(line[4]);
    		shooting = Integer.parseInt(line[5]);
    		}catch(NumberFormatException e){
    			System.out.println("ERROR PARSING "+conLine);
    		}
    		System.out.println("X: " + x + " Y: " + y + " D: " + direction + " W: " + weapon + " S: " + shooting);
    	}
    }
    Some code to detirmine the host. Is still manually controlled:
    Java Code:
    	static void init(){
    		running = true;
    		
    		if(host){
    			host_o = new NetworkSend(9988);
    			host_o.connect();
    			host_o.start();
    		}
    		else if(!host){
    			client_o = new Client("192.168.1.40",998);
    			client_o.Connect();
    			client_o.start();
    		}
    	}
    in my paint event
    Java Code:
            if(!camera.host){
            	g2d.fillRect(camera.client_o.getX()-camera.x, camera.client_o.getY()-camera.y, 8, 8);
            }
    	    if(camera.host)
    	       	g2d.fillRect(camera.host_o.getX()-camera.x, camera.host_o.getY()-camera.y, 8, 8);
    I removed the getters and setters from the 2 connection classes, since that is not really necesary for this.
    The code works really good on 1 pc. But as soon as i launch the host on my pc first, And then the client on my laptop for example, it is VERY laggy! :(
    Do i have to use UDP? or can i do it this way (tcp i believe?) with sockets? Also is there any way of speeding the code up?

  3. #23
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default Re: Movement

    If it works ok on one PC then you'll need someone with 2 PCs to test it.
    Does The lagging only happen when you are using a network between PCs?

  4. #24
    elamre is offline Member
    Join Date
    Jan 2012
    Posts
    23
    Rep Power
    0

    Default Re: Movement

    Ive tested it between my pc and another pc from within the network. And from outside the network. This was just imposible, such big lags everywhere!
    Client.setTcpNoDelay(true);
    Made it MUCH more responsive already. Still isn't totally smooth yet. also this is just with 2 players, Imagine with 8 players even!

  5. #25
    elamre is offline Member
    Join Date
    Jan 2012
    Posts
    23
    Rep Power
    0

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Checkers Movement
    By Hollowsoul in forum Advanced Java
    Replies: 14
    Last Post: 08-28-2011, 08:12 PM
  2. Movement/KeyListener freezing..
    By AndroidAppNewbie in forum New To Java
    Replies: 4
    Last Post: 03-10-2011, 04:31 PM
  3. Key/mouse movement
    By falkon114 in forum New To Java
    Replies: 3
    Last Post: 02-21-2011, 02:12 AM
  4. Sprite Movement
    By Curtiz in forum Java Gaming
    Replies: 1
    Last Post: 04-26-2010, 01:31 PM
  5. Movement of balls
    By BlitzA in forum New To Java
    Replies: 8
    Last Post: 01-09-2008, 03:30 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
  •