Well, I am actually making a custom 2d turn based gaming engine in Java. So far I have actually gotten a solid based down, the ability to load a seamless game world (no instances) as well as a layered world, with a terrain layer, buildings layer, unit layer then an effect layer (but I can alter order, add or delete from this very easily). Also made a custom world loader. But I have moved on to creating a simple network script, which I will elaborate on at a later time. Now my problem is, I looked at a lot of examples and I found one I liked. I altered it to an extent, and because the engine / game is turn based, I needed to make the client server connection turn based in a sense. So I programmed a myTurn variable into the client and server, and set it to do a function if myTurn is true (which is to send a turnOver message), and if it is false then to recieve a Message. Once the 2 are connected, the client sends the first turnOver message, and the server recieves it. Now it is the server's turn to send the same message back, but the client just closes before the server sends the message back. I cannot figure out why this occurs though, and that is my problem. Here is my current server and client code:

Server.java

import java.io.*;
import java.net.*;

import javax.swing.JOptionPane;
public class Server{
	ServerSocket providerSocket;
	Socket connection = null;
	ObjectOutputStream out;
	ObjectInputStream in;
	String message;
	
	boolean myTurn = false;
	
	Server(){}
	
	void run()
	{
		try{
			providerSocket = new ServerSocket(1019, 10);
			
			System.out.println("Waiting for connection");
			connection = providerSocket.accept();
			System.out.println("Connection received from " + connection.getInetAddress().getHostName());
			
			out = new ObjectOutputStream(connection.getOutputStream());
			out.flush();
			in = new ObjectInputStream(connection.getInputStream());
			
			do{
				if(myTurn)
				{
					sendMessage("turnOver");
					myTurn = false;
					Thread.sleep(1000);
				}
				else
				{
					recvMessage();
				}
			}while(!message.equals("bye"));
		}
		catch(IOException ioException){
			ioException.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally{
			try{
				in.close();
				out.close();
				providerSocket.close();
			}
			catch(IOException ioException){
				ioException.printStackTrace();
			}
		}
	}
	
	void sendMessage(String msg)
	{
		try{
			out.writeObject(msg);
			out.flush();
			System.out.println("I said " + msg);
		}
		catch(IOException ioException){
			ioException.printStackTrace();
		}
	}
	
	void recvMessage()
	{
		try {
			message = (String)in.readObject();
			if(message == "turnOver")
			{
				myTurn = true;
			}
			System.out.println(message);
		} catch (IOException e) {
		} catch (ClassNotFoundException e) {
		}
	}
	
	public static void main(String args[])
	{
		Server server = new Server();
		while(true){
			server.run();
		}
	}
}

Client.java

import java.io.*;
import java.net.*;
public class Client{
	Socket requestSocket;
	ObjectOutputStream out;
 	ObjectInputStream in;
 	String message;
 	
 	boolean myTurn = true;
 	
	Client(){}
	
	void run()
	{
		try{
			requestSocket = new Socket("localhost", 1019);
			System.out.println("Connected to localhost in port 2004");
			
			out = new ObjectOutputStream(requestSocket.getOutputStream());
			out.flush();
			in = new ObjectInputStream(requestSocket.getInputStream());
			
			do{
				if(myTurn)
				{
					sendMessage("turnOver");
					myTurn = false;
					Thread.sleep(1000);
				}
				else
				{
					recvMessage();
				}
			}while(!message.equals("bye"));
		}
		catch(UnknownHostException unknownHost){
			System.err.println("You are trying to connect to an unknown host!");
		}
		catch(IOException ioException){
			ioException.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally{
			try{
				in.close();
				out.close();
				requestSocket.close();
			}
			catch(IOException ioException){
				ioException.printStackTrace();
			}
		}
	}

	void sendMessage(String msg)
	{
		try{
			out.writeObject(msg);
			out.flush();
			System.out.println("I said " + msg);
		}
		catch(IOException ioException){
			ioException.printStackTrace();
		}
	}
	
	void recvMessage()
	{
		try {
			message = (String)in.readObject();
			if(message == "turnOver")
			{
				myTurn = true;
			}
			System.out.println(message);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	public static void main(String args[])
	{
		Client client = new Client();
		client.run();
	}
}

If anyone could show me the error in my code, I would gladly appreciate it :D. I simply cannot find out what is wrong with this code, oh and if I solve the problem I will make sure the solution is here, so others can use the code ;). Thanks everyone!

- Pranav

but the client just closes before the server

have you tried debugging the code by adding println() statements to show where and what and why?
What do you expect the code in the finally clause to do? And when?

Yes I have debugged the code using JOption.showMessageDialogs as well as println(). I just got rid of most of so people wouldnt need to do the hassle of removing them if they wanted to. The finally is supposed to do what scripted in it :), and that is to close the socket and terminate the connection.

but the client just closes before the server sends the message back

I thought that was the problem as you stated above.

When you looked at your debug code output, did you see why the client closed the session before the server sends the message?

That was the problem, but i couldnt manage to get a glimpse of the problem. I believe it was an IOException, but I am not 100% sure. I tried Thread.sleep and JOptionPane.showMessageDialog. The latter didnt work beause e.printStackTrace() had no return, so I did e.toString and in the server it said EOFException or something like that. Try compiling the program, I cannot really give you much accurate details because nothing worked for me :(. Thank You!

if(message == "turnOver")

I just saw this. Do NOT use == to compare the contents of two objects. There are methods to do that. object1 == object2 tests if the two references are to the same object.
It is possible to have 2 objects with same contents, but they won't be at the same addresses.

Hi,
What you will get is NullPointerEception because the "message" variable is null the instruction that cause the exception follows (in the Client class):

while(!message.equals("bye"));

try to initialize it.

Hope it helps!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.