《網(wǎng)絡協(xié)議課程設計基于UDP的多人聊天系統(tǒng)源代碼.docx》由會員分享,可在線閱讀,更多相關《網(wǎng)絡協(xié)議課程設計基于UDP的多人聊天系統(tǒng)源代碼.docx(19頁珍藏版)》請在裝配圖網(wǎng)上搜索。
1、 基于UDP的多人聊天系統(tǒng)只需把兩個客戶段代碼 與一個服務器代碼導入就可以了,記得要把服務器ip:192.168.1.77(也可以自己在服務器程序中改一下),每個客戶端需要設置一下ip,比如A客戶端:192.168.1.78,B客戶端:192.168.1.88,A客戶端向B客戶端通訊時,“請輸入對方ip”,這項改為192.168.1.88,就可以了??蛻舳嗽创a程序: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();/ 輸入端口標簽Label label4 = new Label();/ 輸出端口標簽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(輸入對方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() / 檢測并接受對方發(fā)來的消息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() / 和對方建立連try sendsocket = new DatagramSocket(3000);receivesocket = new DatagramSocket(3333); catch (Exception e) void button1_actionPerformed(ActionEvent e) / 發(fā)送消息到對方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();服務器端: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();