網(wǎng)絡(luò)協(xié)議課程設(shè)計(jì)基于UDP的多人聊天系統(tǒng)源代碼.docx

上傳人:good****022 文檔編號(hào):116529633 上傳時(shí)間:2022-07-05 格式:DOCX 頁(yè)數(shù):19 大小:354.42KB
收藏 版權(quán)申訴 舉報(bào) 下載
網(wǎng)絡(luò)協(xié)議課程設(shè)計(jì)基于UDP的多人聊天系統(tǒng)源代碼.docx_第1頁(yè)
第1頁(yè) / 共19頁(yè)
網(wǎng)絡(luò)協(xié)議課程設(shè)計(jì)基于UDP的多人聊天系統(tǒng)源代碼.docx_第2頁(yè)
第2頁(yè) / 共19頁(yè)
網(wǎng)絡(luò)協(xié)議課程設(shè)計(jì)基于UDP的多人聊天系統(tǒng)源代碼.docx_第3頁(yè)
第3頁(yè) / 共19頁(yè)

下載文檔到電腦,查找使用更方便

15 積分

下載資源

還剩頁(yè)未讀,繼續(xù)閱讀

資源描述:

《網(wǎng)絡(luò)協(xié)議課程設(shè)計(jì)基于UDP的多人聊天系統(tǒng)源代碼.docx》由會(huì)員分享,可在線閱讀,更多相關(guān)《網(wǎng)絡(luò)協(xié)議課程設(shè)計(jì)基于UDP的多人聊天系統(tǒng)源代碼.docx(19頁(yè)珍藏版)》請(qǐng)?jiān)谘b配圖網(wǎng)上搜索。

1、 基于UDP的多人聊天系統(tǒng)只需把兩個(gè)客戶(hù)段代碼 與一個(gè)服務(wù)器代碼導(dǎo)入就可以了,記得要把服務(wù)器ip:192.168.1.77(也可以自己在服務(wù)器程序中改一下),每個(gè)客戶(hù)端需要設(shè)置一下ip,比如A客戶(hù)端:192.168.1.78,B客戶(hù)端:192.168.1.88,A客戶(hù)端向B客戶(hù)端通訊時(shí),“請(qǐng)輸入對(duì)方ip”,這項(xiàng)改為192.168.1.88,就可以了??蛻?hù)端源代碼程序:ChatFram.javapackage ChatFrame_01;import .*;import java.io.*;import java.awt.*;import java.awt.event.*;import javax

2、.swing.*;import java.lang.*;import java.awt.color.*;import javax.swing.JOptionPane;public class ChatFrame extends JFrame JPanel contentPane;JLabel label1 = new JLabel();/ 發(fā)送消息JLabel label2 = new JLabel();/ 輸入ipLabel label3 = new Label();/ 輸入端口標(biāo)簽Label label4 = new Label();/ 輸出端口標(biāo)簽JTextField textField

3、1 = new JTextField();/ 發(fā)送消息JTextField textField2 = new JTextField();/ 輸入ipJTextField textField3 = new JTextField();/ 獲得輸入端口JTextField textField4 = new JTextField();/ 獲得輸出端口JButton button1 = new JButton(發(fā)送);/ 發(fā)送消息TextArea textArea1 = new TextArea();JOptionPane jopane = new JOptionPane();DatagramPacke

4、t sendpacket, receivepacket;/ 聲明發(fā)送和接收數(shù)據(jù)包DatagramSocket sendsocket, receivesocket;/ 聲明發(fā)送和接收DatagramSocketpublic ChatFrame() enableEvents(AWTEvent.WINDOW_EVENT_MASK);try toInit(); catch (Exception e) e.printStackTrace();private void toInit() throws Exception contentPane = (JPanel) this.getContentPane(

5、);label1.setText(發(fā)送信息:);label1.setBounds(new Rectangle(13, 100, 60, 21);contentPane.setLayout(null);contentPane.setBackground(Color.pink);this.setSize(new Dimension(363, 502);this.setTitle(多人聊天室);label2.setText(輸入對(duì)方IP地址:);label2.setBounds(new Rectangle(12, 19, 109, 23);label3.setText(輸入端口:);label3.s

6、etBounds(new Rectangle(12, 58, 55, 22);label4.setText(輸出端口:);label4.setBounds(new Rectangle(180, 58, 55, 22);textField3.setText(3000);textField4.setText(3001);button1.setBounds(new Rectangle(280, 100, 59, 21);button1.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(A

7、ctionEvent e) button1_actionPerformed(e););textField1.addKeyListener(new KeyAdapter() public void keyPressed(KeyEvent e) if (e.getKeyCode() = KeyEvent.VK_ENTER) String str = textField2.getText();String str1=textField1.getText();if (pareTo() != 0&!.equals(str1) try textArea1.append(nto + textField2.g

8、etText()+ : + 3000 + say: + textField1.getText();String s = textField1.getText();byte data = s.getBytes(UTF-8);sendpacket = new DatagramPacket(data,data.length,InetAddress.getByName(192.168.1.77),6666);sendsocket.send(sendpacket); catch (IOException exc) exc.printStackTrace();textField1.setText(););

9、textField4.setBounds(new Rectangle(240, 58, 90, 22);textField3.setBounds(new Rectangle(70, 58, 90, 22);textField2.setBounds(new Rectangle(123, 20, 120, 19);textField1.setBounds(new Rectangle(76, 100, 190, 19);textArea1.setBounds(new Rectangle(11, 140, 333, 315);textArea1.setEditable(false);textArea1

10、.setBackground(Color.white);contentPane.add(label4, null);contentPane.add(label3, null);contentPane.add(label2, null);contentPane.add(label1, null);contentPane.add(textField1, null);contentPane.add(textField2, null);contentPane.add(textField3, null);contentPane.add(textField4, null);contentPane.add(

11、textArea1, null);contentPane.add(button1, null);this.setVisible(true);bind();protected void processWindowEvent(WindowEvent e) super.processWindowEvent(e);if (e.getID() = WindowEvent.WINDOW_CLOSING) System.exit(0);public void waitforpackets() / 檢測(cè)并接受對(duì)方發(fā)來(lái)的消息while (true) try byte array = new byte100;re

12、ceivepacket = new DatagramPacket(array, array.length);receivesocket.receive(receivepacket);/ 接收textArea1.append(nfrom + receivepacket.getAddress() + :+ receivepacket.getPort() + get:);String received = new String(receivepacket.getData(), 0,receivepacket.getLength(), UTF-8);textArea1.append(received)

13、; catch (IOException se) se.printStackTrace();void bind() / 和對(duì)方建立連try sendsocket = new DatagramSocket(3000);receivesocket = new DatagramSocket(3333); catch (Exception e) void button1_actionPerformed(ActionEvent e) / 發(fā)送消息到對(duì)方String str = textField2.getText();String str1=textField1.getText();if (pareTo

14、() != 0&!.equals(str1) try textArea1.append(nto + textField2.getText() + : + 3000+ say: + textField1.getText();String s = textField2.getText()+|+textField1.getText();byte data = s.getBytes(UTF-8);sendpacket = new DatagramPacket(data, data.length, InetAddress.getByName(192.168.1.77), 6666);sendsocket

15、.send(sendpacket); catch (IOException exc) exc.printStackTrace();textField1.setText();ChatApp.javapackage ChatFrame_01;import java.io.*;import javax.swing.UIManager;import java.awt.*;public class ChatApp boolean packFrame = false;ChatApp() ChatFrame frame = new ChatFrame();if (packFrame) frame.pack(

16、); else frame.validate();/ 放置窗體在屏幕中央Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();Dimension frameSize = frame.getSize();if (frameSize.height screenSize.height) frameSize.height = screenSize.height;if (frameSize.width screenSize.width) frameSize.width = screenSize.width;frame.set

17、Location(screenSize.width - frameSize.width) / 2,(screenSize.height - frameSize.height) / 2);frame.setVisible(true);frame.waitforpackets();public static void main(String args) new ChatApp();服務(wù)器端:Server.javapackage ChatFrame_01;import .DatagramPacket;import .DatagramSocket;import .InetAddress;import

18、.Socket;import java.util.*;public class Server List ips = new ArrayList();DatagramSocket socket;DatagramPacket packet;DatagramSocket ssocket;DatagramPacket spacket;public Server() throws Exception socket = new DatagramSocket(6666);ssocket=new DatagramSocket(8888);public void listen() throws Exceptio

19、n byte array = new byte100;packet = new DatagramPacket(array, array.length);socket.receive(packet);byte data=packet.getData();String received = new String(data,0,data.length,UTF-8);System.out.println(received);String ss = received.split(|);String ip = ss0;byte dt=ss1.getBytes(UTF-8);spacket=new DatagramPacket(dt,dt.length,InetAddress.getByName(ip),3333);ssocket.send(spacket);public static void main(String args) throws Exception Server s = new Server();while (true) s.listen();

展開(kāi)閱讀全文
溫馨提示:
1: 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
2: 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
3.本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
5. 裝配圖網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

相關(guān)資源

更多
正為您匹配相似的精品文檔
關(guān)于我們 - 網(wǎng)站聲明 - 網(wǎng)站地圖 - 資源地圖 - 友情鏈接 - 網(wǎng)站客服 - 聯(lián)系我們

copyright@ 2023-2025  zhuangpeitu.com 裝配圖網(wǎng)版權(quán)所有   聯(lián)系電話:18123376007

備案號(hào):ICP2024067431號(hào)-1 川公網(wǎng)安備51140202000466號(hào)


本站為文檔C2C交易模式,即用戶(hù)上傳的文檔直接被用戶(hù)下載,本站只是中間服務(wù)平臺(tái),本站所有文檔下載所得的收益歸上傳人(含作者)所有。裝配圖網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)上載內(nèi)容本身不做任何修改或編輯。若文檔所含內(nèi)容侵犯了您的版權(quán)或隱私,請(qǐng)立即通知裝配圖網(wǎng),我們立即給予刪除!