J2EE点到点文件传输程序_第1页
J2EE点到点文件传输程序_第2页
J2EE点到点文件传输程序_第3页
J2EE点到点文件传输程序_第4页
J2EE点到点文件传输程序_第5页
已阅读5页,还剩82页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、 J2EE课程设计报告 设计内容:点到点文件传输 姓名:陈佳宇 学号:20112730 班级:软件二班 西南交通大学2013-2014第二学期 设计报告一、课程设计目的 1.掌握基于TCP的网络程序设计。 2.掌握基于UDP的网络程序设计。 3.编写远程点到点文件传输程序。2、 运行环境 1.系统环境:win7,windows XP 2.编程环境:java JDK,Eclipse,cmd控制台命令3、 设计内容及步骤 1.编写基于TCP协议的网络程序,实现两台计算机之间互相发送文件。Socket通信的一般过程:创建Socket对象关闭Socket创建输入输出创建Socket对象是否创建Serv

2、ersocket等待客户端连接请求 失败抛出异常成功关闭Socket创建输入输出 客户端创建Server端ServerSocket过程代码:public class Server extends Thread public Server() try jbInit(); catch (Exception ex) ex.printStackTrace(); public int port=5000; public int maxThreadNum=5; private ServerSocket checkServer = null; private SocketThread socketThrea

3、d = null; private Socket aSocket = null; private List socketPool; /socket连接池 private boolean serverRunning = false; /服务器是否运行 /构造函数 public Server(List socketPool) this.socketPool = socketPool; public void run() serverRunning = true; try checkServer = new ServerSocket(port); checkServer.setSoTimeout(6

4、0000); catch (IOException ex) ex.printStackTrace(); while (serverRunning) try System.out.println("Started."); aSocket = checkServer.accept(); System.out.println("client connected"); socketThread = new SocketThread(aSocket); socketThread.start(); if(socketPool.size()>=maxThread

5、Num) socketThread.stopThread(); else socketPool.add(socketThread); catch (Exception ex) ex.printStackTrace(); try Thread.sleep(1000); catch (InterruptedException ex) ex.printStackTrace(); try checkServer.close(); catch (IOException ex1) public void stopThread() serverRunning=false; private void jbIn

6、it() throws Exception 2. 编写程序界面代码1) 编写主界面MainFrame 主界面由一个JPanel控件和一个JTabbedPane控件组成。 JPanel控件放在上面,用于存放操作控件,发送文件按钮,设置按钮,以及本机IP显示控件。 JTabbedPane放在下面,用于存放接受控件,存放在里面的控件都是不可见的。当发送主机向目的主机发送文件时,里面控件可见,可以看到发送过来的消息,以及操作按钮。2) 编写发送文件子界面TransFileDialog3) 编写设置子界面SettingDialog4) 编写接受信息界面TransFileManager和TransFile

7、PanelTransFileManager核心代码:public void run() running = true; SocketThread st; TransFilePanel tfp; int threadListSize; int paneListSize; while (running) threadListSize = threadList.size(); paneListSize = paneList.size(); for (int i = 0; i < threadListSize; i+) st = (SocketThread) threadList.get(i);

8、 if (paneListSize <= i) try Thread.sleep(100); catch (InterruptedException ex2) tfp = new TransFilePanel(st.isFileSender(); jtp.addTab(st.getFileName(), tfp); paneList.add(tfp); tfp.jtaFileTransStatus.setText("文件发送 目标IP:"+st.getIP()+" 端口:"+st.getPort()+"nn正在与对方建立连接."

9、); else tfp = (TransFilePanel) paneList.get(i); tfp.setStatus(st.getStatus(); if (tfp.isCanneled) st.cancelTrans(); switch (st.getStatus() case SocketThread.FILE_TRANS_STATUS_WAITFORCONFIRM: if (st.isFileSender() tfp.jtaFileTransStatus.setText(st.getFileTransMessage()+"nn连接成功! 等待对方回应."); e

10、lse tfp.jtaFileTransStatus.setText(st.getFileName() + " (" + st.fileSize + " B) 是否接收?n"+"来自:"+st.getIP()+"n简短附言:"+st.getMessage(); if (tfp.isConfirm) if(tfp.fileName!=null) File aFile = new File(tfp.fileName); long off=0,size; if (aFile.exists() File temFile=n

11、ew File(aFile.getAbsolutePath()+".tmp"); if(temFile!=null) try DataInputStream dis=new DataInputStream(new FileInputStream(temFile); off = dis.readLong(); System.out.println(off); size=dis.readLong(); if(off!=new File(tfp.fileName).length()|size!=st.fileSize) off=0; catch (IOException ex1)

12、 off=0; st.confirmReceiveFile(true,tfp.fileName,off); else st.confirmReceiveFile(false,"",0); break; case SocketThread.FILE_TRANS_STATUS_CONTEXT: double progress = st.getTransFileLength() * 1.0 / st.fileSize * 100; double transSpeed = (st.getTransFileLength() - tfp.transFileLength) / 1024

13、* 2; tfp.transFileLength = st.getTransFileLength(); tfp.jpgbFileTrans.setValue(int) progress); tfp.jtaFileTransStatus.setText(st.getFileTransMessage()+"nn"); tfp.jtaFileTransStatus.setText(tfp.jtaFileTransStatus.getText()+"传输速度:" + (int) transSpeed + "k/s"+" 已完成:&q

14、uot; + (int) progress + "%"); break; case SocketThread.FILE_TRANS_STATUS_SUCCESS: tfp.jtaFileTransStatus.setText(st.getFileTransMessage()+"nn传输成功!"); if(!st.isFileSender() new File(tfp.fileName+".tmp").delete(); if (tfp.isClosed) threadList.remove(i); paneList.remove(i)

15、; jtp.remove(i); i-; threadListSize-; paneListSize-; break; case SocketThread.FILE_TRANS_STATUS_FAIL: tfp.jtaFileTransStatus.setText(st.getFileTransMessage()+"nn传输失败!" + st.getErrorMessage(); if (tfp.isClosed) threadList.remove(i); paneList.remove(i); jtp.remove(i); i-; threadListSize-; pa

16、neListSize-; break; try Thread.sleep(500); catch (InterruptedException ex) 3. 编写服务器之间连接SocketThread代码主要功能实现代码如下:/接收者构造函数 public SocketThread(Socket aSocket) this.aSocket = aSocket; try aSocket.setSoTimeout(300000); catch (SocketException ex) byte address = aSocket.getInetAddress().getAddress(); IP =

17、 (address0 & 0xff) + "." + (address1 & 0xff) + "." + (address2 & 0xff) + "." + (address3 & 0xff); try dis = new DataInputStream(aSocket.getInputStream(); dos = new DataOutputStream(aSocket.getOutputStream(); fileTransStatus = FILE_TRANS_STATUS_FILENAME;

18、catch (IOException ex) setError("创建连接错误!"); try aSocket.setReceiveBufferSize(CACHE_SIZE*2); catch (SocketException ex1) ex1.printStackTrace(); dataBuf=new byteCACHE_SIZE+100;/发送者构造函数 public SocketThread(String serverName, int portNo, String fileName, String message) aFile = new File(fileNa

19、me); this.fileName = aFile.getName(); this.fileSize = fileSize; fileSender = true; if (message != null) this.message = message; this.IP = serverName; this.port = portNo; dataBuf=new byteCACHE_SIZE; /线程执行函数 public void run() running = true; if (fileSender) try aSocket = new Socket(IP, port); aSocket.

20、setSoTimeout(300000); aSocket.setSendBufferSize(CACHE_SIZE*2); dos = new DataOutputStream(aSocket.getOutputStream(); dis = new DataInputStream(aSocket.getInputStream(); fDis = new DataInputStream(new FileInputStream(aFile); fileTransStatus = FILE_TRANS_STATUS_FILENAME; catch (UnknownHostException ex

21、1) ex1.printStackTrace(); setError("连接服务器错误!"); catch (IOException ex1) ex1.printStackTrace(); setError("创建连接错误!"); while (running) if (fileSender) sendFile(); else receiveFile(); try Thread.sleep(6); catch (InterruptedException ex) 4.打包程序,包名为trans4 课程设计总结 通过本次课程设计,使我第一次了解和初步掌握了J

22、AVA网络编程的基础知识,如TCP和UDP通信的过程,会编写简单的网络应用程序,如聊天程序和远程文件传输程序等。 测试报告1、 界面2、 操作测试发送文件:填写目的IP,可以是自己电脑的IP,可以是同局域网其他计算机的IP。为测试方便,这里填写自己IP查看下传输结果:开始接受3 总结 本点到点传输系统能够很好的实现相同计算机,不同计算机之间进行文件的传输。也可以发送对话,设置计算机端口信息等。 附录(附源代码)本程序为一个包程序,包文件夹为trans,运行方法:分别把每个JAVA程序放在一个txt记事本里面,创建一个名为trans文件夹,把他们全部放在里面cmd环境下运行方法:我的trans文

23、件放在D盘Applicaiton1.javapackage trans;import java.awt.Toolkit;import javax.swing.SwingUtilities;import javax.swing.UIManager;import java.awt.Dimension;public class Application1 boolean packFrame = false; /* * Construct and show the application. */ public Application1() MainFrame frame = new MainFrame(

24、); / Validate frames that have preset sizes / Pack frames that have useful preferred size info, e.g. from their layout if (packFrame) frame.pack(); else frame.validate(); / Center the window Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Dimension frameSize = frame.getSize(); if

25、 (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); /* * Applica

26、tion entry point. * * param args String */ public static void main(String args) SwingUtilities.invokeLater(new Runnable() public void run() try UIManager.setLookAndFeel(UIManager. getSystemLookAndFeelClassName(); catch (Exception exception) exception.printStackTrace(); new Application1(); ); MainFra

27、me.javapackage trans;import java.awt.BorderLayout;import java.awt.Dimension;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JToolBar;import java.awt.*;import javax.swing.JButton;import javax.swing.JLabel;import javax.swing.*;import java.util.*;import java.awt.event.ActionEvent

28、;import java.awt.event.ActionListener;import java.io.File;import .InetAddress;import java.awt.event.WindowEvent;import java.awt.event.WindowAdapter;import java.io.*;public class MainFrame extends JFrame JPanel contentPane; JButton jbtnSend = new JButton(); JLabel jlblIP = new JLabel(); JButton jbtnS

29、etting = new JButton(); JTabbedPane jtpTransFile = new JTabbedPane(); TransFileManager tfm=new TransFileManager(jtpTransFile); public MainFrame() try setDefaultCloseOperation(EXIT_ON_CLOSE); jbInit(); catch (Exception exception) exception.printStackTrace(); private void jbInit() throws Exception con

30、tentPane = (JPanel) getContentPane(); contentPane.setLayout(null); this.getContentPane().setBackground(new Color(206, 227, 249); setSize(new Dimension(400, 300); setTitle("P2P文件传输软件"); this.addWindowListener(new MainFrame_this_windowAdapter(this); jbtnSend.setBackground(new Color(236, 247,

31、 255); jbtnSend.setBounds(new Rectangle(14, 14, 85, 25); jbtnSend.setFont(new java.awt.Font("宋体", Font.PLAIN, 13); jbtnSend.setBorder(BorderFactory.createRaisedBevelBorder(); jbtnSend.setText("发送文件"); jbtnSend.addActionListener(new MainFrame_jbtnSend_actionAdapter(this); jlblIP.s

32、etText("本机IP:"); jbtnSetting.setBackground(new Color(236, 247, 255); jbtnSetting.setBounds(new Rectangle(106, 14, 73, 25); jbtnSetting.setFont(new java.awt.Font("宋体", Font.PLAIN, 13); jbtnSetting.setBorder(BorderFactory.createRaisedBevelBorder(); jbtnSetting.setText("设置"

33、;); jbtnSetting.addActionListener(new MainFrame_jbtnSetting_actionAdapter(this); jtpTransFile.setBackground(new Color(206, 227, 249); jtpTransFile.setBounds(new Rectangle( 0, 54, 402, 246); jlblIP.setFont(new java.awt.Font("宋体", Font.PLAIN, 13); jlblIP.setBounds(new Rectangle(197, 18, 180,

34、 16); contentPane.setBackground(new Color(206, 227, 249); contentPane.setToolTipText(""); contentPane.add(jtpTransFile); contentPane.add(jbtnSend); contentPane.add(jbtnSetting); contentPane.add(jlblIP); tfm.start(); byte ip=InetAddress.getLocalHost().getAddress(); jlblIP.setText("本机IP

35、:"+(ip0&0xff)+"."+(ip1&0xff)+"."+(ip2&0xff)+"."+(ip3&0xff); public void jbtnSetting_actionPerformed(ActionEvent e) SettingDialog sd=new SettingDialog(this); sd.jtfPort.setText(String.valueOf(tfm.port); sd.jspnThreadNum.setValue(new Integer(tfm.maxTh

36、readNum); sd.show(); if(sd.flag) if(tfm.port!=sd.port) tfm.setPort(sd.port); tfm.setMaxThreadNum(sd.threadNum); public void jbtnSend_actionPerformed(ActionEvent e) TransFileDialog tfd=new TransFileDialog(this); tfd.show(); if(tfd.flag) for(int i=0;i<tfd.files.length;i+) tfm.sendFile(tfd.serverNam

37、e,tfd.port,tfd.filesi.getAbsolutePath(),tfd.message); public void this_windowClosed(WindowEvent e) tfm.close(); class MainFrame_this_windowAdapter extends WindowAdapter private MainFrame adaptee; MainFrame_this_windowAdapter(MainFrame adaptee) this.adaptee = adaptee; public void windowClosed(WindowE

38、vent e) adaptee.this_windowClosed(e); class MainFrame_jbtnSend_actionAdapter implements ActionListener private MainFrame adaptee; MainFrame_jbtnSend_actionAdapter(MainFrame adaptee) this.adaptee = adaptee; public void actionPerformed(ActionEvent e) adaptee.jbtnSend_actionPerformed(e); class MainFram

39、e_jbtnSetting_actionAdapter implements ActionListener private MainFrame adaptee; MainFrame_jbtnSetting_actionAdapter(MainFrame adaptee) this.adaptee = adaptee; public void actionPerformed(ActionEvent e) adaptee.jbtnSetting_actionPerformed(e); Server.java/服务器端package trans;import java.io.*;import .*;import java.util.List;public class Server extends Thread public Server() try jbInit(); catch (Exception ex) ex.printStackTrace(); public int port=5000; public int maxThreadNum=5

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论