在前几遍文章中,我们已经知道了客户端与服务器通信,包括多个客户端与服务器的通信,接下来我们演示下客户端与服务器的完整通信过程。
先看效果图
客户端1
客户端2
服务器端
服务器线程类:
<span style="font-size:18px;">public class Connection extends Thread { private JTextArea txt; private Socket st; private String msg = null; private BufferedReader br = null; private PrintStream ps; public Connection(Socket st, JTextArea txt) { this.st = st; this.txt = txt; start(); } @Override public void run() { try { br = new BufferedReader(new InputStreamReader(new DataInputStream( st.getInputStream()))); ps=new PrintStream(new DataOutputStream(st.getOutputStream())); while(true){ msg=br.readLine(); txt.append("从客户端收到信息 "+msg+'\n'); Server.send(msg); } } catch (Exception e) { e.printStackTrace(); } } public void send(String msg){ ps.println(msg); } } </span>
<span style="font-size:18px;">public class Server extends JFrame { private JTextArea txt; private ServerSocket ss; private static List<Connection> conns=new ArrayList<Connection>(); public Server(){ txt=new JTextArea(); this.setTitle("服务器"); this.setLayout(new BorderLayout()); this.add(new JScrollPane(txt),BorderLayout.CENTER); this.setSize(500,300); this.setVisible(true); run(); } private void run(){ try { ss=new ServerSocket(3000); txt.append("接受时间:"+new Date()+"\n"); while(true){ Socket st=ss.accept(); conns.add(new Connection(st, txt)); } } catch (Exception e) { e.printStackTrace(); } } public static void send(String msg){ for(Connection c:conns){ c.send(msg); } } }</span>
<span style="font-size:18px;">public class ServerMain { public static void main(String[] args) { Server server=new Server(); } }</span>
客户端:
<span style="font-size:18px;">public class Client extends JFrame implements ActionListener { private JTextArea txta; private JTextField txtf; private JPanel pl; private JButton bt; private BufferedReader br; private DataOutputStream out; private PrintStream ps; private Container f = this.getContentPane(); public Client() { f.setLayout(new BorderLayout()); txta = new JTextArea(); f.add(txta, BorderLayout.CENTER); txtf = new JTextField(20); bt = new JButton("发送"); pl = new JPanel(); pl.setLayout(new FlowLayout()); pl.add(txtf); pl.add(bt); bt.addActionListener(this); f.add(pl, BorderLayout.SOUTH); setTitle("客户端"); setSize(500, 300); setVisible(true); run(); new Thread() { { start(); } public void run() { while (true) { try { txta.append("收到消息 :" + br.readLine() + "\n"); } catch (Exception e) { e.printStackTrace(); } } } }; } public void run() { try { Socket socket=new Socket("192.168.12.112", 3000); out=new DataOutputStream(socket.getOutputStream()); ps=new PrintStream(out); br=new BufferedReader(new InputStreamReader(socket.getInputStream())); } catch (Exception e) { e.printStackTrace(); } } @Override public void actionPerformed(ActionEvent e) { if(e.getSource()==bt){ String msg=txtf.getText(); try { ps.println(msg); txta.append("已经发送消息:"+msg+"\n"); } catch (Exception e2) { e2.printStackTrace(); } } } }</span>
<span style="font-size:18px;">public class ClientMain { public static void main(String[] args) { Client client=new Client(); } }</span>
转载请注明出处:http://blog.csdn.net/hai_qing_xu_kong/article/details/42804309 情绪控_
原文:http://blog.csdn.net/hai_qing_xu_kong/article/details/42804309