Socket字节流与字符流混合应用?
我做网络通信一段时间,目前发现一个当TCP/Socket通信时,字节流与字符流混合应用出现一个奇怪问题,拿出来与大家讨论下:服务端接收文件代码:
import java.net.*;
import java.io.*;
/**
* <p>Title: </p>
* <p>Description: Socket 服务端用来接收文件</p>
* <p>Copyright: Copyright (c) 2006</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class SocketReceFile extends Thread{
private Socket socket;
public SocketReceFile(Socket socket){
this.socket = socket;
}
public void run(){
rece();
}
public void rece(){
java.io.BufferedReader br = null;
java.io.DataInputStream dis = null;
try{
br = new java.io.BufferedReader(new java.io.InputStreamReader(socket.getInputStream()));
dis = new java.io.DataInputStream(new java.io.BufferedInputStream(socket.getInputStream()));
String line = br.readLine();
System.out.println("起始行:"+line);
File file = new File("d:/SSTP/Yaoming.jpg");
if(file.exists()){
file.delete();
file.createNewFile();
}
FileOutputStream fos = new FileOutputStream(file);
int eve = 2048;
byte[] buf = new byte[eve];
int len = dis.read(buf,0,eve);
int sum = 0;
while(len != -1){
System.out.println("len:"+len);
sum = sum +len;
fos.write(buf,0,len);
fos.flush();
len = dis.read(buf,0,eve);
}
System.out.println("sum:"+sum);
System.out.println("文件接受完毕");
socket.close();
br.close();
fos.close();
dis.close();
} catch(Exception ex){
ex.printStackTrace();
}
}
}
客户端发送代码:
import java.net.*;
import java.io.*;
/**
* <p>Title: </p>
* <p>Description:Sokcet客户端用来发送文件 </p>
* <p>Copyright: Copyright (c) 2006</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class SocketSendFile{
public SocketSendFile(){
}
public static void main(String[] args){
new SocketSendFile().sendByGzip("127.0.0.1",8312);
}
/**
* 通过字节流发送文件,文件带包头,包头的发送与文件信息的发送需要休眠一段时间
* @param ip
* @param port
*/
public void send(String ip,int port){
java.io.PrintWriter pw = null;
java.io.DataOutputStream dos = null;
Socket socket = null;
try{
socket = createClientSocket(ip,port);
pw = new java.io.PrintWriter(socket.getOutputStream());
dos = new java.io.DataOutputStream(new java.io.BufferedOutputStream(socket.getOutputStream()));
pw.println("aaaaaaaaaaaaaaaaaaaaaa");
pw.flush();
File file = new File("d:/sstptest1.jpg");
System.out.println("Lenth:" + file.length());
FileInputStream fis = new FileInputStream(file);
int eve = 2048;
byte[] buf = new byte[eve];
//休眠一段时间,如果不休眠,会导致服务端接受出错
Thread.sleep(100);
int len = fis.read(buf,0,eve);
int sum = 0;
while(len != -1){
System.out.println("len:" + len);
sum = sum + len;
dos.write(buf,0,len);
dos.flush();
len = fis.read(buf,0,eve);
}
System.out.println("sum:" + sum);
socket.close();
pw.close();
dos.close();
} catch(Exception ex){
ex.printStackTrace();
}
}
/**
* 创建Socket客户端
* @return
*/
public Socket createClientSocket(String hostip,int port) throws Exception{
Socket socket = null;
try{
InetSocketAddress it = new InetSocketAddress(hostip,port);
socket = new Socket();
socket.connect(it,60 * 1000);
} catch(Exception ex){
throw ex;
}
return socket;
}
}
问题说明:服务端接收没有问题,大家可以看到,客户端通过字符流发送数据后,需要休眠一段时间才能通过字节流发送文件,如果不休眠会导致服务端接受的文件不全。
讨论:1、字节流与字符流能混合应用吗,如数据需要带有包头。
2、为什么一定要休眠呢?这样在实际环境的应用中是否存在不稳定因素?
----------------解决方案--------------------------------------------------------