org.apache.commons.net.tftp
Class TFTPServer

java.lang.Object
  extended by org.apache.commons.net.tftp.TFTPServer
All Implemented Interfaces:
Runnable

public class TFTPServer
extends Object
implements Runnable

A fully multi-threaded tftp server. Can handle multiple clients at the same time. Implements RFC 1350 and wrapping block numbers for large file support. To launch, just create an instance of the class. An IOException will be thrown if the server fails to start for reasons such as port in use, port denied, etc. To stop, use the shutdown method. To check to see if the server is still running (or if it stopped because of an error), call the isRunning() method. By default, events are not logged to stdout/stderr. This can be changed with the setLog and setLogError methods.

Example usage is below: public static void main(String[] args) throws Exception { if (args.length != 1) { System.out .println("You must provide 1 argument - the base path for the server to serve from."); System.exit(1); } TFTPServer ts = new TFTPServer(new File(args[0]), new File(args[0]), GET_AND_PUT); ts.setSocketTimeout(2000); System.out.println("TFTP Server running. Press enter to stop."); new InputStreamReader(System.in).read(); ts.shutdown(); System.out.println("Server shut down."); System.exit(0); }

Since:
2.0
Author:
Dan Armbrust

Nested Class Summary
static class TFTPServer.ServerMode
           
 
Constructor Summary
TFTPServer(File serverReadDirectory, File serverWriteDirectory, int port, TFTPServer.ServerMode mode, PrintStream log, PrintStream errorLog)
          Start a TFTP Server on the specified port.
TFTPServer(File serverReadDirectory, File serverWriteDirectory, TFTPServer.ServerMode mode)
          Start a TFTP Server on the default port (69).
 
Method Summary
protected  void finalize()
           
 int getMaxTimeoutRetries()
          Get the current value for maxTimeoutRetries
 int getSocketTimeout()
          The current socket timeout used during transfers in milliseconds.
 boolean isRunning()
          check if the server thread is still running.
 void run()
           
 void setLog(PrintStream log)
          Set the stream object to log debug / informational messages.
 void setLogError(PrintStream logError)
          Set the stream object to log error messsages.
 void setMaxTimeoutRetries(int retries)
          Set the max number of retries in response to a timeout.
 void setSocketTimeout(int timeout)
          Set the socket timeout in milliseconds used in transfers.
 void shutdown()
          Stop the tftp server (and any currently running transfers) and release all opened network resources.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TFTPServer

public TFTPServer(File serverReadDirectory,
                  File serverWriteDirectory,
                  TFTPServer.ServerMode mode)
           throws IOException
Start a TFTP Server on the default port (69). Gets and Puts occur in the specified directories. The server will start in another thread, allowing this constructor to return immediately. If a get or a put comes in with a relative path that tries to get outside of the serverDirectory, then the get or put will be denied. GET_ONLY mode only allows gets, PUT_ONLY mode only allows puts, and GET_AND_PUT allows both. Modes are defined as int constants in this class.

Parameters:
serverReadDirectory - directory for GET requests
serverWriteDirectory - directory for PUT requests
mode - A value as specified above.
Throws:
IOException - if the server directory is invalid or does not exist.

TFTPServer

public TFTPServer(File serverReadDirectory,
                  File serverWriteDirectory,
                  int port,
                  TFTPServer.ServerMode mode,
                  PrintStream log,
                  PrintStream errorLog)
           throws IOException
Start a TFTP Server on the specified port. Gets and Puts occur in the specified directory. The server will start in another thread, allowing this constructor to return immediately. If a get or a put comes in with a relative path that tries to get outside of the serverDirectory, then the get or put will be denied. GET_ONLY mode only allows gets, PUT_ONLY mode only allows puts, and GET_AND_PUT allows both. Modes are defined as int constants in this class.

Parameters:
serverReadDirectory - directory for GET requests
serverWriteDirectory - directory for PUT requests
mode - A value as specified above.
log - Stream to write log message to. If not provided, uses System.out
errorLog - Stream to write error messages to. If not provided, uses System.err.
Throws:
IOException - if the server directory is invalid or does not exist.
Method Detail

setMaxTimeoutRetries

public void setMaxTimeoutRetries(int retries)
Set the max number of retries in response to a timeout. Default 3. Min 0.

Parameters:
retries -

getMaxTimeoutRetries

public int getMaxTimeoutRetries()
Get the current value for maxTimeoutRetries


setSocketTimeout

public void setSocketTimeout(int timeout)
Set the socket timeout in milliseconds used in transfers. Defaults to the value here: http://commons.apache.org/net/apidocs/org/apache/commons/net/tftp/TFTP.html#DEFAULT_TIMEOUT (5000 at the time I write this) Min value of 10.


getSocketTimeout

public int getSocketTimeout()
The current socket timeout used during transfers in milliseconds.


finalize

protected void finalize()
                 throws Throwable
Overrides:
finalize in class Object
Throws:
Throwable

isRunning

public boolean isRunning()
                  throws Exception
check if the server thread is still running.

Returns:
true if running, false if stopped.
Throws:
Exception - throws the exception that stopped the server if the server is stopped from an exception.

run

public void run()
Specified by:
run in interface Runnable

shutdown

public void shutdown()
Stop the tftp server (and any currently running transfers) and release all opened network resources.


setLog

public void setLog(PrintStream log)
Set the stream object to log debug / informational messages. By default, this is a no-op

Parameters:
log -

setLogError

public void setLogError(PrintStream logError)
Set the stream object to log error messsages. By default, this is a no-op

Parameters:
logError -


Copyright © 2001-2014 The Apache Software Foundation. All Rights Reserved.