Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 16 of 16

Thread: Server-client ping system won't work. (message being sent but not received)

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Server-client ping system won't work. (message being sent but not received)

    Hello
    I am currently making a game in java for a school project, but I have got a problem. I am currently trying to set up an online connection system, that pings to see if the connection is still alive. But somehow the ping-message is not received. This is the code in the server for receiving the ping: https://github.com/dvdbrander/WvW_Se...in/Client.java and this is the code that sends it: https://github.com/dvdbrander/WvW/bl...onnection.java

    client
    package online;
     
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.Socket;
    import java.net.UnknownHostException;
    import java.util.concurrent.CopyOnWriteArrayList;
     
    import launcher.MainThread;
     
    public class Connection extends Thread{
    	Socket socket;
    	private String host = "localhost";
    	private InputStreamReader in;
    	private PrintWriter out;
    	@SuppressWarnings("unused")
    	private Online online;
    	private MainThread main;
    	private CopyOnWriteArrayList<String> messages = new CopyOnWriteArrayList<String>();
    	private long lastPinged;
     
    	public void init(Online online, MainThread main){
    		this.online = online;
    		this.main = main;
    	}
     
    	public void connect() {
    		try {
    			socket = new Socket(host ,25367);
    			in = new InputStreamReader(socket.getInputStream());
    			out = new PrintWriter(socket.getOutputStream(),true);
    		} catch (UnknownHostException e) {
    			System.out.println("Could not connect.");
    			e.printStackTrace();
    		} catch (IOException e) {
    			System.out.println("Could not connect.");
    			e.printStackTrace();
    		}
    		System.out.println("Connected");
     
    	}
     
    	public void ping() {
    		out.println("1|"+System.currentTimeMillis());
    		out.flush();
    		System.out.println("ping...");
    		lastPinged = System.currentTimeMillis();
    	}
     
    	public void update(){
     
    	}
     
    	@Override
    	public void run() {
    		while (main.isRunning()){
    			if (System.currentTimeMillis() - lastPinged >= 1000){
    				ping();
    			}
    			try {
    				if (in.ready()){
    					String string = "";
    					int chr = in.read();
    					while (chr != -1){
    						string += Character.toString((char) chr);
    					}
    					messages.add(string);
    					System.out.println(string);
    				}
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    		}
    	}
     
     
    }
    server
    package main;
     
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.Socket;
     
    public class Client extends Thread{
     
    	private Socket socket;
    	private Main main;
    	private InputStreamReader in;
    	private PrintWriter out;
     
    	public Client(Socket socket, Main main) {
    		this.socket = socket;
    		this.main = main;
    	}
     
    	public void init(){
    		System.out.println("Client connected: "+socket.getInetAddress()+":"+socket.getPort());
    		try {
    			in = new InputStreamReader(socket.getInputStream());
    			out = new PrintWriter(socket.getOutputStream(),true);
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
     
    	@Override
    	public void run() {
    		while (main.isRunning()){
    			try {
    				if (in.ready()){
    					String string = "";
    					int chr = in.read();
    					while (chr != -1){
    						string += Character.toString((char) chr);
    					}
    					System.out.println(string);
    				}
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    		}
    	}
     
     
     
    }
    Does anybody have an idea why it won't receive anything?
    Thanks.
    Last edited by d4dev; June 3rd, 2012 at 01:39 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,029
    Thanks
    52
    Thanked 2,278 Times in 2,249 Posts

    Default Re: Server-client ping system won't work. (message being sent but not received)

    Please post the code on the forum.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jun 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Server-client ping system won't work. (message being sent but not received)

    Did that, all other code is still visible in the github.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,029
    Thanks
    52
    Thanked 2,278 Times in 2,249 Posts

    Default Re: Server-client ping system won't work. (message being sent but not received)

    The posted code does not compile because of missing classes.

    Try making a small program that compiles, executes and shows the problem.
    Last edited by Norm; June 3rd, 2012 at 01:51 PM.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jun 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Server-client ping system won't work. (message being sent but not received)

    Why whould you need to compile it? I really do think it's something little I've looked over since it almost always is... And it doesn't give an error or so, it just never says anything in the server while it does say "ping..."in the client once per second.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,029
    Thanks
    52
    Thanked 2,278 Times in 2,249 Posts

    Default Re: Server-client ping system won't work. (message being sent but not received)

    I let the computer tell me what is wrong. Too easy to misread code.
    Also you cant always tell if something important is missing. Having all the code keeps that from happening.
    If only the posted code is needed to find the problem, can you modify it and add to ti so that it compiles, executes and shows the problem?
    Last edited by Norm; June 3rd, 2012 at 03:11 PM.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jun 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Server-client ping system won't work. (message being sent but not received)

    Ok, but I have to go in a hurry now, forgot the time. Ill make a compiling version later.

  8. #8
    Junior Member
    Join Date
    Jun 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Server-client ping system won't work. (message being sent but not received)

    Okay, I made it so it can compile.
    server
    clientaccepter.java, accepts incoming connections
    package server;
     
    import java.io.IOException;
    import java.net.ServerSocket;
    import java.net.Socket;
     
    public class ClientAccepter extends Thread{
     
    	public static void main(String[] args) {
    		new ClientAccepter();
    	}
     
    	ServerSocket server = null;
     
    	public ClientAccepter() {
    		init();
    		this.start();
    	}
     
     
    	public void init() {
    		try {
    			server = new ServerSocket(25367);
    		} catch (IOException e) {
    			System.out.println("Could not listen on port 25367");
    			System.exit(-1);
    		}
    	}
     
    	@Override
    	public void run() {
    		while (true){
    			try {
    				Socket clientsocket = server.accept();
    				Client client = new Client(clientsocket);
    				client.init();
    				((Thread)client).start();
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
     
    		}
    	}
     
    }
    client.java, maintains the connections
    package server;
     
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.Socket;
     
     
    public class Client extends Thread{
    	private InputStreamReader in;
    	private PrintWriter out;
    	private Socket socket;
     
    	public Client(Socket socket) {
    		this.socket = socket;
    	}
     
    	public void init(){
    		System.out.println("Client connected: "+socket.getInetAddress()+":"+socket.getPort());
    		try {
    			in = new InputStreamReader(socket.getInputStream());
    			out = new PrintWriter(socket.getOutputStream(),true);
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
     
    	@Override
    	public void run() {
    		while (true){
    			try {
    				if (in.ready()){
    					String string = "";
    					int chr = in.read();
    					while (chr != -1){
    						string += Character.toString((char) chr);
    					}
    					System.out.println(string);
    				}
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    		}
    	}
     
    }
    client
    connection.java, connects and pings
    package client;
     
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.Socket;
    import java.net.UnknownHostException;
    import java.util.concurrent.CopyOnWriteArrayList;
     
     
    public class Connection extends Thread{
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		new Connection();
    	}
     
    	public Connection() {
    		connect();
    		((Thread)this).start();
    	}
     
    	Socket socket;
    	private String host = "localhost";
    	private InputStreamReader in;
    	private PrintWriter out;
    	private CopyOnWriteArrayList<String> messages = new CopyOnWriteArrayList<String>();
    	private long lastPinged;
     
     
    	public void connect() {
    		try {
    			socket = new Socket(host ,25367);
    			in = new InputStreamReader(socket.getInputStream());
    			out = new PrintWriter(socket.getOutputStream(),true);
    		} catch (UnknownHostException e) {
    			System.out.println("Could not connect.");
    			e.printStackTrace();
    		} catch (IOException e) {
    			System.out.println("Could not connect.");
    			e.printStackTrace();
    		}
    		System.out.println("Connected");
     
    	}
     
    	public void ping() {
    		out.println("1|"+System.currentTimeMillis());
    		out.flush();
    		System.out.println("ping...");
    		lastPinged = System.currentTimeMillis();
    	}
     
    	public void update(){
     
    	}
     
    	@Override
    	public void run() {
    		while (true){
    			if (System.currentTimeMillis() - lastPinged >= 1000){
    				ping();
    			}
    			try {
    				if (in.ready()){
    					String string = "";
    					int chr = in.read();
    					while (chr != -1){
    						string += Character.toString((char) chr);
    					}
    					messages.add(string);
    					System.out.println(string);
    				}
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    		}
    	}
     
    }

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,029
    Thanks
    52
    Thanked 2,278 Times in 2,249 Posts

    Default Re: Server-client ping system won't work. (message being sent but not received)

    Try debugging the code by adding lots of println statements to show where the code is executing and to show the values of all the variables as their values change. The print out will show you where the problem is.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    Jun 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Server-client ping system won't work. (message being sent but not received)

    The problem is, I already did that and it did say ping... in the client, and in the server the while loop was running... I'll try it again.

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,029
    Thanks
    52
    Thanked 2,278 Times in 2,249 Posts

    Default Re: Server-client ping system won't work. (message being sent but not received)

    Did you add enough printlns to show what the code is doing and how the values of variables change?
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Junior Member
    Join Date
    Jun 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Server-client ping system won't work. (message being sent but not received)

    Probably not...

  13. #13
    Junior Member
    Join Date
    Jun 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Server-client ping system won't work. (message being sent but not received)

    Hey, it gets stuck in the while loop...
    if (in.ready()){
    					System.out.println("in ready...");
    					String string = "";
    					int chr = in.read();
    					while (chr != -1){
    						string += Character.toString((char) chr);
    					        chr = in.read();//Forgot this one.... Supid...
    					}
    					System.out.println("a"+string);
    				}
    Why?
    Shouldn't in.read give -1 when it reaches the end?
    Edit:
    Ow wait, it keeps reading one, probably some in.next() or so I forgot... Searching...

    Edit 2:
    I found it. Most stupid mistake ever. However, I did expect something like this to be the problem. Thanks
    Last edited by d4dev; June 4th, 2012 at 09:05 AM.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,029
    Thanks
    52
    Thanked 2,278 Times in 2,249 Posts

    Default Re: Server-client ping system won't work. (message being sent but not received)

    What is the value of chr? Cast it to char when you print it.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Member
    Join Date
    Jul 2012
    Posts
    119
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default Re: Server-client ping system won't work. (message being sent but not received)

    d4dev
    I see you have 3 diff. threads. 1 server (ClientAccepter), 1 Connection and 1 Client...
    I see how the server accepts an incomming request and starts the Client thread...
    But I don't know where the Connection is invoked...
    Your server can receive an incoming request only if this Connection thread is started somewhere...then your Client thread is started...when your client is started IT PINGS !

  16. #16
    Member
    Join Date
    Jul 2012
    Posts
    119
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default Re: Server-client ping system won't work. (message being sent but not received)

    d4dev
    I review your codes and don't find the relationship between the thread Connection and the Client+ClientAccepter thread. May I give you a hint? Don't use excessive threads. They can cause lots of hidden problems (e.g. Synchronization)

Similar Threads

  1. Replies: 0
    Last Post: May 31st, 2012, 05:35 PM
  2. server/client application fails when client closes
    By billykid in forum Java Networking
    Replies: 4
    Last Post: January 26th, 2012, 12:54 AM
  3. How does an Operating System Work-VIDEO TUTORIAL
    By rainbow9 in forum Java Theory & Questions
    Replies: 1
    Last Post: December 15th, 2011, 12:06 AM
  4. Default system look and feel does not work
    By goodguy in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 5th, 2011, 11:46 PM
  5. Streaming Raw Data to a Client - huge message loss
    By MarkusTandy in forum Java Networking
    Replies: 0
    Last Post: February 19th, 2011, 06:41 PM