【问题标题】:Reading file part-by-part at client side and sending it via sockets and printing it part-by-part在客户端逐部分读取文件并通过套接字发送并逐部分打印
【发布时间】:2013-03-11 05:00:12
【问题描述】:

我想将文件的一部分发送到服务器,它将在服务器屏幕上打印...但是 dos 读取整个输入...请建议我可以做什么...有没有其他方法可以读取流从插座到零件并将这些零件复制到文件中或在屏幕上打印 tem

服务器端:

/*Aim:to read file in parts...send part to server...write part in the file..*/
import java.io.*;
import java.net.*;
public class Tser {

    public static void main(String a[])throws IOException{

        ServerSocket sock=new ServerSocket(6000);
        Socket csock=sock.accept();
        DataInputStream dis=new DataInputStream(csock.getInputStream());
        FileWriter fw=new FileWriter("elephant");
        BufferedWriter bw=new BufferedWriter(fw);
        BufferedInputStream br=new BufferedInputStream(dis);
        String mess="";int c;
        byte b[]=new byte[20];
        while(br.read(b,0,20)!=-1)
        {
            for(int i=0;i<20;i++)
                mess+=(char)b[i];
            System.out.println(mess);
            System.out.println("XX");
        }

        //bw.write(mess);
        //System.out.print(mess);
        br.close();
        bw.close();
        dis.close();
        sock.close();
        csock.close();
    }

}

客户端:

import java.io.*;
import java.net.*;
public class Tcle {

    public static void main(String a[])throws IOException{
        Socket soc=new Socket("localhost",6000);

        FileReader fr=new FileReader("samp1");
        BufferedReader br=new BufferedReader(fr);
        DataOutputStream dos=new DataOutputStream(soc.getOutputStream());
        String hi="";int c;
        char ch[]=new char[20];

        while(br.read(ch,0,20)!=-1)
        {
            hi=String.valueOf(ch);
            dos.writeBytes(hi);
            //System.out.println(ch);
        }


        //br.flush();
        fr.close();
        br.close();
        dos.close();
        soc.close();

    }}

【问题讨论】:

    标签: java java-io


    【解决方案1】:

    使用InputStream.skip() 到达您要开始发送的位置。

    如果您使用InputStream 进行阅读,您应该使用OutputStream 而不是Reader 进行写作。相反,如果您使用的是 Writer,则应该使用 Reader 阅读,但前提是您知道数据是文本。

    【讨论】:

      【解决方案2】:

      正如@EJP 所说,一个简单的方法是使用InputStream.skip()

      解决方案可能是创建一个新方法,

      readBlock(int offset, byte[] buffer, BufferedInputStream bis)
      

      bis读取指定offset的buffer.length字节

      public static int readBlock(int offset, byte[] buffer,
                  BufferedInputStream bis) throws IOException {
      
              bis.skip(offset);
              int numberOfBytesRead = bis.read(buffer);
      
              return numberOfBytesRead;
          }
      

      所以,你的新 Tser 类看起来像

      public class Tser {
          static int BLOCK_SIZE = 20; // only for this example
      
          public static void main(String a[]) throws IOException {
      
              ServerSocket sock = new ServerSocket(6000);
              Socket csock = sock.accept();
              DataInputStream dis = new DataInputStream(csock.getInputStream());
              FileWriter fw = new FileWriter("elephant");
              BufferedWriter bw = new BufferedWriter(fw);
              BufferedInputStream br = new BufferedInputStream(dis);
              String mess = "";
              int c;
      
              byte b[] = new byte[BLOCK_SIZE];
              // we want to read the part contained from byte 10 to 30. (20 bytes)
              readBlock(10, b, br);
              // beware of the encoding!
              System.out.println(new String(b, "UTF-8"));
              System.out.println("XX");
      
              // bw.write(mess);
              // System.out.print(mess);
              br.close();
              bw.close();
              dis.close();
              sock.close();
              csock.close();
          }    
      }
      

      而且,你可以在客户端做同样的事情:

      public class Tcle {
      
          public static void main(String a[]) throws IOException {
              Socket soc = new Socket("localhost", 6000);
      
              FileInputStream fis = new FileInputStream("sample");
              BufferedInputStream bis = new BufferedInputStream(fis);
              DataOutputStream dos = new DataOutputStream(soc.getOutputStream());
      
              // 64 bytes, starting at 2nd byte, for example.
              byte[] b = new byte[64];
              readBlock(2, b, bis);
              dos.write(b);
      
              bis.close();
              dos.close();
              soc.close();
      
          }
      
      
      
      }
      

      在这个例子中,我们在双方(客户端和服务器)进行分割

      假设文件sample包含以下字符:


      12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
      

      1.(客户端):我们从字节 2 开始占用 64 个字节。所以,我们正在发送:


      3456789012345678901234567890123456789012345678901234567890123456
      

      2.(服务器端):我们从第 10 个字节开始读取 20 个字节。(就是这样,它将是原始文件的第 74 个字节)。因此,输出将是:

      34567890123456789012
      XX
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-09-08
        • 1970-01-01
        • 1970-01-01
        • 2017-05-05
        • 1970-01-01
        • 2019-01-29
        • 2012-05-11
        • 2014-08-30
        相关资源
        最近更新 更多