Okay, I know there are a lot of similar questions here, but i couldn't find out my problem by going through them. So i have to post this.Sorry for that guys. Here is my prob... I made a chat server that worked on netbeans console for output a few days ago and that worked fine. But now i am making that with the Jframes. But when i click on the send button on the frame of one client the, button freezes and nothing happens. The message doesn't goes to the other frame of the second client.
here is my client side code. I have made the thread class in the client class itself.I don't know if that would make a problem.,
public class chat extends javax.swing.JFrame {
Socket socket;
DataOutputStream dos ;
chat() {
initComponents();
}
private void sendActionPerformed(java.awt.event.ActionEvent evt) {
try{
dos = new DataOutputStream(socket.getOutputStream());
String data ="";
data = msg.getText();
do{
dos.writeUTF(msg.getText());
dos.flush();
}
while(data!= "stop");
}
catch(Exception e)
{
JOptionPane.showMessageDialog(this, e, "Error @ Cthread1", JOptionPane.ERROR_MESSAGE);
}
finally{
try{
dos.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(this,e,"Error @Finally of Cthread1",JOptionPane.ERROR_MESSAGE);
}
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
try{
chat n = new chat();
n.run();
n.setVisible(true);
}catch(Exception e){
JOptionPane.showMessageDialog(null,e,"main",JOptionPane.ERROR_MESSAGE);
}
}
});
}
private javax.swing.JLabel jLabel1;
public javax.swing.JTextField msg;
private javax.swing.JButton send;
public void run()
{
try{
socket = new Socket("127.0.0.1", 1036);
dos = new DataOutputStream(socket.getOutputStream());
Cthread2 thread2 = new Cthread2(socket);
thread2.start();
}
catch(Exception e){
JOptionPane.showMessageDialog(null, e,"Error @ MAin run", JOptionPane.ERROR_MESSAGE);
}
}
class Cthread2 extends Thread{
Socket socket;
Cthread2(Socket socket) {
this.socket = socket;
}
public void run() {
try {
String fromServer ="";
while(fromServer != "stop")
{
DataInputStream sr = new DataInputStream(socket.getInputStream());
fromServer = sr.readUTF();
msg.setText(fromServer);
}
} catch (IOException e) {
JOptionPane.showMessageDialog(null,e,"Error @ Cthread2", JOptionPane.ERROR_MESSAGE);
}
}
}
}
And here is my server class
public class Multiserver {
ServerSocket serversocket;
Socket socket;
ArrayList<Socket> al = new ArrayList<Socket>();
DataInputStream dis;
DataOutputStream dos;
Multiserver() throws IOException
{
serversocket = new ServerSocket(1036);
//JOptionPane.showMessageDialog(null,"Server Started on port 1036","Info !", JOptionPane.ERROR_MESSAGE);
System.out.println("Server Started on port 1036");
while(true)
{
socket = serversocket.accept();
System.out.println(socket);
al.add(socket);
Pthread thread = new Pthread(socket, al);
thread.start();
}
And this is the server thread
public class Pthread extends Thread {
DataInputStream dis;
DataOutputStream dos;
ArrayList al;
Socket socket;
Pthread(Socket socket, ArrayList al)
{
this.al = al;
this.socket = socket;
}
public void run()
{
try{
String data =new chat().msg.getText();
dis = new DataInputStream(socket.getInputStream());
while(data != null)
{
data = dis.readUTF();
if(data != "stop")
broadcast(data);
else
{
dos = new DataOutputStream(socket.getOutputStream());
dos.writeUTF(data);
dos.flush();
}
}
}
catch(Exception e){
JOptionPane.showMessageDialog(null,e,"Error @run",JOptionPane.ERROR_MESSAGE);
}
}
public void broadcast(String data )
{
try{
Iterator it = al.iterator();
while(it.hasNext())
{
Socket socket1 = (Socket)it.next();
dos = new DataOutputStream(socket1.getOutputStream());
dos.writeUTF(data);
dos.flush();
}
}
catch(Exception e){
JOptionPane.showMessageDialog(null, e,"Error @ broadcast",JOptionPane.ERROR_MESSAGE);
}
}
}
I tried finding out what the problem was, but i just cant seem to get it. I get stuck in when i try to run the program in my mind to understand it. Its frustrating. Thanks for anybody who could help me out with whatever the problem is. And yeah. I have tried to just change my previous program that worked on console to the one that uses frames.