Java TCP Socket Protocol Implementation

Classified in Electronics

Written on in English with a size of 3.31 KB

Protocol Implementation

The following implementation details the core logic for managing TSocket connections, including matching, establishing, and closing connections.

Matching TSocket Instances

The getMatchingTSocket method identifies a socket based on local and remote port parameters:

protected TSocket getMatchingTSocket(int localPort, int remotePort) {
    lk.lock();
    try {
        for (TSocket ts : activeTSocks) {
            if (ts.localPort == localPort && ts.remotePort == remotePort) {
                return ts;
            }
        }
        for (TSocket ts : listenTSocks) {
            if (ts.localPort == localPort) {
                return ts;
            }
        }
        return null;
    } finally {
        lk.unlock();
    }
}

Passive Connection Handling (PASIU)

The accept method is used in passive mode to wait for and retrieve incoming connections from the queue:

public TSocket accept() {
    lk.lock();
    try {
        log.debug("%1$s->accept()", this);
        if (state != LISTEN) {
            return null;
        }
        while (acceptQueue.empty()) {
            appCV.awaitUninterruptibly();
        }
        log.debug("%1$s->accepted", this);
        return acceptQueue.get();
    } finally {
        lk.unlock();
    }
}

Active Connection Establishment (ACTIU)

In active mode, the connect method initiates the TCP handshake by sending a SYN segment:

protected void connect(int remPort) {
    lk.lock();
    try {
        log.debug("%s->connect(%d)", this, remPort);
        remotePort = remPort;
        proto.addActiveTSocket(this);
        TCPSegment syn = new TCPSegment();
        syn.setSyn(true);
        syn.setPorts(localPort, remotePort);
        this.sendSegment(syn);
        state = SYN_SENT;
        while (state != ESTABLISHED) {
            appCV.awaitUninterruptibly();
        }
    } finally {
        lk.unlock();
    }
}

Connection Termination and Closing

The close method handles the transition of the socket state to CLOSED using FIN segments:

public void close() {
    lk.lock();
    try {
        log.debug("%s->close()", this);
        switch (state) {
            case ESTABLISHED: {
                state = FIN_WAIT;
                TCPSegment fin = new TCPSegment();
                fin.setFin(true);
                fin.setPorts(this.localPort, this.remotePort);
                this.sendSegment(fin);
                while (state != CLOSED) {
                    appCV.awaitUninterruptibly();
                }
                break;
            }
            case CLOSE_WAIT: {
                state = CLOSED;
                TCPSegment fin = new TCPSegment();
                fin.setFin(true);
                fin.setPorts(this.localPort, this.remotePort);
                this.sendSegment(fin);
                appCV.signal();
                proto.removeActiveTSocket(this);
                break;
            }
            default:
                log.error("%s->close: connection does not exist", this);
        }
    } finally {
        lk.unlock();
    }
}

Related entries: