added types of sockets and their comparison (#30893)
Added information about TCP and UDP protocols and their corresponding sockets ie. stream and datagram sockets. Comparison is also added.
This commit is contained in:
committed by
Randell Dawson
parent
a2bf343c82
commit
2f57c6131c
@ -14,7 +14,16 @@ But, how will it arrive at the apartment? Every apartment has their own unique a
|
||||
|
||||
## Basics of Socket Programming
|
||||
|
||||
Socket programming is a way of connecting two nodes on a network to communicate with each other. One socket (node) listens on a particular port at an IP, while another socket reaches out to the other to form a connection. Server forms the listener socket while client reaches out to the server.
|
||||
Socket programming is a way of connecting two nodes on a network to communicate with each other. One socket(node) listens on a particular port at an IP, while other socket reaches out to the other to form a connection. Server forms the listener socket while client reaches out to the server.
|
||||
|
||||
## Types of Sockets
|
||||
There are two widely used socket types, stream sockets, and datagram sockets. Stream sockets treat communications as a continuous stream of characters, while datagram sockets have to read entire messages at once. Each uses its own communciations protocol. Stream sockets use TCP (Transmission Control Protocol), which is a reliable, stream oriented protocol, and datagram sockets use UDP (Unix Datagram Protocol), which is unreliable and message oriented.
|
||||
|
||||
## Difference between stream and datagram sockets
|
||||
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.
|
||||
|
||||
## I have Socket... Now what?
|
||||
|
||||
|
Reference in New Issue
Block a user