计算机网络编程实验(共11页).doc
精选优质文档-倾情为你奉上计算机网络编程技术实 训 报 告基于UDP协议多客户有图形用户界面的网上聊天程序 UDP-based protocol multi-client with a graphical user interface, online chat program班 级:姓 名:学 号:伊犁师范学院电子与信息工程学院一 实验目的巩固所学的Java语言基础知识,增进Java语言的编辑基本功,拓展常用类库的应用,了解Java网络程序设计的思路和方法,熟练掌握UDP协议编写程序的设计方法,掌握基本网络包的使用方法,熟练使用Socket以及多线程。二 实验内容运用Java程序编写聊天室,实现一个基于UDP协议的简单的聊天程序,支持多人聊天并且客户端具有图形化用户界面。程序实现了聊天室的基本功能,其中有:1.启动服务器。2.启动登录界面。3.进入客户端界面。4.发送信息。5.离开客户端界面。6.关闭服务器。三、实验环境操作系统:Windows平台。测试环境:Eclipse。四、实验步骤 1创建项目,组织文件 启动Eclipse ,建立项目、包及各类,键入代码,操作结果如图1所示。图1项目和文件2. 运行程序进行测试(1)首先运行Server.java,打开“聊天室服务器”。服务器IP地址为192.168.0.2如图2所示。图2服务器控制台(2) 服务器监听是否有新聊天者连接,如果没有,一直等待。如图3所示。图3 服务器等待(3)运行客户端程序client.java,打开“多人聊天室“界面,客户端1的IP地址为192.168.0.3客户端2的IP地址为192.168.0.49,如图4所示。图4聊天室客户端界面(4) 在“多人聊天室”界面输入框输入聊天信息。如图5所示。图5在输入框输入聊天信息(5)在“多人聊天室”界面发送信息。如图6所示。图6发送聊天信息(6) 服务器端显示两个客户端聊天内容,如图7所示。图7服务器端显示聊天内容(7) 退出服务器和多人聊天室。五、程序清单/多人聊天室服务器端package 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();/多人聊天室客户端package 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();/登录窗口package 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.0.19"),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.0.19"), 6666);sendsocket.send(sendpacket); catch (IOException exc) exc.printStackTrace();textField1.setText("");六、总结通过本次实验,使我们对基于UDP协议多线程聊天程序的设计有了进一步的认识,巩固了所学的Java语言基础知识,增进了Java语言的编辑基本功,拓展了常用类库的应用,了解到Java网络程序设计的思路和方法,熟练掌握了UDP协议编写程序的设计方法,掌握了基本网络包的使用方法,并学会熟练使用Socket以及多线程。运用Java程序编写聊天室,实现一个基于UDP协议的简单的聊天程序,支持多人聊天并且客户端具有图形化用户界面。程序实现了聊天室的基本功能。在操作当中,虽然我们遇到了很多问题,但是通过我们的努力,所有问题都得到了解决,并且增长了我们我们的知识,使我们在这次实验中受益颇多。这也是我们懂得在以后的实验编程中要充分发挥认真仔细,坚持不懈,实事求是的精神,是自己活得更大的进步。专心-专注-专业