Search code examples
javasocketsobjectinputstreamsocketserver

Java chat application only picking up some sent messages


I'm writing an all-in-one java chat program which will either act as a client or a server. I'm currently having this problem where, after the connection is established, the program only successfully recieves (or sends?) some of the messages. I've used a loop to spam through a load of junk and I've seen that the other end will only pick up some of the messages. I've never got a message to send manually. Here is the code:

public class ConnectionThread implements Runnable {

private Connection c = Connection.getInstance();
private ChatInterface chatInterface;
private static ConnectionThread serverThread;
private ServerSocket serverSocket;
private Socket socket;
private ObjectInputStream dataIn;
private ObjectOutputStream dataOut;

public ConnectionThread() {}

public static synchronized ConnectionThread getInstance() {
    if (serverThread == null) {
        serverThread = new ConnectionThread();
    }
    return serverThread;
}

public void run() {
    // If the programs role is server, set up the server
    if (c.getRole() == ConnectionRole.SERVER) {
        try {
            setupServer();
            waitForConnection();
            setupStreams();
        } catch (IOException e) {
            e.printStackTrace();
        }
        do {
            try {
                chatInterface.addToChatHistory(dataIn.readUTF());
            } catch (IOException e) {
                e.printStackTrace();
            }
        } while (c.getRole() == ConnectionRole.SERVER);
    }
    // If the programs role is client, set up a connection to the server
    if (c.getRole() == ConnectionRole.CLIENT) {
        try {
            setupClient();
            setupStreams();
        } catch (IOException e) {
            e.printStackTrace();
        }
        do {
            try {
                chatInterface.addToChatHistory(dataIn.readUTF());
            } catch (IOException e) {
                e.printStackTrace();
            }
        } while (c.getRole() == ConnectionRole.CLIENT);
    }
}

private void setupClient() throws IOException {
    System.out.println("ATTEMPTING TO CONNECT...");
    socket = new Socket("127.0.0.1", 8080);
    System.out.println("CONNECTED!");
}

private void setupServer() throws IOException {
    System.out.println("SETTING UP SERVER..");
    serverSocket = new ServerSocket(8080, 1);
    System.out.println("SERVER SETUP");
}

private void waitForConnection() throws IOException {
    System.out.println("WAITING FOR A CONNECTION...");
    socket = serverSocket.accept();
    System.out.println("CONNECTION RECIEVED");
}

private void setupStreams() throws IOException {
    System.out.println("SETTING UP STREAMS...");
    dataOut = new ObjectOutputStream(socket.getOutputStream());
    dataIn = new ObjectInputStream(socket.getInputStream());
    chatInterface = ChatInterface.getInstance();
    System.out.println("STREAMS SETUP");
}

public void sendMessage(String message) throws IOException {
    System.out.println("SENDING MESSAGE...");
    dataOut.writeUTF(message);
    chatInterface.addToChatHistory(message);
    System.out.println("MESSAGE SENT!");
}
}

Can anyone tell me why not all messages are being sent/picked up properly? I've been playing with it for quite a while now and I can't figure out why.


Solution

  • I found out after following EJP's recommendation to switch to DataInput/OutputStreams which worked straight away. Although I did need to be using ObjectInput/OutputStreams so I switched back. I found that I got the same issue again, until I switched to write/readObject instead of write/readUTF. If I cast the readObject to (String) it would then manage to receive every message. So if anyone is having the same problem with ObjectInput/OutputStreams using write/readUTF try using write/readObject instead.