Can someone tell me when does TCP send the data to the application layer (when receiving it) and if the message is divided into multiple segments does it wait till they all arrive to forward to application layer? what happens specifically , I tried searching online but I couldnt find an answer
The abstraction model for an application using TCP is a stream of bytes. Bytes will be delivered to the receiving application in the same order they were sent by the sending application but often not in the same sized chunks. If an application wants to send messages over a TCP connection it will need to provide it's own message framing.
If a packet is lost and needs to be retransmitted or if the network reorders packets then the receiving TCP implementation will need to hold the data after the gap back until it can deliver the data to the receiving appliation in order.
Format: RECEIVE (local connection name, buffer address, byte count) -> byte count, urgent flag, push flag
This command allocates a receiving buffer associated with the specified connection. If no OPEN precedes this command or the calling process is not authorized to use this connection, an error is returned.
In the simplest implementation, control would not return to the calling program until either the buffer was filled, or some error occurred, but this scheme is highly subject to deadlocks. A more sophisticated implementation would permit several RECEIVEs to be outstanding at once. These would be filled as segments arrive. This strategy permits increased throughput at the cost of a more elaborate scheme (possibly asynchronous) to notify the calling program that a PUSH has been seen or a buffer filled.
If enough data arrive to fill the buffer before a PUSH is seen, the PUSH flag will not be set in the response to the RECEIVE. The buffer will be filled with as much data as it can hold. If a PUSH is seen before the buffer is filled the buffer will be returned partially filled and PUSH indicated.
If there is urgent data the user will have been informed as soon as it arrived via a TCP-to-user signal. The receiving user should thus be in "urgent mode". If the URGENT flag is on, additional urgent data remains. If the URGENT flag is off, this call to RECEIVE has returned all the urgent data, and the user may now leave "urgent mode". Note that data following the urgent pointer (non-urgent data) cannot be delivered to the user in the same buffer with preceeding urgent data unless the boundary is clearly marked for the user.
To distinguish among several outstanding RECEIVEs and to take care of the case that a buffer is not completely filled, the return code is accompanied by both a buffer pointer and a byte count indicating the actual length of the data received.
Alternative implementations of RECEIVE might have the TCP allocate buffer storage, or the TCP might share a ring buffer with the user.