TCP: How are the seq / ack numbers generated?
I have the same job to do.
Firstly the initial seq# will be generated randomly(0-4294967297).
Then the receiver will count the length of the data it received and send the ACK of seq# + length = x
to the sender. The sequence will then be x and the sender will send the data. Similarly the receiver will count the length x + length = y
and send the ACK as y
and so on... Its how the the seq/ack is generated...
If you want to show it practically try to sniff a packet in Wireshark and follow the TCP stream and see the scenario...
When a TCP connection is established, each side generates a random number as its initial sequence number. It is a strongly random number: there are security problems if anybody on the internet can guess the sequence number, as they can easily forge packets to inject into the TCP stream.
Thereafter, for every byte transmitted the sequence number will increment by 1. The ACK field is the sequence number from the other side, sent back to acknowledge reception.
RFC 793, the original TCP protocol specification, can be of great help.