0

So i have a basic chat from a tutorial and everything is fine with it except one thing there is no messages. I mean the server running the client too...but there is a problem with the send button or whatever?Am and ah yeah the server.java is quiet slow and sometimes freeze.I used Netbeans's frame option.

This is the client

package chat;

import java.io.DataInputStream;
import java.io.DataOutputStream;

import java.net.Socket;

 public class chat_client extends javax.swing.JFrame {

static DataInputStream din;
static DataOutputStream dout;
static Socket s;


public chat_client() {
    initComponents();
}


private void msg_sendActionPerformed(java.awt.event.ActionEvent evt) {                                         

    try {
    String msgout = "";
   msgout = msg_text.getText().trim();
   dout.writeUTF(msgout);
    } catch(Exception e) {
           }

}                                        

public static void main(String args[]) {




    java.awt.EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            new chat_client().setVisible(true);
        }
    });

    try {
         s = new Socket("127.0.0.1",1201); //The Local address because the Computer is the same

         din = new DataInputStream(s.getInputStream());
         dout = new DataOutputStream(s.getOutputStream());
         String msgin = "";


         while(msgin.equals("exit")) {
             msgin = din.readUTF();
             msg_area.setText(msg_area.getText().trim()+"\n Server:\t"+ msgin);

         }
    }catch (Exception e) {
}
}


}

This is the server

package chat;


import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class chat_server extends javax.swing.JFrame {

 static ServerSocket ss;
 static Socket s;
 static DataInputStream din;
  static DataOutputStream dout;

  public chat_server() {
    initComponents();
}




    msg_text.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            msg_textActionPerformed(evt);
        }
    });

    msg_send.setText("Send");
    msg_send.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            msg_sendActionPerformed(evt);
        }
    });



private void msg_textActionPerformed(java.awt.event.ActionEvent evt) {                                         

}                                        

private void msg_sendActionPerformed(java.awt.event.ActionEvent evt) {                                         
   try {




    String msgout = "";
   msgout = msg_text.getText().trim();
   dout.writeUTF(msgout); // sending the server message to the client
  } catch (Exception e) {


  }
}                                        


public static void main(String args[]) {

    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new chat_server().setVisible(true);
        }
    });

    String msgin = "";
    try {
         ss = new ServerSocket(1201); // The server starting port
          s = ss.accept();             // allow Connection


          din = new DataInputStream(s.getInputStream());
         dout = new DataOutputStream(s.getOutputStream());

         while(!msgin.equals("exit")) {
             msg_area.setText(msg_area.getText().trim()+"\n"+msgin);  //Displaying message

    }

    }catch(Exception e) {

    }
}

                  }
Devyrien
  • 3
  • 5
  • 1
    If your chat program isn't sending messages, then "everything works fine" isnt the first thing that comes to mind. Anyway, you need to either debug the code youself, or reduce your given code to a more readable example – user1231232141214124 Jan 07 '16 at 20:54
  • You have right. I tried to edit more clearly and these are the parts that i wrote. – Devyrien Jan 07 '16 at 21:19

1 Answers1

0

In the client code, in the main method the condition of the while loop is wrong, you missed a ! to negate it (as you did on the server actually). So it should be while (!msgin.equals("exit")).

In the server code, you are not reading from the input stream (as you instead do on the client actually), so you missed the msgin = din.readUTF(); statement in the while loop.
This is also the reason why the server was slow, it was continuously looping and writing an empty text on the text are while it should have been blocked on reading the next input on the input stream.

Further improvements you could apply to your code:

  • Use camel case for class names (i.e. instead of chat_client use ChatClient)
  • Use a logging mechanism (i.e. log4j, or in this sample case use System.out.println("log here the "+variable) calls) to get feedback of actions and interactions
  • Don't leave catch blocks empty: you may miss exceptions. So in the catch blocks at least add statements like System.out.err(e); (but, again, better to use a logging library with the right logging level)
  • Clean the text from the text field on the client side after sending a message (currently the text remains there). A msg_text.setText(""); at the end of the try block of the msg_sendActionPerformed method would already be enough.
Community
  • 1
  • 1
A_Di-Matteo
  • 26,902
  • 7
  • 94
  • 128