| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 456 人关注过本帖
标题:TCP传东西,怎么会丢失?
只看楼主 加入收藏
whhguo
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:42
专家分:119
注 册:2012-11-21
结帖率:100%
收藏
 问题点数:0 回复次数:3 
TCP传东西,怎么会丢失?
传一个class文件,客户端,打印出的要传的文件长度为1024,1024,425,
服务器端,打印出的接受的文件长度为1024,1024,425,
但为什么接收到的文件,比源文件要小。。。
搜索更多相关主题的帖子: 打印 源文件 
2012-11-30 18:34
whhguo
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:42
专家分:119
注 册:2012-11-21
收藏
得分:0 
客户端:
package ch2;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.awt.FlowLayout;
import
import
import
import
import
import
import
import
import
import

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;


public class TCPClient extends JFrame {
    JTextArea jta;
    JButton jb;
    public  TCPClient(){
        jta=new JTextArea(20,20);
        jb=new JButton("查找");
        jb.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                try {
                    deal();
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
    }});

        this.setLayout(new FlowLayout());
        this.add(jb);
        this.add(jta);
        this.setBounds(200, 100, 400, 300);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }
        public File  dealaa(){
            File fileFlag = new File("");
            String filename="";
             JFileChooser j1 = new JFileChooser();

              int n = j1.showOpenDialog(null);
               filename = j1.getSelectedFile().toString();
              System.out.println(filename);
              if (n == JFileChooser.APPROVE_OPTION) {
                   System.out.println(filename);
                    fileFlag = new File(filename);
         }
              return fileFlag;
        }
    public static void main(String[] args) throws Exception, IOException {
        TCPClient tcp=new TCPClient();
    }
    public void deal() throws UnknownHostException, IOException, InterruptedException{
        Socket socket=new Socket("127.0.0.1",887);
        System.out.println("连接上服务器");

          OutputStream os = socket.getOutputStream();
          PrintWriter writer = new PrintWriter(os, true);
          File file=dealaa();
          BufferedReader br=new BufferedReader(new FileReader(file));
          long fileSize = file.length();
          String fileName = file.getName();
          writer.println(fileSize);
          writer.println(fileName);

          final char[] buffer = new char[1024];
          int len = 0;
          while((len = br.read(buffer,0,buffer.length)) != -1) {
              writer.write(buffer, 0, len);
              writer.flush();
              }
          socket.shutdownOutput();
       //    writer.close();
        //    jta.append(""+socket.isClosed());
             InputStreamReader reader=new InputStreamReader(socket.getInputStream());
               BufferedReader bra=new BufferedReader(reader);
               String response=bra.readLine();
               jta.append(response+"/n");
         //      bra.close();

            //   jta.append(""+socket.isClosed());
                br.close();
          //      jta.append(""+socket.isClosed());
        //        writer.close();
          //      jta.append(""+socket.isClosed());
                os.close();
                Thread.sleep(1000);
             //   jta.append(""+socket.isClosed());


             //   System.out.println();
         /*     InputStreamReader reader=new InputStreamReader(socket.getInputStream());
               BufferedReader bra=new BufferedReader(reader);
               String response=bra.readLine();
               jta.append(response+"/n");
               bra.close();*/

              socket.close();
    }
}

服务器端:
package ch2;

import java.awt.FlowLayout;
import
import
import
import
import
import
import
import
import

import javax.swing.*;

public class TCPServer extends JFrame{

  public static void main(String[]args) throws Exception{
      TCPServer tcp=new TCPServer();
      tcp.deal();
  }
  public  void deal() throws Exception{
      ServerSocket server=new ServerSocket(887);
          Socket s=server.accept();   
          System.out.println("客户端连接进来了");
          InputStream is = s.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            PrintWriter pw = new PrintWriter(s.getOutputStream());
            System.out.println("文件开始上传");  
            String fileSizeStr = br.readLine();            
            System.out.println("文件长度"+fileSizeStr);           
            long fileSize = Long.valueOf(fileSizeStr).longValue();   
            String fileName = br.readLine();            
            System.out.println("文件名称"+fileName);         
            StringBuilder filePath = new StringBuilder();     
            filePath.append("D://file");            
            File fileCatalog = new File(filePath.toString());            
            if(!fileCatalog.exists())               
                fileCatalog.mkdirs();            
            filePath.append("//").append(fileName);     
            System.out.println(filePath);
            File file = new File(filePath.toString());            
            FileWriter fos = new FileWriter(file);            
            final char[] buffer = new char[1024];                                
            System.out.println("开始接收文件");   
            
            
            int len = 0;            
            while((len = br.read(buffer,0,buffer.length))!= -1) {
                //System.out.println("hahahhahahha");
                fos.write(buffer, 0, len);               
                fos.flush();            
                }   
            System.out.println("文件上传完毕");   
            pw.write("文件上传完毕");
            pw.flush();
            pw.close();
            
            fos.close();         
            is.close();            
             s.close();
             server.close();
            
   
      }
  }

2012-11-30 18:40
whhguo
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:42
专家分:119
注 册:2012-11-21
收藏
得分:0 
不用看了哈,已经搞好了
2012-12-01 11:08
快速回复:TCP传东西,怎么会丢失?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.023809 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved