java gui chat
服务端
import java.awt.BorderLayout; import java.awt.Container; import java.awt.FlowLayout; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; /** * @author konecms * @date 2018年2月4日 * @version 1.0 */ public class ServerChat extends JFrame implements ActionListener,Runnable { JTextField txtPort; JButton btnConnect,btnClose; JTextArea txtLog; ServerSocket servSocket; ThreadSocket clients[]; Thread servThread=null; int PORT; public ServerChat(){ fm(); } public void fm(){ setTitle("聊天室-服务端"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); txtPort=new JTextField("9099",6); btnConnect=new JButton("启动"); btnClose=new JButton("关闭"); txtLog=new JTextArea(8,35); txtLog.setEditable(false); Container cp=this.getContentPane(); JPanel p=new JPanel(); p.setLayout(new FlowLayout(FlowLayout.LEFT)); p.add(new JLabel("端口:")); p.add(txtPort); p.add(btnConnect); p.add(btnClose); cp.add(p,BorderLayout.NORTH); p=new JPanel(); p.setLayout(new FlowLayout(FlowLayout.LEFT)); p.add(new JLabel("日志:")); JScrollPane sp=new JScrollPane(txtLog); p.add(sp); cp.add(p,BorderLayout.CENTER); this.setSize(400,240); this.setLocationRelativeTo(null); this.pack(); this.setVisible(true); btnConnect.addActionListener(this); btnClose.addActionListener(this); } @Override public void run() { // TODO Auto-generated method stub Thread cur=Thread.currentThread(); while(cur==servThread){ try { Socket clientSocket=servSocket.accept(); int i; for(i=0;i<clients.length;i++){ if(clients[i]==null){ clients[i]=new ThreadSocket(this,clientSocket); clients[i].start(); break; } } if(i>clients.length){ clientSocket.close(); clientSocket=null; } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } class ThreadSocket extends Thread{ ServerChat serv; Socket socket; BufferedReader in=null; DataOutputStream out=null; String name; public ThreadSocket(ServerChat serv,Socket socket){ this.serv=serv; this.socket=socket; } public void run(){ while(socket!=null){ try { in=new BufferedReader(new InputStreamReader(socket.getInputStream())); out=new DataOutputStream(socket.getOutputStream()); name=in.readLine(); serv.addClient(name); String str=null; str=in.readLine(); while(!str.equals("Bye")) { serv.addChat(name+":"+str); str=in.readLine(); } serv.delClient(name); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }//end of while }//end of run public void sendMessage(String str){ try { out.writeBytes(str); txtLog.append("发送给客户端的消息:"+str+"\n"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void close(){ try { in.close(); socket.close(); serv=null; socket=null; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }//end of class ThreadSocket @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(e.getSource()==btnConnect){ connect(); } if(e.getSource()==btnClose){ stop(); } } //广播消息 public void addChat(String str) { // TODO Auto-generated method stub try{ for(int i=0;i<clients.length;i++){ if(clients[i]!=null){ clients[i].sendMessage(str+i); } } }catch(Exception e){ e.printStackTrace(); } } public void addClient(String name) { // TODO Auto-generated method stub String str="["+name+"] Enter\n"; txtLog.append(str); try{ for(int i=0;i<clients.length;i++){ if(clients[i]!=null){ clients[i].sendMessage(name+i); } } }catch(Exception e){ e.printStackTrace(); } } public void delClient(String name){ String str="client:"+name+" exit \n"; txtLog.append(str); for(int i=0;i<clients.length;i++){ if(clients[i]!=null){ clients[i].sendMessage(str); if(clients[i].name.equals(name)){ clients[i].close(); clients[i]=null; } } } } public void connect(){ try { PORT=Integer.parseInt(txtPort.getText()); servSocket=new ServerSocket(PORT); clients=new ThreadSocket[20]; String strHost; try{ strHost=InetAddress.getLocalHost().toString(); }catch(Exception e){ strHost="Localhost/127.0.0.1"; } txtLog.append("服务器["+strHost.toUpperCase()+"]启动成功。\n"); txtLog.append("服务器在端口"+PORT+"监听\n\n"); Toolkit.getDefaultToolkit().beep(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } servThread=new Thread(this); servThread.start(); } public void stop(){ if(servThread==null){ JOptionPane.showMessageDialog(this, "服务器未启动 !"); } servThread=null; for(int i=0;i<clients.length;i++){ if(clients[i]!=null){ clients[i].close(); clients[i]=null; } } try { servSocket.close(); clients=null; servSocket=null; txtLog.append("服务器停止!\n"); } catch (IOException e) { // TODO Auto-generated catch block txtLog.append("服务器停止错误!\n"); e.printStackTrace(); } } public static void main(String[] args) { ServerChat chat =new ServerChat(); } }
客户端
import java.awt.BorderLayout; import java.awt.Container; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.Socket; import java.net.UnknownHostException; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; /** * @author konecms * @date 2018年2月4日 * @version 1.0 */ public class ClientChat extends JFrame implements Runnable,ActionListener { JTextField txtHost,txtPort,txtName,txtMessage; JButton btnConnect,btnClose,btnSendMessage; JTextArea ta=null; Socket socket; String HOST; int PORT; Thread myThread; BufferedReader in=null; DataOutputStream out=null; public ClientChat(){ fr(); } public void fr(){ this.setTitle("聊天室-客户端"); Container cp=this.getContentPane(); JPanel p=new JPanel(); p.setLayout(new FlowLayout(FlowLayout.LEFT)); p.add(new JLabel("主机:")); txtHost=new JTextField("127.0.0.1",8); p.add(txtHost); p.add(new JLabel("端口:")); txtPort=new JTextField("9099",8); p.add(txtPort); p.add(new JLabel("网名:")); txtName=new JTextField("k",4); p.add(txtName); btnConnect=new JButton("启动"); p.add(btnConnect); btnClose=new JButton("关闭"); p.add(btnClose); cp.add(p,BorderLayout.NORTH); p=new JPanel(); p.setLayout(new FlowLayout(FlowLayout.LEFT)); ta=new JTextArea(10,45); ta.setEditable(false); JScrollPane sp=new JScrollPane(ta); p.add(sp); cp.add(p,BorderLayout.CENTER); p=new JPanel(); p.setLayout(new FlowLayout(FlowLayout.LEFT)); p.add(new JLabel("消息:")); txtMessage=new JTextField(35); p.add(txtMessage); btnSendMessage=new JButton("发送"); p.add(btnSendMessage); cp.add(p,BorderLayout.SOUTH); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setResizable(false); this.setLocationRelativeTo(null); this.pack(); this.setVisible(true); btnConnect.addActionListener(this); btnClose.addActionListener(this); btnSendMessage.addActionListener(this); } @Override public void run() { // TODO Auto-generated method stub ta.append("ddd"); Thread curThread=Thread.currentThread(); while(curThread==myThread&&socket!=null){ try { String str=in.readLine(); ta.append(str+curThread.getId()+"\n"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public void connect(){ HOST=txtHost.getText(); PORT=Integer.parseInt(txtPort.getText()); if(socket!=null){ JOptionPane.showMessageDialog(this, "已经启动,请勿重复操作 !"); return; } try { socket=new Socket(HOST,PORT); in=new BufferedReader(new InputStreamReader(socket.getInputStream())); //发送用户名: out=new DataOutputStream(socket.getOutputStream()); out.writeBytes(txtName.getText().trim()+"\n"); //获取服务端发送过来的消息 myThread=new Thread(this); myThread.start(); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void sendMessage(String msg){ if(out!=null&&socket!=null){ try { out.writeBytes(msg+"-client\n"); txtMessage.setText(null); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public void close(){ myThread =null; if(socket!=null){ try { sendMessage("Bye"); in.close(); out.close(); socket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } socket=null; } } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(e.getSource()==btnConnect){ connect(); } if(e.getSource()==btnSendMessage){ sendMessage(txtMessage.getText()); } if(e.getSource()==btnClose){ close(); } } public static void main(String[] args) { ClientChat chat=new ClientChat(); } }