socket編程TCP實(shí)現(xiàn)簡(jiǎn)單聊天功能_第1頁
socket編程TCP實(shí)現(xiàn)簡(jiǎn)單聊天功能_第2頁
socket編程TCP實(shí)現(xiàn)簡(jiǎn)單聊天功能_第3頁
socket編程TCP實(shí)現(xiàn)簡(jiǎn)單聊天功能_第4頁
socket編程TCP實(shí)現(xiàn)簡(jiǎn)單聊天功能_第5頁
免費(fèi)預(yù)覽已結(jié)束,剩余13頁可下載查看

下載本文檔

版權(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ì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論