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) {
}
}
}