Hello I'm trying to make a file transfer program to transfer file over the internet or a network. I've been able to successfully transfer one file but when I try to send multiple files I keep getting a EOFException after the client trys to send the files. The client says it sent both files and the connection was closed but the server said that it only received one file and throws a EOFException. I've included the code from the client and the server. Any help is much appreciated.
The Client
CODE
private class createSocket implements Runnable {
String address;
int port;
public createSocket ( String address, int port ) {
this.address = address;
this.port = port;
}
public void run() {
try {
postStatusMsg( "Opening Connection at " + address + ":" + port );
Socket socket = new Socket( address, port );
postStatusMsg( "Connected to " + address + ":" + port );
DataOutputStream dos = new DataOutputStream( socket.getOutputStream() );
DataInputStream dis = new DataInputStream( socket.getInputStream() );
dos.writeInt( files.length );
for ( File f : files ) {
BufferedOutputStream out = new BufferedOutputStream( dos );
BufferedInputStream in = new BufferedInputStream( new FileInputStream(f) );
postStatusMsg( "Sending: " + f.getName() );
byte[] b = new byte[8192];
int read = -1;
dos.writeUTF( f.getName() );
while ( ( read = in.read( b ) ) >= 0 ) {
out.write( b, 0, read );
}
postStatusMsg( "File Sent" );
in.close();
}
dos.close();
postStatusMsg( "Closing Connection" );
socket.close();
postStatusMsg( "Connection Closed" );
}
catch( IOException e ) {
Main.ShowMsgBox( statusTA, "Error", e.getMessage(), JOptionPane.ERROR_MESSAGE );
try {
e.printStackTrace( new PrintStream( "./log.txt" ) );
}
catch ( FileNotFoundException ee ) {
ee.printStackTrace();
}
}
}
}
The Server
CODE
public class ServerHandler implements Runnable {
private Socket s = null;
public ServerHandler( Socket s ) {
this.s = s;
}
public void run() {
try {
handlers.add( this );
DataInputStream dis = new DataInputStream( s.getInputStream() );
DataOutputStream dos = new DataOutputStream( s.getOutputStream() );
BufferedInputStream in = new BufferedInputStream( dis );
BufferedOutputStream out = null;
int count = dis.readInt();
for ( int i = 0; i < count; i++ ) {
File f = new File( Main.settings.SAVE_DIR );
if ( !f.isDirectory() )
f.mkdir();
f = new File( Main.settings.SAVE_DIR + "\\" + dis.readUTF() );
out = new BufferedOutputStream( new FileOutputStream( f ) );
postStatusMsg( "Receving: " + f.getName() );
byte[] b = new byte[8192];
int read = -1;
while ( ( read = in.read( b ) ) >= 0 ) {
out.write( b, 0, read );
}
postStatusMsg( "File Received" );
out.close();
}
in.close();
postStatusMsg( "Closing Connection" );
s.close();
postStatusMsg( "Connection Closed" );
handlers.remove( this );
}
catch ( IOException e ) {
Main.ShowMsgBox( statusTA, "Error", e.getMessage(), JOptionPane.ERROR_MESSAGE );
try {
e.printStackTrace( new PrintStream( "./log.txt" ) );
}
catch ( FileNotFoundException ee ) {
ee.printStackTrace();
}
}
}
}