实验五 基于UDP编程实验
来源:互联网 发布:白狐小说系统源码 编辑:程序博客网 时间:2024/05/22 11:51
实验五 基于UDP编程
实验目的:
1、理解UDP及基于数据报通信的基本原理;
2、学会基于UDP编程的代码编写及理解各语句内容;
3、掌握基于UDP编程的具体应用。
实验要求:
1、建立两个独立的基于UDP编程的Java工程,用于服务器和客户端系统;
2、建立UDP的JAVA类,并能正确运行且能实现数据报通信;
3、对JAVA类的功能进行拓广,使基于UDP编程用于某一具体的应用 。
3、调试使以上两个工程能正常运行,使服务端和客户端能正常通信。
4、对服务器端和客户端的程序代码进行阅读和理解,要求做到每条语句都能明白其具体含义,每个类都了解其功能。
5、对以上程序进行适当的修改,要求数据报通信功能保持实现。
(注:修改的方向有:图形化界面、多台电脑通信、结合线程编程等。)
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */package myinteaddress1;import java.io.*;import java.net.*;import java.awt.event.*;import java.awt.*;import javax.swing.*;/** * * @author Administrator */public class UDPChat implements Runnable,ActionListener{ JTextArea showArea; JLabel lbl1,lbl2,lbl3; JTextField msgText,sendPortText,receivePortText,IPAddressText; JFrame mainiJframe; JButton sendBtn,startBtn; JScrollPane JSPane; JPanel Pane1,Pane2; Container con; Thread thread=null; DatagramPacket sendPack,receivePack; DatagramSocket sendSocket,receiveSocket; private InetAddress sendIP; private int sendPort,receivePort; private byte intBuf[],outBuf[]; public static final int BUFSIZE=1024; public UDPChat(){ mainiJframe=new JFrame("聊天---UDP协议"); con=mainiJframe.getContentPane(); showArea=new JTextArea(); showArea.setEditable(false); showArea.setLineWrap(true); lbl1=new JLabel("接受端口号"); lbl2=new JLabel("发送端口号"); lbl3=new JLabel("对方的地址"); sendPortText=new JTextField(); sendPortText.setColumns(5); receivePortText=new JTextField(); receivePortText.setColumns(5); IPAddressText=new JTextField(); IPAddressText.setColumns(8); startBtn=new JButton("开始"); startBtn.addActionListener(this); Pane1=new JPanel(); Pane1.setLayout(new FlowLayout()); Pane1.add(lbl1); Pane1.add(receivePortText); Pane1.add(lbl2); Pane1.add(sendPortText); Pane1.add(lbl3); Pane1.add(IPAddressText); Pane1.add(startBtn); JSPane=new JScrollPane(showArea); msgText=new JTextField(); msgText.setColumns(40); msgText.setEditable(false); msgText.addActionListener(this); sendBtn=new JButton("发送"); sendBtn.setEnabled(false); sendBtn.addActionListener(this); Pane2=new JPanel(); Pane2.add(msgText); Pane2.add(sendBtn); con.add(Pane1,BorderLayout.NORTH); con.add(JSPane,BorderLayout.CENTER); con.add(Pane2,BorderLayout.SOUTH); mainiJframe.setSize(600,400); mainiJframe.setVisible(true); mainiJframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main (String[] args){ new UDPChat(); } public void actionPerformed(ActionEvent e) { try{ if(e.getSource()==startBtn) { intBuf=new byte[BUFSIZE]; sendPort=Integer.parseInt(sendPortText.getText()); sendIP=InetAddress.getByName(IPAddressText.getText()); sendSocket=new DatagramSocket(); receivePort=Integer.parseInt(receivePortText.getText()); receivePack=new DatagramPacket(intBuf,BUFSIZE); receiveSocket=new DatagramSocket(receivePort); thread=new Thread(this); thread.setPriority(Thread.MIN_PRIORITY); thread.start(); startBtn.setEnabled(false); sendBtn.setEnabled(true); msgText.setEditable(true); } else{ outBuf=msgText.getText().getBytes(); sendPack=new DatagramPacket(outBuf,outBuf.length,sendIP,sendPort); sendSocket.send(sendPack); showArea.append("我说:"+msgText.getText()+"\n"); msgText.setText(null); } }catch(UnknownHostException el){ showArea.append("无法连接到指定的地址\n"); }catch(SocketException el){ showArea.append("无法打开指定端口\n"); }catch(IOException el){ showArea.append("发送数据失败\n"); } } public void run(){ String msgstr; while(true){ try{ receiveSocket.receive(receivePack); msgstr=new String(receivePack.getData(),0,receivePack.getLength()); showArea.append("对方说:"+msgstr+"\n"); }catch(IOException el){ showArea.append("接受数据出错\n"); } } }}
0 0
- 实验五 基于UDP编程实验
- 实验五 基于UDP编程实验
- 实验五 基于UDP编程实验
- UDP编程实验
- socket编程实验-UDP文件传输
- Socket编程 ——UDP 实验报告
- 【网络编程】UDP数据传输及文件传输实验
- 实验五:实验报告
- 实验五
- 实验五
- 实验五
- 实验五
- 实验五
- 实验五
- 实验五
- 实验五
- 实验五
- 实验五
- 计算几何--bnu51638
- 树莓派2——arm-linux交叉编译环境的建立
- 第九周项目二 深复制函数(3)
- Android网络之数据解析----SAX方式解析XML数据
- Android退出应用最优雅的方式
- 实验五 基于UDP编程实验
- shader forge初级篇|减法与除法
- [转] ubuntu下解压缩文件 .tar.gz;.tar;.bz2;.tar.bz2;.bz;.tar.bz;.zip
- hdu5670Machine
- 编程之美之找到符合条件的数
- SQL语句中的group by 和 order by
- 斯特林数
- arm的一些常用概念
- 周易六十四卦——贲卦