我想将文件的一部分发送到服务器,它将在服务器屏幕上打印...但是 dos 读取整个输入...请建议我能做什么....有没有其他方法可以将流从套接字读取到零件并将这些零件复制到文件中或在屏幕上打印
服务器端:
/*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();
}}