TCP/UDP and Sockets Study Notes for GATE & Computer Science Engineering Exams

By BYJU'S Exam Prep

Updated on: September 25th, 2023

Transport Layer Protocols

There are two types of transport layer protocols as given below:-

UDP (User Datagram Protocol)

UDP is a connection less protocol. UDP is a connection less protocol. UDP provides how for application to send encapsulate IP datagram and send them without having to determine a connection.


Here we are providing a list of important topics of the TCP/UDP and Sockets asked in GATE and Computer Science engineering exams

  • Datagram oriented
  • Unreliable, connectionless
  • Simple
  • Unicast and multicast
  • Useful only for few applications, e.g., multimedia applications
  • Used a lot for services: Network management (SNMP), routing (RIP), naming (DNS), etc.

UDP transmitted segments consists of an 8 byte header followed by the payload. The two parts serve to spot the top points within the source and destinations machine. When UDP packets arrives, then its payload is handed to the process attached to the destination ports.

  • Source Port Address (16 Bits)

Total length of the User Datagram (16 Bits)

  • Destination Port Address (16 Bits)

Checksum (used for error detection) (16 Bits

TCP (Transmission Control Protocol)

TCP provides full transport layer services to applications. TCP is reliable stream transport port-to-port protocol. The term stream during this context, means connection-oriented, a connection must be established between both ends of transmission before either may transmit data. By creating this connection, TCP generates a virtual circuit between the sender and receiver that is active during the transmission.

TCP is a reliable, point-to-point, connection-oriented, full-duplex protocol.


Flag bits

  • URG: Urgent pointer is valid If the bit is set, the following bytes contain an urgent message in the sequence number range “SeqNo <= urgent message <= SeqNo + urgent pointer”
  • ACK: Segment carries a valid acknowledgement
  • PSH: PUSH Flag, Notification from sender to the receiver that the receiver should pass all data that it has to the application. Normally set by sender when the sender’s buffer is empty
  • RST: Reset the connection, The flag causes the receiver to reset the connection. Receiver of a RST terminates the connection and indicates higher layer application about the reset
  • SYN: Synchronize sequence numbers, Sent in the first packet when initiating a connection
  • FIN: Sender is finished with sending. Used for closing a connection, and both sides of a connection must send a FIN.

TCP segment format

 Each machine supporting TCP features a TCP transport entity either a library procedure, a user process or port of kernel. In all the cases, it manages TCP streams and interfaces to respective IP layer. A TCP entities accepts the user data stream from local processes, and breaks them up into pieces not exceeding the value 64 K bytes and sends each bit as separate IP datagrams. 


A socket is one end of an inter-process channel. The two processes each establish their own socket. The system calls establishes a connection which are slightly different for the client and the server, but both involve the basic construct of a socket.

The steps involved in establishing a socket on the client side are as follows:

  1. Create a socket with the socket() system call
  2. Connect the socket to the address of the server using the connect() system call
  3. Send and receive data. There are a number of ways to do this, but the simplest is to use the read() and write() system calls.

The steps involved in establishing a socket on the server side are as follows:

  1. Create a socket with the socket() system call
  2. Bind the socket to an address using the bind() system call. For a server socket on the Internet, an address consists of a port number on the host machine.
  3. Listen for connections with the listen() system call
  4. Accept a connection with the accept() system call. This call typically blocks until a client connects with the server.
  5. Send and receive data.

When a socket is made , the program has got to specify the address domain and therefore the socket type

Two processes can communicate with one another as long as their sockets are of an equivalent type and within the same domain.

There are widely used domains, such as Unix domain, in which two processes which share a common file system communicate, and the Internet domain, in which the two processes running on any two hosts on the web communicate. Each of these has its own address format.

The address of a socket within the Unix domain may be a character string which is essentially an entry within the filing system .

The address of a socket within the Internet domain consists of the web address of the host machine (every computer on the web features a unique 32 bit address, often referred to as its IP address) .  In addition, each socket needs a port number thereon host. Port numbers are 16 bit unsigned integers. The lower numbers are reserved in Unix for normal services

 It is important that standard services be at an equivalent port on all computers in order that clients will know their addresses though, port numbers above 2000 are generally available.

Socket Types

There are two widely used types -socket types, stream sockets, and datagram sockets.

Each uses its own communications protocol. Stream sockets use TCP (Transmission Control Protocol), which may be a reliable, stream oriented protocol, and datagram sockets use UDP (Unix Datagram Protocol), which is unreliable and message oriented. A second sort of connection may be a datagram socket. You might want to use a datagram socket in cases where there’s just one message being sent from the client to the server, and just one message being sent back. There are various differences between a datagram socket and the stream socket. 

  1. Datagrams are unreliable, which means that if a packet of information gets lost somewhere in the Internet, the sender is not told (and of course the receiver does not know about the existence of the message). In contrast, with a stream socket, the underlying TCP protocol will detect that a message was lost because it was not acknowledged, and it will be retransmitted without the process at either end knowing about this.
  2. Message boundaries are preserved in datagram sockets. If the sender sends a datagram of 100 bytes, the receiver must read all 100 bytes at once. This can be contrasted with a stream socket, where if the sender wrote a 100 byte message, the receiver could read it in two chunks of 50 bytes or 100 chunks of one byte.
  3. The communication is done using special system calls sendto() and receivefrom() rather than the more generic read() and write().
  4. There is a lot less overhead associated with a datagram socket because connections do not need to be established and broken down, and packets do not need to be acknowledged. This is why datagram sockets are often used when the service to be provided is short, such as a time-of-day service.
 State Transition Diagram at Transport Layer:
TCP/UDP and Sockets Study Notes for GATE & Computer Science Engineering Exams


Candidates can also practice 110+ Mock tests for various exams like GATE, NIELIT with BYJU’S Exam Prep Test Series check the given link:

Click Here to Avail GATE CSE Test Series!


Our Apps Playstore
SSC and Bank
Other Exams
GradeStack Learning Pvt. Ltd.Windsor IT Park, Tower - A, 2nd Floor, Sector 125, Noida, Uttar Pradesh 201303
Home Practice Test Series Premium