Computer Network & Network Security System — Application Layer, NEC licence examination syllabus (Nepal Engineering Council).
Every network app you've ever used was built by a programmer calling functions with names like "bind" and "listen."
A socket is the programming interface (an API) that lets an application actually send/receive data over a network — it's the code-level handle you hold onto, which sits on top of everything covered in this whole networking subject.
Server sidecreate socket → bind to a port → listen for connections → accept a connection → send/receive data.
Client sidecreate socket → connect to server's address+port → send/receive data.
TCP sockets (SOCK_STREAM) give you a reliable, connection-oriented byte stream. UDP sockets (SOCK_DGRAM) give you fast, connectionless datagrams — you choose based on the same TCP-vs-UDP tradeoffs covered in the transport layer chapter.
The sequence looks like it has one call too many. It does not, and the reason is the most useful thing in this topic.
A client calls socket() then connect(), and skips bind(), listen() and accept() entirely.
No bindThe OS assigns an ephemeral port automatically. A client does not care which port it uses — only that it is unique — whereas a server must be found at a known one.
No listen or acceptThe client initiates rather than waits, so there is nothing to queue.
TCPsocket → bind → listen → accept, then send/recv on the accepted socket. A connection exists, so neither side repeats the address.
UDPsocket → bind, then sendto/recvfrom. No connection, no listen or accept — every datagram carries its own destination, and recvfrom reports who sent it.
accept() and recv() block by default — the program stops there until something arrives. A single-threaded server that calls recv() on one client cannot accept anyone else meanwhile.
That is why real servers use threads, processes, or an event loop with select/poll. The naive sequence in a textbook handles exactly one client at a time, which is worth stating plainly since the code looks complete and is not.
Create a free account to tick topics off, take notes as you read, watch the video lessons and get a day-by-day study plan built around your exam date.
Loading…