Wednesday, April 26, 2023

10 points on TCP/IP Protocol, Java Programmers should Know

TCP/IP is one of the most important protocol as its backbone of HTTP, internet and most of the communication happens today. It's also important from technical interview perspective, you might have already seen difference between TCP and UDP multiple time during interviews ( I have seen). I believe every programmer, not just Java developer should know what is TCP/IP protocol? How it works and the below points I am going to mention. These are basic points and most of you already know but during Interview I have found many developers who doesn't really know what TCP/IP offers and how it works and what are the pros and cons of TCP/IP protocol. That's where, I think this article will help you. Anyway, let's jump into technical details now. 


10 points on TCP/IP Protocol, Java Programmers Should Know

Here are the 10 essential point about TCP/IP protocol every programmer should know and remember. 

1) TCP/IP is a Connection oriented Protocol, which means connection is established before sending data. Here is how TCP/IP works and how connections are established. 




2) TCP/IP maintains order of data, which means receiver will receive data in same order as sender has sent to them. This prevents lots of out-of-order handling in application.

3) TCP/IP provides delivery guarantee, which means no message is lost in transit. TCP/IP achieves this guarantee by acknowledgement. When Sender sends data, a TCP packet, it waits for receiver’s ack. If no ack received than it sends the data again, till then it holds data in its buffer. When receiver responds, it also indicate size of unused buffer and receive window, to say how much more data it can accept.


4) Each TCP/IP connection is identified by combination of local IP, local port, remote IP address and remote port. You can use netstat command to see all TCP/IP connection for a host. netstat works in both windows and Linux.

$ netstat -na | grep tcp
tcp        0      0 0.0.0.0:7500                0.0.0.0:*                   LISTEN    
tcp        0      0 127.0.0.1:36655             127.0.0.1:1521   

Since TCP connection also has different state, you can use them to find only ESTABLISHED connection or Server connection, which are listening. You can check netstat command to learn more options.


5) TCP No Delay and Naggle's algorithm
This question was asked to me during an interview with a big investment bank where I was interviewing for a low latency Java developer role, mainly working with FIX protocol or developing similar protocol.  You now might have guessed it that its related to performance. 

Yes, Nagle's algorithm is a way of improving the efficiency of TCP/IP networks by reducing the number of packets that need to be sent over the network and its controlled using an option called TCP_NODELAY. 

6) TIME_WAIT state
This is one of the state of socket which you will see when you query using Linux commands like netstat. You will mostly see the state with TCP connection and not with UDP connections because there are no states used in UDP, this column is usually blank.  TIME_WAIT state means  the socket is waiting after close to handle packets still in the network.

7) Congestion control
TCP protocol also does congestion control which means it doesn't operate in full speed at the start instead it slowly increased the speed depending upon how receiver is performing to avoid congestion and exhausting bandwidth. 

8) TCP States
Here are the different states on which a TCP Socket can be. 
       ESTABLISHED
              The socket has an established connection.
       SYN_SENT
              The socket is actively attempting to establish a connection.
       SYN_RECV
              A connection request has been received from the network.
       FIN_WAIT1
              The socket is closed, and the connection is shutting down.
       FIN_WAIT2
              Connection is closed, and the socket is waiting for a shutdown from the remote end.
       TIME_WAIT
              TIME_WAIT state means  the socket is waiting after close to handle packets still in the network.
       CLOSED The socket is not being used.
       CLOSE_WAIT
              The remote end has shut down, waiting for the socket to close.
       LAST_ACK
              The remote end has shut down, and the socket is closed. Waiting for acknowledgement.
       LISTEN The socket is listening for incoming connections.  Such sockets are not included in the output unless you specify the --listening  (-l)  or
              --all (-a) option.
       CLOSING
              Both sockets are shut down but we still don't have all our data sent.
       UNKNOWN
              The state of the socket is unknown.


9) netstat
You can see this article about how to use netstat command to find TCP connections as well as process id of the process which opens that connection. 


10) sliding window protocol
This is another key concept associated with TCP connection and a useful algorithms to know. As per Wikipedia, A sliding window protocol is a feature of packet-based data transmission protocols. Sliding window protocols are used where reliable in-order delivery of packets is required, such as in the data link layer (OSI layer 2) as well as in the Transmission Control Protocol (TCP).


That's all about the 10 essential point about TCP connections every programmer should learn and remember. As I said, TCP/IP and UDP are two important protocol in the world of computers and its used everywhere. In fact, HTTP is based upon TCP, hence its imperative for every programmer or developer to know about what is TCP and how it works as well as tools to deal with TCP connections for debugging and troubleshooting. 


Related UNIX and Linux command tutorials for Java Programmers:
  • How to call REST web service from UNIX command line? (command)
  • 10 examples of find command in UNIX (examples)
  • 10 examples of grep command in UNIX (examples)
  • The difference between the soft link and hard link in Linux? (answer)
  • UNIX command to find the size of file and directory? (command)
  • How to get an IP address from a hostname and vice-versa in Linux (command)
  • 10 examples of tar command in UNIX (examples)
  • How to how long argument of a process in Solaris (command)
  • 10 examples of Vim in UNIX (examples)
  • How to create, update and delete soft link in UNIX (command)
  • 5 examples of sort command in Linux (examples)
  • How to make directory tree in one command? (example)
  • UNIX command to find out how long a process is running? (answer)
  • 5 examples of kill command in Linux (examples)
  • 10 examples of chmod command in UNIX (examples)
  • 10 examples of xargs command in Linux (examples)
  • How to delete empty files and directory in UNIX (solution)
  • 10 tips to work fast in UNIX? (tips)
  • 10 examples of date command in Linux (examples)

Thanks for reading this article so far. If you like this TCP/IP tutorial and find the information useful please share with your friends and colleagues. If you have any questions or feedback then please drop a note. If you want to learn more, you can also checkout these best TCP/IP books and courses, I highly recommend them if you want to learn in depth. 

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.