Java網(wǎng)絡(luò)編程實(shí)現(xiàn)多線(xiàn)程聊天
本文實(shí)例為大家分享了Java網(wǎng)絡(luò)編程實(shí)現(xiàn)多線(xiàn)程聊天的具體代碼,供大家參考,具體內(nèi)容如下
聊天程序如果是單線(xiàn)程,會(huì)導(dǎo)致沒(méi)人只能說(shuō)一句,并且說(shuō)了以后,必須等到另一個(gè)人的回復(fù),才能說(shuō)第二句。收發(fā)都在主線(xiàn)程中,不能同時(shí)進(jìn)行。
解決方法:
將收發(fā)放到兩個(gè)不同的線(xiàn)程
1. SendThread 發(fā)送消息線(xiàn)程2. RecieveThread 接受消息線(xiàn)程3. Server一旦接受到連接,就啟動(dòng)收發(fā)兩個(gè)線(xiàn)程4. Client 一旦建立了連接,就啟動(dòng)收發(fā)兩個(gè)線(xiàn)程
多線(xiàn)程聊天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('監(jiān)聽(tīng)端口號(hào):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();} }}簡(jiǎn)單對(duì)話(huà)框
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+'rn'); } catch (Exception e) {e.printStackTrace(); }} }}.start();b.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) {String text = tf.getText();ta.append(text+'rn');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+'rn'); } catch (Exception e) {e.printStackTrace(); }} }}.start();b.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) {String text = tf.getText();ta.append(text+'rn');try { DataOutputStream dos = new DataOutputStream( s.getOutputStream()); dos.writeUTF(text);} catch (Exception ex) { ex.printStackTrace();} }}); }}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. ASP基礎(chǔ)知識(shí)VBScript基本元素講解2. Python requests庫(kù)參數(shù)提交的注意事項(xiàng)總結(jié)3. ajax請(qǐng)求添加自定義header參數(shù)代碼4. IntelliJ IDEA導(dǎo)入jar包的方法5. Gitlab CI-CD自動(dòng)化部署SpringBoot項(xiàng)目的方法步驟6. Kotlin + Flow 實(shí)現(xiàn)Android 應(yīng)用初始化任務(wù)啟動(dòng)庫(kù)7. 詳談ajax返回?cái)?shù)據(jù)成功 卻進(jìn)入error的方法8. python操作mysql、excel、pdf的示例9. vue-electron中修改表格內(nèi)容并修改樣式10. python爬蟲(chóng)學(xué)習(xí)筆記之pyquery模塊基本用法詳解
