同时更新同一个类的多个实例的对象

时间:2016-05-25 11:34:38

标签: java multithreading swing networking

我正在尝试开发基于GUI的聊天服务器。我构建了一个客户端类,其中包含为客户端生成GUI的代码以及写入服务器然后从服务器读取的方法。客户端类单击send按钮时,将消息写入ServerSocket的缓冲区。

但问题是,在我在客户端GUI的JTextField中写入消息后执行多个客户端,然后单击“发送”后,该消息仅在该特定的JTextArea中可见客户端的实例,而其他客户端实例的JTextArea保持为空,即不会发生更新。

我在控制台中注意到JVM将客户端类的实例放在EventQueue

我希望更新应该发生在每个客户端实例的每个JTextArea中。有人可以帮我实现这个目标吗?

  

客户代码

import java.io.*;
import java.net.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

 class TestClient88 implements ActionListener
{
    Socket s;
    DataOutputStream dout;DataInputStream din;BufferedReader br;

    static String nameOfClient=null;
    static String message=null;
    JFrame frame;
    JTextArea textArea;
    JTextField textField;
    JButton button;
public TestClient88 ()  
    {         //just at the time of login
        System.out.println("enter ur name");

             nameOfClient=new Scanner(System.in).nextLine();
frame=new JFrame(nameOfClient);
    textArea=new JTextArea();
    textArea.setBounds(0,0,500,400);
    textArea.setEditable(false);
    textField=new JTextField();
    textField.setBounds(0,405,350,30);

    // textField.setText("test");

    button=new JButton("send");
    button.setBounds(355,405,100,30);
    // button.addActionListener(this);
    button.addActionListener(this);

    frame.add(textField);
    frame.add(textArea);
    frame.add(button);
    frame.setSize(500,500);
    frame.setLayout(null);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        try{

            s=new Socket("localhost",10);
            System.out.println(s);
            din=new DataInputStream(s.getInputStream());
            dout=new DataOutputStream(s.getOutputStream());

         br=new BufferedReader(new InputStreamReader(System.in));//to put the name of client in the server socket

        }catch(Exception e){}   
    }

    public void actionPerformed(ActionEvent ae)
    {
        message=textField.getText();
        System.out.println("message is "+message);
        textField.setText("");
        try{
        clientchat();
    }catch(Exception e){}
    }
    public void clientchat() throws IOException
   {                           // //receiving thread to receive message from server
    System.out.println("after start "+Thread.currentThread().getName());   //Main thread

    String s1=null;          
            s1=message;
            System.out.println("s1 is "+s1);
try{
        dout.writeUTF(nameOfClient+" : " +s1);  
        message=null;
        dout.flush();

// Thread.sleep(20000);
      String s2=din.readUTF();
                System.out.println("message sent from server is "+ s2);
               textArea.append(s2+"\n");

           }catch(Exception e){}

   }

   public static void main(String ar[])
   {
     new TestClient88();
   }

}

  

服务器代码

import java.io.*;
import java.net.*;
import java.util.*;

public class Myserver1
{
    ArrayList al=new ArrayList();ServerSocket ss;Socket s;

    // static ArrayList clientsList=new ArrayList(); 
      static int count;
     ObjectInputStream oin;
    public Myserver1()
    {
        try{
            ss=new ServerSocket(10);
            System.out.println("server socket is "+ss);
            while(true)
            {
                s=ss.accept();//waits for the client socket
                System.out.println("client connected");
                System.out.println("mike testing");

                al.add(s);

                 Iterator i=al.iterator();
            while(i.hasNext())
            {
                System.out.println("inside while");
                System.out.println(i.next());
            }
                System.out.println("startting server thread ");
                Runnable r=new MyThread(s,al);
                Thread t=new Thread(r);
                t.start();
            }
        }catch(Exception e){}
        }


        public static void main(String ar[])
        {
            new Myserver1();
        }
}
    class MyThread  implements Runnable
    {
        Socket s;ArrayList al;
        MyThread(Socket s,ArrayList al)
        {
            this.s=s;
            this.al=al;
        }
        public void run()
        {
            String s1;int i=0;
            try{
                DataInputStream din=new DataInputStream(s.getInputStream());
                do{

                    s1=din.readUTF();//message of each client

                        System.out.println(s1);
                    if(!s1.equals("stop"))
                          {
                           tellEveryone(s1); 
                           }
                        else
                        {
                            DataOutputStream dout=new DataOutputStream(s.getOutputStream());
                            dout.writeUTF(s1);
                            dout.flush();
                            al.remove(s);//logging out a client from the server.
                        }
                        }while(!s1.equals("stop"));
                }catch(Exception e){}
            }
            public void tellEveryone(String s1)
            {
                Iterator i=al.iterator();
                while(i.hasNext())
                {
                    try{
                        Socket sc=(Socket)i.next();
                        DataOutputStream dout=new DataOutputStream(sc.getOutputStream());
                        dout.writeUTF(s1);
                        dout.flush();
                        // System.out.println("client");
                    }catch(Exception e){}
                }
            }


}

0 个答案:

没有答案