A TCP connection opens with SYN, SYN-ACK and ACK. Write the three messages for given starting numbers.
What to build
A program that prints the three handshake messages.
Requirements
- Line 1:
client -> SYN seq X. - Line 2:
server -> SYN-ACK seq Y ack X+1with the number X + 1 worked out. - Line 3:
client -> ACK seq X+1 ack Y+1with both numbers worked out.
Input
One line: the client's starting number X and the server's starting number Y.
Output
Three lines.
Constraints
0 ≤ X, Y ≤ 1000000000.
Example
Input:
1000 5000
Output:
client -> SYN seq 1000
server -> SYN-ACK seq 5000 ack 1001
client -> ACK seq 1001 ack 5001
Why it works
A SYN uses up one sequence number, so each side acknowledges the other's starting number plus one, and the client's next sequence number is X + 1.
Examples
1000 5000
client -> SYN seq 1000 server -> SYN-ACK seq 5000 ack 1001 client -> ACK seq 1001 ack 5001
0 0
client -> SYN seq 0 server -> SYN-ACK seq 0 ack 1 client -> ACK seq 1 ack 1
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
The SYN-ACK acknowledges the client's number plus 1.
Hint 2
The final ACK has seq = client + 1 and ack = server + 1.
Hint 3
Build each line as one string with + and str().
Solution
Show one correct solution (try it yourself first)
x, y = map(int, input().split())
print("client -> SYN seq", x)
print("server -> SYN-ACK seq", y, "ack", x + 1)
print("client -> ACK seq", x + 1, "ack", y + 1)
▶ Open this solution in the compiler, then press Visualize to watch it run step by step