4 * Copyright 2015 TJ <hacker@iam.tj>.
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 package uk.ac.ntu.n0521366.wsyd.libs.net;
26 import java.net.ServerSocket;
27 import java.net.SocketException;
30 * Dual-use multithreading network TCP server that can be used stand-alone
31 * or in a Swing GUI application as a background worker thread.
33 * @see NetworkServerAbstract
34 * @author TJ <hacker@iam.tj>
36 public class NetworkServerTCP extends NetworkServerAbstract {
37 ServerSocket _serverSocket;
40 * Open the socket ready for accepting connections.
42 * It should also set a reasonable socket timeout with a call to setSoTimeout()
44 * @throws SocketException
47 public void serverOpen() throws SocketException {
52 * Send an unsolicited message to a remote service.
54 * This method is called by the main worker loop if there is a message to
57 * @param message must have its _serviceTarget parameter set
58 * @return true if the message was sent
61 protected boolean serverSend(NetworkMessage message) {
62 boolean result = false;
70 * @throws SocketException
73 public void serverClose() throws SocketException {
78 * Accept connection from remote hosts.
80 * This method should wait for a single incoming connection.
82 * @return true if the server should continue listening
85 public boolean serverListen() {
86 boolean result = false;
91 /* XXX: Methods below here all execute on the GUI Event Dispatch Thread */
94 * Clean up after doInBackground() has returned.
96 * This method will run on the GUI Event Dispatch Thread so must complete quickly.
99 protected void done() {