HOME PCB
..ReceivingStreamThread.javaSendingStreamThread.javaTCPMessageService.javaTCPSession.java
package Communication.Messages.TCP;

import Communication.Messages.Service.MessageService;

import java.io.IOException;
import java.net.ServerSocket;

public class TCPMessageService extends MessageService
{
    private final ServerSocket sock;

    public TCPMessageService(int port) throws IOException
    {
        this.sock = new ServerSocket(port);
    }

    @Override
    protected ReceivingStreamThread startReceiver()
    {
        ReceivingStreamThread thread = new ReceivingStreamThread(this.sock);
        thread.start();
        return thread;
    }

    @Override
    protected SendingStreamThread startSender()
    {
        SendingStreamThread thread = new SendingStreamThread(this.sock.getLocalPort());
        thread.start();
        return thread;
    }

    @Override
    protected void finalize() throws Throwable
    {
        if (!this.sock.isClosed())
        {
            this.sock.close();
        }
        super.finalize();
    }
}