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