Java網路編程筆記6,java網路編程筆記
在前幾遍文章中,我們已經知道了用戶端與伺服器通訊,包括多個用戶端與伺服器的通訊,接下來我們示範下用戶端與伺服器的完整通訊過程。
先看
用戶端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();}@Overridepublic 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();}}@Overridepublic 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 情緒控_