使用Socket编写类似QQ通讯工具,显示屏幕的历史聊天记录待优化。 import javax.swing.JFrame; import javax.swing.JButton; import javax.swing.JTable; import javax.swing.JTextArea; import javax.swing.table.AbstractTableModel; import javax.swing.table.TableModel; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.List; import java.util.ArrayList; import java.util.Set; import java.util.HashSet; import java.net.Socket; import java.io.InputStream; import java.io.DataOutputStream; import java.io.DataInputStream; import java.io.ObjectInputStream; import java.io.ByteArrayInputStream; import java.io.DataInputStream; class ClientDemo{ public static void main(String[] args) throws Exception{ //String user_name = Integer.toString(socket.getPort()); String user_name = "jiangmin"; int random = (int)(1+Math.random()*(10-1+1)); MyWindow window = new MyWindow(); ClientMessage cm = new ClientMessage(window,user_name + random,0); cm.start(); } } class MyWindow extends JFrame{ private static final long serialVersionUID = -6944831795769317874L; private JTable table; private JTextArea in_text; private JTextArea out_text; public MyWindow(){ ini(); } public JTable getTable(){ return table; } public void ini(){ this.setSize(600,400); this.setLocation(200,200); this.setLayout(null); JButton btn = new JButton("button"); this.add(btn); JTable table = new JTable(); table.setBounds(500,0,100,400); this.add(table); this.table = table; JTextArea out_text = new JTextArea(); out_text.setEditable(false); out_text.setBounds(0,0,480,300); this.add(out_text); this.out_text = out_text; JTextArea in_text = new JTextArea(); in_text.setEditable(true); in_text.setBounds(0,310,400,50); this.add(in_text); this.in_text = in_text; JButton button = new JButton("Send"); button.setBounds(410,310,70,50); button.addMouseListener(new MouseAdapter(){ public void mouseClicked(MouseEvent e){ StringBuilder builder = new StringBuilder(); builder.append(in_text.getText()); builder.append("\r\n"); new ClientMessage(MyWindow.this,builder.toString(),1).start(); in_text.setText(""); } }); this.add(button); this.setVisible(true); } public static void update(JTable table,final List<String> list){ Set<String> set = new HashSet(list); final List<String> new_list = new ArrayList<String>(set); TableModel dataModel = new AbstractTableModel(){ public int getColumnCount(){ return 1; } public int getRowCount(){ return new_list.size(); } public Object getValueAt(int row,int column){ return new_list.get(row); } }; table.setModel(dataModel); } public static void history(MyWindow window,String out_content){ window.out_text.setText(out_content); } } class ClientMessage{ private MyWindow window; private String content; private int type; public ClientMessage(MyWindow window,String content,int type){ this.window = window; this.content = content; this.type = type; } public void start(){ try{ Socket socket = new Socket("127.0.0.1",8888); new ClientPushThread(window,socket,content,type).start(); new ClientPullThread(window,socket,content).start(); }catch(Exception e){ e.printStackTrace(); } } } class ClientPushThread extends Thread{ private MyWindow window; private Socket socket; private String content; private int type; public ClientPushThread(MyWindow window,Socket socket,String content,int type){ super(); this.window = window; this.socket = socket; this.content = content; this.type = type; } public void run(){ System.out.println("push............"); try{ DataOutputStream out = new DataOutputStream(socket.getOutputStream()); byte[] write_buf = ClientUtil.MessagePack(type,content); out.write(write_buf); Thread.sleep(5000); }catch(Exception e){ e.printStackTrace(); } } } class ClientPullThread extends Thread{ private MyWindow window; private Socket socket; private String user_name; public ClientPullThread(MyWindow window,Socket socket,String user_name){ super(); this.window = window; this.socket = socket; this.user_name = user_name; } public void run(){ while(true){ System.out.println("pull............"); try{ DataInputStream in = new DataInputStream(socket.getInputStream()); int type = in.read(); int length = ClientUtil.readMessageLength(in); byte[] content_pack = ClientUtil.readMessage(in,length); if(type == 0){ ByteArrayInputStream bais = new ByteArrayInputStream(content_pack); ObjectInputStream ois = new ObjectInputStream(bais); List<String> list = (List<String>)ois.readObject(); MyWindow.update(window.getTable(),list); }else if(type == 1){ MyWindow.history(window,new String(content_pack)); System.out.println("output text"); } Thread.sleep(5000); }catch(Exception e){ e.printStackTrace(); } } } } class ClientUtil{ public static byte[] readMessage(InputStream in,int length){ try{ byte[] bytes = new byte[length]; in.read(bytes); return bytes; }catch(Exception e){ e.printStackTrace(); } return null; } public static int readMessageLength(InputStream in){ try{ byte[] bytes = new byte[4]; in.read(bytes); int i3 = bytes[0] << 24; int i2 = (bytes[1] & 0xFF) << 16; int i1 = (bytes[2] & 0xFF) << 8; int i0 = (bytes[3] & 0xFF); return i3 | i2 | i1 | i0; }catch(Exception e){ e.printStackTrace(); } return -1; } public static byte[] MessagePack(int type,String content){ byte[] content_pack = content.getBytes(); int length = content_pack.length; byte[] pack = new byte[1 + 4 + length]; pack[0] = (byte)type; byte[] length_pack = ClientUtil.intToByteArr(length); System.arraycopy(length_pack,0,pack,1,4); System.arraycopy(content_pack,0,pack,5,length); return pack; } public static byte[] intToByteArr(int i){ byte[] bytes = new byte[4]; bytes[0] = (byte)(i >> 24); bytes[1] = (byte)(i >> 16); bytes[2] = (byte)(i >> 8); bytes[3] = (byte)i; return bytes; } } import java.net.ServerSocket; import java.net.Socket; import java.io.InputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.ObjectOutputStream; import java.io.ByteArrayOutputStream; import java.util.Set; import java.util.HashSet; import java.util.List; import java.util.ArrayList; class ServerDemo{ public static void main(String[] args) throws Exception{ System.out.println("start............"); ServerMessage sm = new ServerMessage(); sm.start(); System.out.println("begin............"); } } class ServerMessage{ public static List<String> firends = new ArrayList<String>(); public static List<Socket> sockets = new ArrayList<Socket>(); public static StringBuilder builder = new StringBuilder(); public void start(){ try{ ServerSocket ss = new ServerSocket(8888); while(true){ Socket socket = ss.accept(); sockets.add(socket); new ServerPullThread(socket,firends).start(); new ServerPushThread(socket,firends,0).start(); } }catch(Exception e){ e.printStackTrace(); } } } class ServerPullThread extends Thread{ private List<String> list ; private Socket socket; public ServerPullThread(Socket socket,List<String> list){ super(); this.socket = socket; this.list = list; } public void run(){ while(true){ try{ DataInputStream in = new DataInputStream(socket.getInputStream()); int type = in.read(); int length = ServerUtil.readMessageLength(in); byte[] content_pack = ServerUtil.readMessage(in,length); if(type == 0){ System.out.println(content_pack.length); System.out.println("pull............0"); String user_name = new String(content_pack,"utf-8"); list.add(user_name); }else if(type == 1){ System.out.println("pull............1"); new ServerPushThread(socket,content_pack,type).start(); } Thread.sleep(5000); }catch(Exception e){ e.printStackTrace(); } } } } class ServerPushThread extends Thread{ private Socket socket; private List<String> list = new ArrayList<String>(); private byte[] content_pack; private int type; public ServerPushThread(Socket socket,List<String> list,int type){ super(); this.socket = socket; this.list = list; this.type = type; } public ServerPushThread(Socket socket,byte[] content_pack,int type){ super(); this.socket = socket; this.content_pack = content_pack; this.type = type; } public void run(){ while(true){ try{ DataOutputStream out = new DataOutputStream(socket.getOutputStream()); if(type == 0){ System.out.println("push...........1"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(list); byte[] content_pack = baos.toByteArray(); byte[] pack = ServerUtil.MessagePack(type,content_pack); out.write(pack); }else if(type == 1){ System.out.println("push...........1"); String content = new String(content_pack,"utf-8"); ServerMessage.builder.append(content); byte[] new_content_pack = ServerMessage.builder.toString().getBytes(); byte[] pack = ServerUtil.MessagePack(type,new_content_pack); out.write(pack); }else{ System.out.println("..........error"); } Thread.sleep(5000); }catch(Exception e){ e.printStackTrace(); } } } } class ServerUtil{ public static byte[] readMessage(InputStream in,int length){ try{ byte[] bytes = new byte[length]; in.read(bytes); return bytes; }catch(Exception e){ e.printStackTrace(); } return null; } public static int readMessageLength(InputStream in){ try{ byte[] bytes = new byte[4]; in.read(bytes); int i3 = bytes[0] << 24; int i2 = (bytes[1] & 0xFF) << 16; int i1 = (bytes[2] & 0xFF) << 8; int i0 = (bytes[3] & 0xFF); return i3 | i2 | i1 | i0; }catch(Exception e){ e.printStackTrace(); } return -1; } public static byte[] intToByteArr(int i){ byte[] bytes = new byte[4]; bytes[0] = (byte)(i >> 24); bytes[1] = (byte)(i >> 16); bytes[2] = (byte)(i >> 8); bytes[3] = (byte)i; return bytes; } public static byte[] MessagePack(int type,byte[] content_pack){ int length = content_pack.length; byte[] pack = new byte[1 + 4 + length]; pack[0] = (byte)type; byte[] length_pack = intToByteArr(length); System.arraycopy(length_pack,0,pack,1,4); System.arraycopy(content_pack,0,pack,5,length); return pack; } }
本文出自 “森林敏” 博客,请务必保留此出处http://senlinmin.blog.51cto.com/6400386/1790126
原文地址:http://senlinmin.blog.51cto.com/6400386/1790126