如何在基于swing的应用程序的jtextarea上涉及套接字程序的java聊天信使上显示连接的客户端 server swing gui
我希望无论何时任何客户端连接/断开,信息都显示在上面服务器帧的jtextarea上。 由于我的项目包括一个聊天室,所以每当客户进入聊天室时,我也想显示所有连接/断开连接的客户端的列表,任何用户都可以进行私聊。 client chatroom gui
//this is event handling code when events start from serverJFrame gui
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try
{
JOptionPane.showMessageDialog(new JFrame(),"start", "Hello",JOptionPane.INFORMATION_MESSAGE);
BroadcastServer.broadcastmain();
}
catch(Exception e)
{
e.printStackTrace();
}
}
private void jPasswordField2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try
{
Textvalue = new String(jPasswordField2.getPassword());
if(Textvalue.equalsIgnoreCase(""))
{
JOptionPane.showMessageDialog(new JFrame(),"EMPTY PASSWORD FIELD","ERROR",JOptionPane.ERROR_MESSAGE);
return;
}
new DBconnect();//DBconnect is a class that connect with database and validate password
if(DBconnect.check==101)
{
jButton4.setEnabled(true);
jButton3.setEnabled(true);
jButton2.setEnabled(true);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
//broadcastmain() is in BroadcastServer class
public static void broadcastmain()
{
try
{
System.out.println("server started");
ArrayList al=new ArrayList();
ServerSocket ss=new ServerSocket(1500);
while(true)
{
System.out.println("waiting for client request");
Socket sk=ss.accept();
al.add(sk);
new ClientThread(al,sk);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
//ClientThread class
public class ClientThread extends Thread {
ArrayList al;
Socket sk;
public static ArrayList<String> ar1=new ArrayList<String>();
ClientThread(ArrayList al,Socket sk)
{
this.al=al;
this.sk=sk;
start();
}
public void run()
{
try
{
DataInputStream din=new DataInputStream(sk.getInputStream());
String name=din.readUTF();
System.out.println(name+" : connected");
ar1.add(name);
DataOutputStream dout=new DataOutputStream(sk.getOutputStream());
String msg="welcome "+name+" ,start chatting";
dout.writeUTF(msg);
dout.flush();
while(true)
{
String msg1=din.readUTF();
if(msg1.equalsIgnoreCase("stop"))
{
al.remove(sk);
ar1.remove(name);
System.out.println(name+" disconnected");
break;
}
broadcastMsg(name+" : "+msg1);
}
}
catch(Exception e)
{
e.printStackTrace();
}
//ChatClient class for client
public class ChatClient {
static FileWriter fr;
public static String name;
public static void ChatClientmain()
{
try
{
System.out.println("client started");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter your name");
name=br.readLine();
fr=new FileWriter(name,true);
Socket sk=new Socket("localhost",1500);
System.out.println("client connected");
DataOutputStream dout=new DataOutputStream(sk.getOutputStream());
dout.writeUTF(name);
dout.flush();
new ReaderThread(sk);
while(true)
{
System.out.println("Enter message, Enter stop to terminate");
String msg=br.readLine();
dout.writeUTF(msg);
fr.write(msg);
dout.flush();
fr.flush();
if(msg.equalsIgnoreCase("stop"))
{
System.out.println("YOUR DISCONNECTED");
break;
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
//ReaderThread class
public class ReaderThread extends Thread{
Socket sk;
ReaderThread(Socket sk)
{
this.sk=sk;
setDaemon(true);
start();
}
public void run()
{
try
{
DataInputStream din=new DataInputStream(sk.getInputStream());
while(true)
{
String msg=din.readUTF();
System.out.println(msg);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}