MTG beta 1.1

My MTG beta 1.1 available here. Code here. Patch notes here.

You need the newest Java SE 7 to run it. Download from here. If you don’t know which one to choose, you probably need Windows x86 Online.

Do you know what are two the worst things in programming?

1. Initialising and closing a program. Closing is usually easier, because we can often just use System.exit(0), until we want to close only specified fields of program. On the other hand, initialising a program often requires as much code as all program’s features which program uses once initialised. I have encountered many problems with closing sockets of disconnected players, closing server sockets, informing about everything all connected clients and informing a server’s UI about its state. After about 10 hours of struggles it all seems to work correctly – clients can disconnect even during initialisation phase (i.e. when the server is up, but the game has not yet started) and both existing and new clients are informed correctly about what’s going on, but never mind with those details. I just could not crash neither a server, nor a client.

2. The second worst thing is making a program “idiot resistant” as it called friend of mine. When a program asks a user for an integer, we can not assume that the user entered an integer. The program has to check if it is really an integer and if it is not, it has not show a proper message like “you will not crash me that way! haha!”, ask for an integer again etc. And all of this has to be in a loop, because user may again enter a non-integer… Generally, user interface is the worst part of every program. I remember my few little programs which have 100-200 lines of real functionality (including checking arguments legality) and three times more lines of GUI…

But it’s enough of my complaints, I like programming anyway ;) I have come up against an interesting problem with Sockets. Consider following server and client code:

public class Server {
   public static void main(String[] a) throws Exception {
        ServerSocket ss = new ServerSocket(56789);
        Socket s = ss.accept();
        ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
        while (true) {
            try {
                Object obj = ois.readObject();
                System.out.println(obj);
            } catch (Exception ex) {
                System.out.println(ex);
            }
            Thread.sleep(500);
        }
    }
}
public class Client {
    public static void main(String[] a) throws Exception {
        Socket s = new Socket("localhost", 56789);
        ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
        oos.flush();
        oos.writeObject("TEST");
        oos.flush();
        //s.close();
    }
}

Now server prints “TEST” and then keeps printing “java.net.SocketException: Connection reset”. If we uncomment s.close() in client, server keeps printing “java.io.EOFException”. The task is to differ between client’s closure or broken connection (and then break server’s loop) and error while sending object. Client’s process cannot be closed, because we want the program to run and reuse a socket (e.g. connect to different server). I assume that end of file exception may occur during normal data transfer and therefore server’s loop has to continue in such case. I have discussed this problem with friend of mine and we think that server’s socket somehow keeps an eye on the connection and checks if client is responding. If client is closed unexpectedly, nothing is sent to server and SocketException is thrown. While when the client’s socket is closed, it sends something to the server. What is the solution I used? Send some specific data to the server to let it know that it can break its loop:

public class Server {
   public static void main(String[] a) throws Exception {
        ServerSocket ss = new ServerSocket(56789);
        Socket s = ss.accept();
        ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
        while (true) {
            try {
                Object obj = ois.readObject();
                if (obj.getClass().equals(Disconnect.class)) {
                    break;
                } else {
                    //do something what client wants
                }
            } catch (Exception ex) {
                //handle error and continue listening
            }
        }
        s.close();
    }
}
public class Client {
    public static void main(String[] a) throws Exception {
        Socket s = new Socket("localhost", 56789);
        ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
        oos.flush();
        oos.writeObject("TEST");
        oos.flush();
        oos.writeObject(new Disconnect());
        oos.flush();
        s.close();
    }
}
public class Disconnect {
}

About Jaroslaw Pawlak

I have done MSci in Computer Science at King’s College London and currently work as Software Engineer specialising in Java. I spend most of my time in front of computer improving my programming (and other) skills or just relaxing with a good game. I also train some sports but at the moment I am not a member of any club. I love cycling and volleyball, but I have also played a lot of football, tennis and trained martial arts.

Posted on October 5, 2011, in Magic the Gathering - Virtual Table, My Projects. Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: