?哎呀,还有点不会用,刚明白怎么添加图片,最仅这两天会吧以前做过的东西传到这上面来,希望大家关注
服务器端的代码
import java.awt.BorderLayout; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; import java.util.ArrayList; import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; public class chatServer extends JFrame { private static JTextArea jta = new JTextArea(); private ServerSocket server; private static List<Socket> list = new ArrayList<Socket>(); private ExecutorService exec; public chatServer(){ setLayout(new BorderLayout()); add(new JScrollPane(jta),BorderLayout.CENTER); setTitle("聊天室服务器"); setSize(500,300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); try{ server = new ServerSocket(54321); exec = Executors.newCachedThreadPool(); jta.append("服务器已经启动"); Socket client = null; while(true){ client =server.accept(); list.add(client); exec.execute(new Task(client)); } }catch(Exception e){ } } static class Task implements Runnable{ private Socket client; private DataInputStream fromclient; private DataOutputStream toclient; String msg; public Task(Socket client) throws IOException{ this.client = client; fromclient = new DataInputStream(client.getInputStream()); } public void run() { try{ while((msg=fromclient.readUTF())!=null){ msg = "["+client.getPort()+"]说:"+msg; jta.append(msg+"\n"); sendMessage(); } }catch(Exception e){ } } public void sendMessage() throws IOException{ for(Socket client:list){ toclient= new DataOutputStream(client.getOutputStream()); toclient.writeUTF(msg); } } } public static void main(String[] args){ new chatServer(); } }
?客户端的实现代码:
import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.Socket; import java.net.UnknownHostException; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.JTextField; public class newClient extends JFrame{ private JPanel p= new JPanel(); private JTextField jtf1 = new JTextField(); private JTextArea jta1 = new JTextArea(); private DataOutputStream toserver ; private DataInputStream fromserver; private JButton button = new JButton("发送"); public newClient(){ p.setLayout(new BorderLayout()); JPanel p2 = new JPanel(); p2.setLayout(new BorderLayout()); p2.add(jtf1,BorderLayout.CENTER); p2.add((new JLabel("NND 你一定不要忘了曾经的承诺和坚持")),BorderLayout.SOUTH); p2.add(button,BorderLayout.EAST); p.add(p2,BorderLayout.SOUTH); p.add(jta1,BorderLayout.CENTER); this.setTitle("最简单的聊天室-----antking"); this.add(p); this.setSize(500,300); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); button.addActionListener(new buttonListener()); try { Socket client = new Socket("localhost",54321); toserver = new DataOutputStream(client.getOutputStream()); fromserver = new DataInputStream(client.getInputStream()); while(true){ String msg =fromserver.readUTF(); if(msg!=null) jta1.append(msg+"\n"); } } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } private class buttonListener implements ActionListener{ public void actionPerformed(ActionEvent e) { String str = jtf1.getText().toString(); try { toserver.writeUTF(str); toserver.flush(); } catch (UnknownHostException e1) { e1.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } } } public static void main(String[] args) { new newClient(); } }?