资源预览内容
第1页 / 共19页
第2页 / 共19页
第3页 / 共19页
第4页 / 共19页
第5页 / 共19页
第6页 / 共19页
第7页 / 共19页
第8页 / 共19页
第9页 / 共19页
第10页 / 共19页
亲,该文档总共19页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
基于UDP的多人聊天系统只需把两个客户段代码 与一个服务器代码导入就可以了,记得要把服务器ip:192.168.1.77(也可以自己在服务器程序中改一下),每个客户端需要设置一下ip,比如A客户端:192.168.1.78,B客户端:192.168.1.88,A客户端向B客户端通讯时,“请输入对方ip”,这项改为192.168.1.88,就可以了。客户端源代码程序:ChatFram.javapackage ChatFrame_01;import java.net.*;import java.io.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.lang.*;import java.awt.color.*;import javax.swing.JOptionPane;public class ChatFrame extends JFrame JPanel contentPane;JLabel label1 = new JLabel();/ 发送消息JLabel label2 = new JLabel();/ 输入ipLabel label3 = new Label();/ 输入端口标签Label label4 = new Label();/ 输出端口标签JTextField textField1 = new JTextField();/ 发送消息JTextField textField2 = new JTextField();/ 输入ipJTextField textField3 = new JTextField();/ 获得输入端口JTextField textField4 = new JTextField();/ 获得输出端口JButton button1 = new JButton(发送);/ 发送消息TextArea textArea1 = new TextArea();JOptionPane jopane = new JOptionPane();DatagramPacket sendpacket, receivepacket;/ 声明发送和接收数据包DatagramSocket sendsocket, receivesocket;/ 声明发送和接收DatagramSocketpublic ChatFrame() enableEvents(AWTEvent.WINDOW_EVENT_MASK);try toInit(); catch (Exception e) e.printStackTrace();private void toInit() throws Exception contentPane = (JPanel) this.getContentPane();label1.setText(发送信息:);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.setBounds(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(ActionEvent 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 (str.compareTo() != 0&!.equals(str1) try textArea1.append(nto + textField2.getText()+ : + 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(););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.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(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() / 检测并接受对方发来的消息while (true) try byte array = new byte100;receivepacket = 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); catch (IOException se) se.printStackTrace();void bind() / 和对方建立连try sendsocket = new DatagramSocket(3000);receivesocket = new DatagramSocket(3333); catch (Exception e) void button1_actionPerformed(ActionEvent e) / 发送消息到对方String str = textField2.getText();String str1=textField1.getText();if (str.compareTo() != 0&!.equals(str1) try textArea1.append(nto + textField2.getText() + : + 3000+ say: + textField1.getText();String s = textField2.getText()+|+textField1.getText();byte data = s.g
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号