下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、計(jì)算機(jī)網(wǎng)絡(luò)報(bào)告socket編程TCP實(shí)現(xiàn)簡(jiǎn)單聊天功能java網(wǎng)絡(luò)編程,通過TCP,Socket實(shí)現(xiàn)多對(duì)一的局域網(wǎng)聊天室可以實(shí)現(xiàn)多個(gè)客戶端連接服務(wù)器,服務(wù)器接收到信息就會(huì)把信息廣播到所有的客戶端服務(wù)端源碼: 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;
2、import .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 stat
3、ic 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;/R3來監(jiān)聽客戶端連接static List<Socket> clients;保存連接到服務(wù)器的客戶端public ServerUI() super("服務(wù)器端");
4、btStart = new JButton("啟動(dòng)月艮務(wù)");btSend = new JButtonC 發(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
5、 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
6、 = 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_A LWAYS);sp.setViewportVi
7、ew(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);import java.io.BufferedReader;import java.io.IOException;import java.io.PrintWriter;import .Ser
8、verSocket;import .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 Array
9、List<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ù)器失?。憾丝?228");println(e.toS
10、tring();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(Strin
11、g 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 c
12、atch block e.printStackTrace();import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import .Socket;/*這個(gè)類是服務(wù)器端的等待客戶端發(fā)送信息*/public class ListenerClient extends Thread BufferedReader reader;PrintWriter writer;ServerUI ui;Socket client;publi
13、c 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(
14、), 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.clien
15、ts.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
16、(s + "n");客戶端源碼: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.JOpti
17、onPane;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)連接&quo
18、t;);btSend = new JButtonC 發(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 A
19、ctionListener() 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)閉嗎? ", "溫馨提示 ",JOptionPan
20、e.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_A LWAYS);sp.setViewportV
21、iew(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);publicJButton btStart;publicJButton btSend;publicJTextField tfSend;publicJTextField tfIP;publi
22、cJTextField tfPost;publicJTextArea taShow;public ClientThread serverimport java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import .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 Buffe
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2026年馬鞍山安徽和州城市建設(shè)集團(tuán)有限公司公開招聘工作人員1名備考題庫(kù)及一套完整答案詳解
- 2026上半年安徽事業(yè)單位聯(lián)考合肥市肥東縣招聘51人備考考試試題及答案解析
- 2026內(nèi)蒙古鄂爾多斯市德方律師事務(wù)所招聘實(shí)習(xí)律師及助理備考題庫(kù)及一套參考答案詳解
- 2026湖北武漢東風(fēng)汽車集團(tuán)股份有限公司采購(gòu)管理部招聘5人備考考試題庫(kù)及答案解析
- 2026中央廣播電視總臺(tái)招聘?jìng)淇碱}庫(kù)有完整答案詳解
- 中國(guó)棉花協(xié)會(huì)招聘2人考試備考試題及答案解析
- 2026山東事業(yè)單位統(tǒng)考?jí)酃馐姓衅?0人備考題庫(kù)及1套參考答案詳解
- 2026上半年齊齊哈爾醫(yī)學(xué)院及直屬單位長(zhǎng)期公開招聘編制內(nèi)工作人員126人備考題庫(kù)附答案詳解
- 2026山東濟(jì)南高新區(qū)龍奧大廈附近小學(xué)招聘派遣制小學(xué)數(shù)學(xué)代課老師1人備考考試題庫(kù)及答案解析
- 2025-2030中國(guó)銅材行業(yè)前景調(diào)研及可持續(xù)發(fā)展建議研究報(bào)告
- 門診導(dǎo)診工作流程
- 2025初三英語中考英語滿分作文
- 寫字樓物業(yè)安全管理實(shí)務(wù)操作手冊(cè)
- 解析卷蘇科版八年級(jí)物理下冊(cè)《物質(zhì)的物理屬性》單元測(cè)試試題(含解析)
- 2025年及未來5年中國(guó)飲料工業(yè)行業(yè)競(jìng)爭(zhēng)格局分析及發(fā)展趨勢(shì)預(yù)測(cè)報(bào)告
- 魯迅的救國(guó)之路
- 液壓機(jī)安全操作培訓(xùn)課件
- 孕期梅毒課件
- 鋼箱梁施工安全培訓(xùn)課件
- 畢業(yè)論文寫作與答辯(第三版)課件 專題二 論文選題
- 含Al奧氏體耐熱鋼:強(qiáng)化機(jī)制剖析與高溫性能探究
評(píng)論
0/150
提交評(píng)論