Java TCP Server and Handling Thread


When a TCP connection is received by the server, it is offloaded to a separate thread for the processing and interaction to occur.

public class LogTCPThread extends Thread {

    protected ServerSocket socket = null;
    protected boolean listen = true;
    int count = 0;
    
    public LogTCPThread() throws IOException {
        this("TCPThread");
    }

    public LogTCPThread(String name) throws IOException {
        super(name);
        socket = new ServerSocket(4446);
    }

    @Override
    public void run() {
        try {
            while (listen) {
                try {
                    // Wait for a syn handshake to open a connection
                    Socket connectionSocket = socket.accept();
                    System.out.println("Recieving: " + ++count);
                    // Thread out the rest of the connection
                    ProcessTCPThread processThread = new ProcessTCPThread(connectionSocket);
                    processThread.start();
                    
                } catch (Exception e) {
                    e.printStackTrace();
                    listen = false;
                }
            }
        } finally {
            try {
                socket.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}


public class ProcessTCPThread extends Thread {
    
     Socket connectionSocket;    
     public ProcessTCPThread(Socket connectionSocket) {
         this.connectionSocket = connectionSocket;
     }
    
     @Override
     public void run() {
         try {
             BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
             //DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
             String tcpData = inFromClient.readLine();
             //System.out.println("Received: " + tcpData);
             
             //outToClient.writeBytes("output);
             // figure out response
             RawLog log = new RawLog();
             log.dataString = tcpData;
             log.timestamp = new Date();
             log.sourceAddress = connectionSocket.getInetAddress().getHostAddress();
             log.sourceName = connectionSocket.getInetAddress().getHostName();
             log.sourceType = "TCP";

             Main.messagesToLog.add(log);
         } catch(Exception ex) {
             ex.printStackTrace();
         } finally {
            try {
                connectionSocket.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
         }
     }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s