資源描述:
《網(wǎng)絡(luò)協(xié)議課程設(shè)計(jì)基于udp的多人聊天系統(tǒng)源代碼》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在學(xué)術(shù)論文-天天文庫(kù)。
1、基于UDP的多人聊天系統(tǒng)只需把兩個(gè)客戶段代碼與一個(gè)服務(wù)器代碼導(dǎo)入就可以了,記得要把服務(wù)器ip:192.168.1.77(也可以自己在服務(wù)器程序中改一下),每個(gè)客戶端需要設(shè)置一下ip,比如A客戶端:192.168.1.78,B客戶端:192.168.1.88,A客戶端向B客戶端通訊時(shí),“請(qǐng)輸入對(duì)方ip”,這項(xiàng)改為192.168.1.88,就可以了。客戶端源代碼程序:ChatFram.javapackageChatFrame_01;importjava.net.*;importjava.io.*;importjava.awt.*;importjava.awt.event.*;
2、importjavax.swing.*;importjava.lang.*;importjava.awt.color.*;importjavax.swing.JOptionPane;publicclassChatFrameextendsJFrame{JPanelcontentPane;JLabellabel1=newJLabel();//發(fā)送消息JLabellabel2=newJLabel();//輸入ipLabellabel3=newLabel();//輸入端口標(biāo)簽Labellabel4=newLabel();//輸出端口標(biāo)簽JTextFieldtextField1=n
3、ewJTextField();//發(fā)送消息JTextFieldtextField2=newJTextField();//輸入ipJTextFieldtextField3=newJTextField();//獲得輸入端口JTextFieldtextField4=newJTextField();//獲得輸出端口JButtonbutton1=newJButton("發(fā)送");//發(fā)送消息TextAreatextArea1=newTextArea();JOptionPanejopane=newJOptionPane();DatagramPacketsendpacket,recei
4、vepacket;//聲明發(fā)送和接收數(shù)據(jù)包DatagramSocketsendsocket,receivesocket;//聲明發(fā)送和接收DatagramSocketpublicChatFrame(){enableEvents(AWTEvent.WINDOW_EVENT_MASK);try{toInit();}catch(Exceptione){e.printStackTrace();}}privatevoidtoInit()throwsException{contentPane=(JPanel)this.getContentPane();label1.setText("
5、發(fā)送信息:");label1.setBounds(newRectangle(13,100,60,21));contentPane.setLayout(null);contentPane.setBackground(Color.pink);this.setSize(newDimension(363,502));this.setTitle("多人聊天室");label2.setText("輸入對(duì)方IP地址:");label2.setBounds(newRectangle(12,19,109,23));label3.setText("輸入端口:");label3.setBoun
6、ds(newRectangle(12,58,55,22));label4.setText("輸出端口:");label4.setBounds(newRectangle(180,58,55,22));textField3.setText("3000");textField4.setText("3001");button1.setBounds(newRectangle(280,100,59,21));button1.addActionListener(newjava.awt.event.ActionListener(){publicvoidactionPerformed(Ac
7、tionEvente){button1_actionPerformed(e);}});textField1.addKeyListener(newKeyAdapter(){publicvoidkeyPressed(KeyEvente){if(e.getKeyCode()==KeyEvent.VK_ENTER){Stringstr=textField2.getText();Stringstr1=textField1.getText();if(str.compareTo("")!=0&&!"".equals(str1)){try{t