您好,登錄后才能下訂單哦!
本文小編為大家詳細介紹“Java網絡編程如何實現多線程聊天”,內容詳細,步驟清晰,細節處理妥當,希望這篇“Java網絡編程如何實現多線程聊天”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學習新知識吧。
解決方法:
將收發放到兩個不同的線程
1. SendThread 發送消息線程
2. RecieveThread 接受消息線程
3. Server一旦接受到連接,就啟動收發兩個線程
4. Client 一旦建立了連接,就啟動收發兩個線程
1 SendThread
package socket; import java.io.DataOutputStream; import java.io.IOException; import java.io.OutputStream; import java.net.Socket; import java.util.Scanner; public class SendThread extends Thread { private Socket s; public SendThread(Socket s) { this.s = s; } @Override public void run() { try { OutputStream os = s.getOutputStream(); DataOutputStream dos = new DataOutputStream(os); while(true){ Scanner sc = new Scanner(System.in); String str = sc.next(); dos.writeUTF(str); } } catch (IOException e) { e.printStackTrace(); } } }
2 RecieveThread
package socket; import java.io.DataInputStream; import java.io.IOException; import java.io.InputStream; import java.net.Socket; public class RecieveThread extends Thread { private Socket s; public RecieveThread(Socket s) { this.s = s; } @Override public void run() { InputStream is = null; try { is = s.getInputStream(); DataInputStream dis = new DataInputStream(is); while(true){ String msg = dis.readUTF(); System.out.println(msg); } } catch (IOException e) { e.printStackTrace(); } } }
3 Server
package socket; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; public class Server { public static void main(String[] args) { try { ServerSocket ss = new ServerSocket(8888); System.out.println("監聽端口號:8888"); Socket s = ss.accept(); new SendThread(s).start(); new RecieveThread(s).start(); } catch (IOException e) { e.printStackTrace(); } } }
4 Client
package socket; import java.io.IOException; import java.net.Socket; import java.net.UnknownHostException; public class Client { public static void main(String[] args) { try { Socket s = new Socket("127.0.0.1", 8888); new SendThread(s).start(); new RecieveThread(s).start(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
Server
package socket; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.DataInputStream; import java.io.DataOutputStream; import java.net.ServerSocket; import java.net.Socket; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JTextArea; import javax.swing.JTextField; public class GUIServer { public static void main(String[] args) throws Exception { JFrame f = new JFrame(); f.setTitle("server"); f.setSize(400, 300); f.setLocation(100, 200); f.setLayout(null); JButton b = new JButton("send"); b.setBounds(10, 10, 80, 30); f.add(b); final JTextField tf = new JTextField(); tf.setBounds(10, 110, 80, 30); f.add(tf); final JTextArea ta = new JTextArea(); ta.setBounds(110,10, 200, 300); f.add(ta); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); ServerSocket ss = new ServerSocket(8888); System.out.println("listenning on port:8888"); final Socket s = ss.accept(); new Thread() { public void run() { while (true) { try { DataInputStream dis = new DataInputStream( s.getInputStream()); String text = dis.readUTF(); ta.append(text+"\r\n"); } catch (Exception e) { e.printStackTrace(); } } } }.start(); b.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String text = tf.getText(); ta.append(text+"\r\n"); try { DataOutputStream dos = new DataOutputStream( s.getOutputStream()); dos.writeUTF(text); } catch (Exception ex) { ex.printStackTrace(); } } }); } }
Client
package socket; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.DataInputStream; import java.io.DataOutputStream; import java.net.Socket; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JTextArea; import javax.swing.JTextField; public class GUIClient { public static void main(String[] args) throws Exception { JFrame f = new JFrame(); f.setTitle("client"); f.setSize(400, 300); f.setLocation(600, 200); f.setLayout(null); JButton b = new JButton("send"); b.setBounds(10, 10, 80, 30); f.add(b); final JTextField tf = new JTextField(); tf.setBounds(10, 110, 80, 30); f.add(tf); final JTextArea ta = new JTextArea(); ta.setBounds(110,10, 200, 300); f.add(ta); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); final Socket s = new Socket("127.0.0.1", 8888); new Thread() { public void run() { while (true) { try { DataInputStream dis = new DataInputStream( s.getInputStream()); String text = dis.readUTF(); ta.append(text+"\r\n"); } catch (Exception e) { e.printStackTrace(); } } } }.start(); b.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String text = tf.getText(); ta.append(text+"\r\n"); try { DataOutputStream dos = new DataOutputStream( s.getOutputStream()); dos.writeUTF(text); } catch (Exception ex) { ex.printStackTrace(); } } }); } }
讀到這里,這篇“Java網絡編程如何實現多線程聊天”文章已經介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領會,如果想了解更多相關內容的文章,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。