Socket is one of the IPC (inter process communication) for communication mechanism, either locally on a single machine or across the network. There is 2 type of socket that is TCP and UDP.
Both TCP and UDP come under Transport layer.
UDP Vs TCP
TCP:
- Connection Oriented
- Connection Persistent
- 2 way communication
- Re-transmission
- Reliable
- Acknowledgement
- Sequential
- No Duplication
- byte stream (similar to Input/Output streams).
UDP:
- No Connection (Connection less)
- No Acknowledgement
- No Re-transmission
- Unreliable (packet may lost so no guaranteed)
- Out of order
- Duplication possible
- Quick look up
- Single query reply action
- Fast (no connection is required)
Key Point of UDP:
- When overhead of establishing and maintaining a socket connection is not necessary.
- Short query to server and expect a single respond.
- Allowing OS to hold the incoming request in a queue.
- On local area network, UDP is a very reliable.
TCP API:
Socket:
- Create new communication end point.
- create socket that cannot be shared.
Bind:
- Naming the socket (using IP address)
- Assign a local address to socket.
Listen:
- Create a queue for incoming connections.
- To accept incoming connection on socket.
Accept:
- Its blocker call (until connection arrive (If there is no connection pending on queue, accept will block, until a client does make connection))
- It created dedicate communication for each client.
- It create “New Socket” to communicate with specific client and return its descriptor.
- This new socket is created that is distinct from named socket (this socket is solely for communication with particular client).
- Client will wait on queue until server is ready.
Connect:
- It establish connection.
- It will wait on queue until server is ready.
send:
- It is used to send the packet.
receive:
- It is used to receive the packet
UDP API:
Socket:
- Create new communication end point.
Bind:
- bind is need for receiving only.
- If we want to send the data only then bind is NOT needed.