网络协议课程设计基于UDP的多人聊天系统源代码.docx

上传人:good****022 文档编号:116529633 上传时间:2022-07-05 格式:DOCX 页数:19 大小:354.42KB
返回 下载 相关 举报
网络协议课程设计基于UDP的多人聊天系统源代码.docx_第1页
第1页 / 共19页
网络协议课程设计基于UDP的多人聊天系统源代码.docx_第2页
第2页 / 共19页
网络协议课程设计基于UDP的多人聊天系统源代码.docx_第3页
第3页 / 共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 .*;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 (pareTo() != 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 (pareTo() != 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.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(); 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.setLocation(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 .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 Exception 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();
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 建筑环境 > 铁路桥梁


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!