在客户端逐个读取文件并通过套接字发送并逐个打印

我想将一部分文件发送到服务器,然后将其打印在服务器屏幕上…但是dos会读取整个输入…请提示我能做什么….有没有其他方法可以从socket读取流零件并将这些零件复制到文件或打印屏幕上

服务器端:

/*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(); }} 

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

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

 readBlock(int offset, byte[] buffer, BufferedInputStream 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 

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

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