I am trying to send a large file over the network lan using a very simple
code, but it only works sometimes. The rest of the times i am getting
D:\java>java fclient
Connecting...
Sending...
Exception in thread "main" 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:126)
at java.io.DataOutputStream.write(DataOutputStream.java:85)
at fclient.main(fclient.java:25)
Maybe anyone have any ideas of improvements?
Thanks in advance!
Stein
---------client side-----
import java.io.*;
import java.net.*;
class fclient {
public static void main (String [] args ) throws IOException {
// create socket
Socket sock = new Socket("127.0.0.1",1111);
System.out.println("Connecting...");
// get the file handle to send
File myFile = new File ("mymp3file.mp3");
byte [] mybytearray = new byte [(int)myFile.length()];
FileInputStream fis = new FileInputStream(myFile);
BufferedInputStream bis = new BufferedInputStream(fis);
bis.read(mybytearray,0,mybytearray.length);
OutputStream os;
DataOutputStream dos;
os=sock.getOutputStream();
dos=new DataOutputStream(os);
System.out.println("Sending...");
dos.write(mybytearray,0,mybytearray.length);
// cleanup
sock.close();
}
}
---------server side-------
import java.io.*;
import java.net.*;
class fserver {
public static void main (String [] args ) throws IOException {
int filesize=6022386; // filesize temporary hardcoded
// create socket
ServerSocket servsock = new ServerSocket(1111);
System.out.println("Waiting for connection...");
Socket sock = servsock.accept();
System.out.println("Accepted connection");
byte [] mybytearray = new byte [filesize];
BufferedInputStream bis = new BufferedInputStream(sock.getInputStream(),filesize);
bis.read(mybytearray,0,mybytearray.length);
FileOutputStream fos = new FileOutputStream("mymp3-copy.mp3");
BufferedOutputStream bos = new BufferedOutputStream(fos);
fos.write(mybytearray,0,mybytearray.length);
// cleanup after file transfer
sock.close();
servsock.close();
}
}
--------------------
> Hi network coding gurus,
>
> I am trying to send a large file over the network lan using a very simple
> code, but it only works sometimes. The rest of the times i am getting
>
> D:\java>java fclient
> Connecting...
> Sending...
> Exception in thread "main" 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:126)
> at java.io.DataOutputStream.write(DataOutputStream.java:85)
> at fclient.main(fclient.java:25)
>
> Maybe anyone have any ideas of improvements?
I'm not sure if this is what's causing the problem, but it's the only
possibility I can see. In the server code, when you call
bis.read(mybytearray,0,mybytearray.length);
it's not guaranteed to read all mybytearray.length bytes (see the API
docs for InputStream.read(byte[],int,int) ). The reason I think this is
the source of the error is that you only do one read and then close the
socket. If that one call to read doesn't get all the bytes from the
client, then the server will close the socket before the client is
finished writing, so the client finds itself trying to write to a closed
socket.
You have to check the return value of read(), which tells you how may
bytes where actually read. Something like this should work (assuming
you know the size of the file in advance):
int bytesRead = 0;
while ( bytesRead < filesize ) {
int off = bytesRead;
int len = filesize - bytesRead
int readCount = bis.read( mybytearray, off, len );
if ( readCount == -1 ) { // chech for EOF
System.err.println( "unexpected EOF" );
System.exit( 1 );
}
bytesRead -= readCount;
}
Regards,
Walter Brameld
<snip>
> You have to check the return value of read(), which tells you how may
> bytes where actually read. Something like this should work (assuming
> you know the size of the file in advance):
>
> int bytesRead = 0;
> while ( bytesRead < filesize ) {
> int off = bytesRead;
> int len = filesize - bytesRead
>
> int readCount = bis.read( mybytearray, off, len );
>
> if ( readCount == -1 ) { // chech for EOF
> System.err.println( "unexpected EOF" );
> System.exit( 1 );
> }
>
> bytesRead -= readCount;
^^
Sorry, that's supposed to be +=, as in
bytesRead += readCount;
> }
Regards,
Walter Brameld
Stein
Walter Brameld <bu...@bigfoot.com> wrote in message news:<3D743637...@bigfoot.com>...