JavaSE小练习(简单的聊天器)
Posted vamps0911
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaSE小练习(简单的聊天器)相关的知识,希望对你有一定的参考价值。
为了巩固JavaSE的知识,练习一哈,大神绕道。下面贴一下代码。
Client端代码
package GUI; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.EOFException; import java.io.IOException; import java.net.Socket; import java.net.SocketException; import java.net.UnknownHostException; import javax.swing.JFrame; import javax.swing.JTextArea; import javax.swing.JTextField; public class ChatClient extends JFrame { /** * */ private static final long serialVersionUID = 1L; JTextField tfTxt = new JTextField(); JTextArea taContent = new JTextArea(); Socket s = null; DataOutputStream dos = null; DataInputStream dis = null; Thread TRecv = new Thread(new RecvThread()); private boolean bConnected = true; public static void main(String[] args) { // TODO Auto-generated method stub new ChatClient().lauchFrame(); } public void lauchFrame(){ //主窗口设置 this.setTitle("弱智交流平台?"); this.setSize(500, 500); this.setLocationRelativeTo(null); this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent arg0){ disconnect(); System.exit(0); } }); //设置布局 this.setLayout(new BorderLayout()); this.add(taContent, BorderLayout.NORTH); this.add(tfTxt, BorderLayout.SOUTH); //设置组件大小 tfTxt.setPreferredSize(new Dimension(500, 50)); taContent.setPreferredSize(new Dimension(500, 400)); //文本域内容自动换行 taContent.setLineWrap(true); //字体设置 Font font1 = new Font("宋体",Font.BOLD,24); taContent.setFont(font1); Font font2 = new Font("宋体",Font.BOLD,18); tfTxt.setFont(font2); //将文本框的内容写入文本域的事件 tfTxt.addActionListener(new TFListener()); this.setVisible(true); //连接服务端 connect(); //启动线程接受信息 TRecv.start(); } private class TFListener implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub //将文本框内容写入文本域 String str = tfTxt.getText().trim(); tfTxt.setText(""); //将文本框内容写入服务端 try{ dos.writeUTF(str); dos.flush(); }catch(IOException e3){ e3.printStackTrace(); } } } public void connect(){ try{ s = new Socket("127.0.0.1", 7777); dos = new DataOutputStream(s.getOutputStream()); dis = new DataInputStream(s.getInputStream()); System.out.println("已经连上服务端!"); }catch(UnknownHostException e1){ e1.printStackTrace(); }catch(IOException e2){ e2.printStackTrace(); } } public void disconnect(){ try{ dos.close(); dis.close(); s.close(); }catch(IOException e){ e.printStackTrace(); } } private class RecvThread implements Runnable{ @Override public void run() { // TODO Auto-generated method stub while(bConnected){ String str; try { str = dis.readUTF(); taContent.setText(taContent.getText()+str+" "); }catch(SocketException e){ System.out.println("退出了!"); bConnected = false; }catch(EOFException e){ bConnected = false; System.out.println("退出了!"); }catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }
Server端
package GUI; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.EOFException; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; import java.net.SocketException; import java.util.ArrayList; import java.util.List; public class ChatServer { List<Client> clients = new ArrayList<Client>(); ServerSocket ss = null; public static void main(String[] args) { new ChatServer().start(); } public void start(){ try{ ss = new ServerSocket(7777); }catch(IOException e){ e.printStackTrace(); } try{ //服务器不止连接一个客户端 while(true){ Socket s = ss.accept(); System.out.println("连接一个客户端!"); Client c = new Client(s); clients.add(c); new Thread(c).start(); } }catch(Exception e){ e.printStackTrace(); }finally{ try { ss.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } private class Client implements Runnable{ private Socket s; private DataInputStream dis; private DataOutputStream dos; public Client(Socket s){ this.s = s; try{ dis = new DataInputStream(s.getInputStream()); dos = new DataOutputStream(s.getOutputStream()); }catch(IOException e){ e.printStackTrace(); } } public void sent(String s){ try { dos.writeUTF(s); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public void run() { Client c = null; try{ while(true){ String str = dis.readUTF(); System.out.println(str); for(int i=0; i<clients.size();i++){ c = clients.get(i); c.sent(str); } } }catch(SocketException e){ if(c != null){ clients.remove(this); } }catch(EOFException e){ System.out.println("客户端断开连接!"); }catch(IOException e){ e.printStackTrace(); }finally{ try{ if(dis != null){ dis.close(); } if(dos != null){ dos.close(); } if(s != null){ s.close(); } }catch(IOException e){ e.printStackTrace(); } } } } }
初学Java到现在,第一次把所学的一些知识,联系起来,虽然是个很小的程序,但是也是有收获的,希望自己继续努力!
以上是关于JavaSE小练习(简单的聊天器)的主要内容,如果未能解决你的问题,请参考以下文章