# I need help with Multithreading File server in Java



## preiman (Jul 30, 2009)

Here is my code as I have it so far, but only accepts one client connection.
I am stuck as to where to proceed.
******************************************************8
import java.io.*;
import java.net.*;


// Java extension packages


public class Server
{

private ObjectOutputStream output;
private ObjectInputStream input;
private BufferedReader sendFile;
private BufferedWriter returnFile;
private ServerSocket server;
private Socket connection;
private File file;
private int counter = 0;
private String fileName = null;


// Constructor starts the execution of the server
public Server()
{

runServer();
} // end of constructor

public static void main( String args[] ) throws IOException
{
// creat a server
new Server();
}// end main


// set up and run server
private void runServer()
{
// set up server to receive connections and process connections
try
{
// Step 1: Create a ServerSocket.
server = new ServerSocket( 5001, 100 );

while ( true )
{

// Step 2: Wait for a connection.
waitForConnection();

// Step 3: Get input and output streams.
getStreams();

// Step 4: Process connection.
getTheFileName();
receiveTheFile();

}
}

// process EOFException when client closes connection
catch ( EOFException eofException )
{
System.out.println( "Client terminated connection" );
}

// process problems with I/O
catch ( IOException ioException )
{
ioException.printStackTrace();
}
} // end runServer()

// process connection with client
private void getTheFileName() throws IOException
{
//String stringLine = null;
try
{
fileName = (String) input.readObject();
System.out.println(fileName);
}
catch( ClassNotFoundException classNotFoundException )
{
//empty
}



// Step 5: Close connection.
closeConnection();

}//end processConnection

private void sendTheFile()
{
String sendLine = null;

try
{
// open the file
sendFile = new BufferedReader( new FileReader(fileName) );


while( (sendLine = sendFile.readLine() ) != null )
{
output.writeObject( sendLine );
System.out.println(sendLine);
output.flush();

}

}
catch( IOException ioException )
{
//
}

}

private void receiveTheFile()
{
// set up server again to receive the file
try
{
// Step 1: Create a ServerSocket.
server = new ServerSocket( 5000, 100 );

while ( true )
{
// Step 2: Wait for a connection.
waitForConnection();

// Step 3: Get input and output streams.
getStreams();

// Step 4: Process connection.
processRecieve();

// Step 5: Close connection.
closeConnection();
System.out.println( "File send, now connection closed!");
}
}

// process EOFException when client closes connection
catch ( EOFException eofException )
{

System.out.println( "Client terminated connection" );
}

// process problems with I/O
catch ( IOException ioException )
{
ioException.printStackTrace();
}
}

private void processRecieve()
{
String returnLine = "";

try
{
returnFile = new BufferedWriter( new FileWriter(fileName) );
returnLine = (String) input.readObject();
System.out.println( "This is file on server: \n" + returnLine);
returnFile.write( returnLine);//, 0, returnLine.length() );
//returnFile.newLine();
returnFile.append(fileName);


}

catch(FileNotFoundException fileNotFound)
{
//
}
catch(ClassNotFoundException classNotFoundException)
{
//
}
catch(IOException e)
{
//
}

}

// wait for connection to arrive
private void waitForConnection() throws IOException
{


// allow server to accept a connection
connection = server.accept();

// debug
System.out.println("Server is running");

} // end waitForConnection

// get streams to send and receive data
private void getStreams() throws IOException
{
// set up output stream for objects
output = new ObjectOutputStream(connection.getOutputStream() );


// set up input stream for objects
input = new ObjectInputStream(connection.getInputStream() );

}// end getStreams

// close streams and socket
private void closeConnection() throws IOException
{
output.close();
input.close();
connection.close();
} //end closeConnection

} // end class FileServer


----------

