日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

java网络编程,通过TCP,Socket实现多对一的局域网聊天室

發(fā)布時(shí)間:2025/3/13 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java网络编程,通过TCP,Socket实现多对一的局域网聊天室 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

java網(wǎng)絡(luò)編程,通過TCP,Socket實(shí)現(xiàn)多對(duì)一的局域網(wǎng)聊天室

可以實(shí)現(xiàn)多個(gè)客戶端連接服務(wù)器,服務(wù)器接收到信息就會(huì)把信息廣播到所有的客戶端

?

這是服務(wù)器端的代碼

View Code import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.net.Socket; import java.util.List;import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; /*這個(gè)類是服務(wù)器端的UI*/ public class ServerUI extends JFrame {public static void main(String[] args) {ServerUI serverUI = new ServerUI();}public JButton btStart;//啟動(dòng)服務(wù)器public JButton btSend;//發(fā)送信息按鈕public JTextField tfSend;//需要發(fā)送的文本信息public JTextArea taShow;//信息展示public Server server;//用來監(jiān)聽客戶端連接static List<Socket> clients;//保存連接到服務(wù)器的客戶端public ServerUI() {super("服務(wù)器端");btStart = new JButton("啟動(dòng)服務(wù)");btSend = new JButton("發(fā)送信息");tfSend = new JTextField(10);taShow = new JTextArea();btStart.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {server = new Server(ServerUI.this);}});btSend.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {server.sendMsg(tfSend.getText());tfSend.setText("");}});this.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {int a = JOptionPane.showConfirmDialog(null, "確定關(guān)閉嗎?", "溫馨提示",JOptionPane.YES_NO_OPTION);if (a == 1) {server.closeServer();System.exit(0); // 關(guān)閉 }}});JPanel top = new JPanel(new FlowLayout());top.add(tfSend);top.add(btSend);top.add(btStart);this.add(top, BorderLayout.SOUTH);final JScrollPane sp = new JScrollPane();sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);sp.setViewportView(this.taShow);this.taShow.setEditable(false);this.add(sp, BorderLayout.CENTER);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setSize(400, 300);this.setLocation(100, 200);this.setVisible(true);} } View Code import java.io.BufferedReader; import java.io.IOException; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.util.ArrayList; /*這個(gè)類是服務(wù)器端的等待客戶端連接*/ public class Server extends Thread {ServerUI ui;ServerSocket ss;BufferedReader reader;PrintWriter writer;public Server(ServerUI ui) {this.ui = ui;this.start();}public void run() {try {ss = new ServerSocket(1228);ui.clients=new ArrayList<Socket>();println("啟動(dòng)服務(wù)器成功:端口1228");while (true) {println("等待客戶端");Socket client = ss.accept();ui.clients.add(client);println("連接成功" + client.toString());new ListenerClient(ui, client);}} catch (IOException e) {println("啟動(dòng)服務(wù)器失敗:端口1228");println(e.toString());e.printStackTrace();}}public synchronized void sendMsg(String msg) {try {for (int i = 0; i < ui.clients.size(); i++) {Socket client = ui.clients.get(i);writer = new PrintWriter(client.getOutputStream(), true);writer.println(msg);}} catch (Exception e) {println(e.toString());}}public void println(String s) {if (s != null) {this.ui.taShow.setText(this.ui.taShow.getText() + s + "\n");System.out.println(s + "\n");}}public void closeServer() {try {if (ss != null)ss.close();if (reader != null)reader.close();if (writer != null)writer.close();} catch (IOException e) {// TODO Auto-generated catch block e.printStackTrace();}} } View Code import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; /*這個(gè)類是服務(wù)器端的等待客戶端發(fā)送信息*/ public class ListenerClient extends Thread {BufferedReader reader;PrintWriter writer;ServerUI ui;Socket client;public ListenerClient(ServerUI ui, Socket client) {this.ui = ui;this.client=client;this.start();}//為每一個(gè)客戶端創(chuàng)建線程等待接收信息,然后把信息廣播出去public void run() {String msg = "";while (true) {try {reader = new BufferedReader(new InputStreamReader(client.getInputStream()));writer = new PrintWriter(client.getOutputStream(), true);msg = reader.readLine();sendMsg(msg);} catch (IOException e) {println(e.toString());// e.printStackTrace();break;}if (msg != null && msg.trim() != "") {println(">>" + msg);}}}//把信息廣播到所有用戶public synchronized void sendMsg(String msg) {try {for (int i = 0; i < ui.clients.size(); i++) {Socket client = ui.clients.get(i);writer = new PrintWriter(client.getOutputStream(), true);writer.println(msg);}} catch (Exception e) {println(e.toString());}}public void println(String s) {if (s != null) {this.ui.taShow.setText(this.ui.taShow.getText() + s + "\n");System.out.println(s + "\n");}} }

?

客戶端代碼

View Code import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent;import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField;public class ClientUI extends JFrame {public static void main(String[] args) {ClientUI client = new ClientUI();}public ClientUI() {super("客戶端");btStart = new JButton("啟動(dòng)連接");btSend = new JButton("發(fā)送信息");tfSend = new JTextField(10);tfIP = new JTextField(10);tfPost = new JTextField(5);taShow = new JTextArea();btStart.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {server = new ClientThread(ClientUI.this);}});btSend.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {server.sendMsg(tfSend.getText());tfSend.setText("");}});this.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {int a = JOptionPane.showConfirmDialog(null, "確定關(guān)閉嗎?", "溫馨提示",JOptionPane.YES_NO_OPTION);if (a == 1) {System.exit(0); // 關(guān)閉 }}});JPanel top = new JPanel(new FlowLayout());top.add(tfSend);top.add(btSend);top.add(btStart);this.add(top, BorderLayout.SOUTH);final JScrollPane sp = new JScrollPane();sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);sp.setViewportView(this.taShow);this.taShow.setEditable(false);this.add(sp, BorderLayout.CENTER);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setSize(400, 300);this.setLocation(600, 200);this.setVisible(true);}public JButton btStart;public JButton btSend;public JTextField tfSend;public JTextField tfIP;public JTextField tfPost;public JTextArea taShow;public ClientThread server;} View Code import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket;public class ClientThread extends Thread {ClientUI ui;Socket client;BufferedReader reader;PrintWriter writer;public ClientThread(ClientUI ui) {this.ui = ui;try {client = new Socket("127.0.0.1", 1228);//這里設(shè)置連接服務(wù)器端的IP的端口println("連接服務(wù)器成功:端口1228");reader = new BufferedReader(new InputStreamReader(client.getInputStream()));writer = new PrintWriter(client.getOutputStream(), true);// 如果為 true,則 println、printf 或 format 方法將刷新輸出緩沖區(qū)} catch (IOException e) {println("連接服務(wù)器失敗:端口1228");println(e.toString());e.printStackTrace();}this.start();}public void run() {String msg = "";while (true) {try {msg = reader.readLine();} catch (IOException e) {println("服務(wù)器斷開連接");break;}if (msg != null && msg.trim() != "") {println(">>" + msg);}}}public void sendMsg(String msg) {try {writer.println(msg);} catch (Exception e) {println(e.toString());}}public void println(String s) {if (s != null) {this.ui.taShow.setText(this.ui.taShow.getText() + s + "\n");System.out.println(s + "\n");}}}

?

?

轉(zhuǎn)載于:https://www.cnblogs.com/taoweiji/archive/2012/12/14/2818801.html

總結(jié)

以上是生活随笔為你收集整理的java网络编程,通过TCP,Socket实现多对一的局域网聊天室的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。