Search code examples
javamacosserversocket

Simple java chat program doesn't work on Mac


I have made a simple java chat program that will have 1 server and 1 client. It works fine on windows, it can chat with each other within LAN. But when I try it on Mac computer, it doesn't work. In the server program, if it works properly, it should freeze when I press the start button, and wait for client to join. But in Mac, it does nothing when I press start button. The program doesn't freeze, it's like clicking on a non-coded button. The client in Mac cannot join the server too (server hosted in Windows).

On Windows: Start server --> freeze(wait for client) --> Client joined --> able to chat

On Mac: Start server --> not freeze, like click on normal button --> Client clicked join --> nothing happens

In the Start Server button:

private void startsvbtnActionPerformed(java.awt.event.ActionEvent evt) {                                           
    try {
        // TODO add your handling code here:
        server = new ServerSocket(7430);
        client = server.accept();
        System.out.println("Client request accepted: "+client.getOutputStream());
        dos = new DataOutputStream(client.getOutputStream());
        dis = new DataInputStream(client.getInputStream());
        ReceiveMessage serverThread = new ReceiveMessage(dis,textarea);
        serverThread.start();
    } catch (IOException ex) {
        System.out.println("No client available");
    }

}         

Solution

  • You're checking only IOException, as per doc there could be other exceptions. Catch all and print stacktrace. http://docs.oracle.com/javase/8/docs/api/java/net/ServerSocket.html#accept--

    catch(Exception e){
    e.printStackTrace();
    }