Socket编程,关于SocketException异常
我刚写了个Socket程序,服务器端向客户端传送一个文件。程序在执行的时候,发送比较小的文件时不会发生异常(我试了一下,大约500kB以下)。可是当发送比较大的多媒体文件的时候,就会有异常产生。异常的具体信息是:
java.net.SocketException: Software caused connection abort: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
at java.net.SocketOutputStream.write(SocketOutputStream.java:136)
at java.io.BufferedOutputStream.write(BufferedOutputStream.java:105)
at server.Server.sendMusicFileToClient(Server.java:104)
at server.Server.<init>(Server.java:50)
at server.Server.main(Server.java:116)
不知道这是什么原因造成的,希望有哪位达人,能帮忙解答一下。谢谢
代码如下:
服务器端:
messageOut = new PrintWriter(new BufferedWriter(new OutputStreamWriter(serverSocket.getOutputStream())),true);
localFile=new BufferedInputStream(new FileInputStream(filePath+"\\"+selectedFile));
dataOut=new BufferedOutputStream(serverSocket.getOutputStream());
try{
File file=new File(filePath+"\\"+selectedFile);
int fileLength=(int)file.length();
messageOut.println((new Integer(fileLength)).toString());
byte[] musicData=new byte[fileLength];
localFile.read(musicData, 0, fileLength);
dataOut.write(musicData, 0, fileLength);
dataOut.flush();
localFile.close();
dataOut.close();
}catch(Exception e)
客户端:localFile=new BufferedInputStream(new FileInputStream(filePath+"\\"+selectedFile));
dataOut=new BufferedOutputStream(serverSocket.getOutputStream());
try{
File file=new File(filePath+"\\"+selectedFile);
int fileLength=(int)file.length();
messageOut.println((new Integer(fileLength)).toString());
byte[] musicData=new byte[fileLength];
localFile.read(musicData, 0, fileLength);
dataOut.write(musicData, 0, fileLength);
dataOut.flush();
localFile.close();
dataOut.close();
}catch(Exception e)
try{
int fileLength;
fileLength=Integer.parseInt(messageIn.readLine());
byte[] musicData=new byte[fileLength];
dataIn=new BufferedInputStream(clientSocket.getInputStream());
localFile=new BufferedOutputStream(new FileOutputStream("music/"+selectedMusicName));
dataIn.read(musicData, 0, fileLength);
localFile.write(musicData, 0, fileLength);
localFile.flush();
dataIn.close();
localFile.close();
}catch(Exception e){
}
int fileLength;
fileLength=Integer.parseInt(messageIn.readLine());
byte[] musicData=new byte[fileLength];
dataIn=new BufferedInputStream(clientSocket.getInputStream());
localFile=new BufferedOutputStream(new FileOutputStream("music/"+selectedMusicName));
dataIn.read(musicData, 0, fileLength);
localFile.write(musicData, 0, fileLength);
localFile.flush();
dataIn.close();
localFile.close();
}catch(Exception e){
}