public class TCPSocketServer extends Object
ClientThread
) on the server.
It acts as a Network based relay of requests.
It works well with its companion class TCPSocketClient
. DEFAULT_port
44444
.java ca.bc.webarts.tools.sockets.TCPSocketServer [portNumber] - portNumber is optionalIts default function is to take whatever message is sent and execute it directly on the commanline. You should change this to whatever you want.
TCPSocketServer tcpServer = new TCPSocketServer(portToUse); int i = 0; boolean posted = false; while (tcpServer.getSocket()!=null) { try { // Listening for another client connection if (debug_>1) System.out.println("Accepting new connections "); posted = tcpServer.postConnection(tcpServer.getSocket().accept()); //if (debug_>1) System.out.println("connectionPosted> "+posted); if (!posted) { if (debug_>0) System.out.println(" NOT Posted: ClientThreads MAXXED: "); } } catch (IOException e) { System.out.println(e); } }
Modifier and Type | Class and Description |
---|---|
(package private) class |
TCPSocketServer.ClientProcessorThread
Manages the processing of the connection requests that come in.
|
Modifier and Type | Field and Description |
---|---|
protected boolean |
acceptingConnections_ |
protected ArrayBlockingQueue<Thread> |
clientQueue_
The queue of client process threads used by the processor_ .
|
protected Socket |
clientSocket_
The client socket.
|
protected static String |
clientThreadClassname_
the name of the ClientThread class that gets used for processing.
|
static int |
debug_
Simple multi-level debug flag (0-2).The higher the number, the higher the verbosity.
|
static int |
DEFAULT_PORT
Default Server Listening Port (44444).
|
static String |
END
Template Result message for a last final/close result message.
|
static String |
ERROR
Template Result message for a bad /error result.
|
protected static int |
MAX_CLIENTS_COUNT
This chat server can accept up to maxClientsCount clients' connections.
|
protected String |
messageEnd_ |
protected int |
portNumber_
This sever's listening TCP socket.
|
protected TCPSocketServer.ClientProcessorThread |
processor_
The client request processor object thread.
|
protected ServerSocket |
serverSocket_
The server socket.
|
static String |
SUCCESS
Template Result message for a successfull completion.
|
Constructor and Description |
---|
TCPSocketServer()
Default Constructor using defaults.
|
TCPSocketServer(int portNum)
Constructor to allow a custom port.
|
Modifier and Type | Method and Description |
---|---|
void |
closeUpShop(int finalTime)
Clean shutdown.
|
static String |
getClientThreadClassname()
Get Method for class field 'clientThreadClassname_'.
|
ClientThread |
getClientThreadInstance()
Get an instance of the default ClientThread connected to the default class clientSocket_ and executes all client requests as a command on the server.
|
ClientThread |
getClientThreadInstance(String newClassName)
Get an instance of the your own overridden ClientThread that handles client requests as you program. it connects to the default class clientSocket_.
|
String |
getMessageEnd()
Returns the value of messageEnd_.
|
ServerSocket |
getServerSocket() |
ServerSocket |
getSocket() |
protected void |
initPortProcessors()
Initializes all required class objects to be able to process requests.
|
static void |
main(String[] args)
Runs this from the commandline using defaults.
|
boolean |
postConnection(Socket s) |
boolean |
postConnection(Socket s,
int maxBlockTime_ms)
Call this when a new connection comes in to start the procesing of the action that is taken.
|
static void |
setClientThreadClassname(String clientThreadClassname)
Set Method for class field 'clientThreadClassname_'.
|
void |
setMessageEnd(String messageEnd)
Sets the value of messageEnd_.
|
public static int debug_
public static final int DEFAULT_PORT
protected static final int MAX_CLIENTS_COUNT
public static final String SUCCESS
public static final String ERROR
public static final String END
protected ServerSocket serverSocket_
protected Socket clientSocket_
protected int portNumber_
protected TCPSocketServer.ClientProcessorThread processor_
protected ArrayBlockingQueue<Thread> clientQueue_
protected static String clientThreadClassname_
protected boolean acceptingConnections_
protected String messageEnd_
public TCPSocketServer()
public TCPSocketServer(int portNum)
protected void initPortProcessors()
public ServerSocket getServerSocket()
public ServerSocket getSocket()
public String getMessageEnd()
public void setMessageEnd(String messageEnd)
messageEnd
- The value to assign to messageEnd_.public void closeUpShop(int finalTime)
initPortProcessors
.public static void setClientThreadClassname(String clientThreadClassname)
clientThreadClassname
- is the value to set this class field to.public static String getClientThreadClassname()
public ClientThread getClientThreadInstance()
public ClientThread getClientThreadInstance(String newClassName)
newClientThreadClassName
- the name of your ClientThread classpublic boolean postConnection(Socket s)
public boolean postConnection(Socket s, int maxBlockTime_ms)
WebARTS Library Licensed Under the GNU - General Public License. Other Libraries licensed under their respective Open Source Licenses