How do sockets work?
An Introduction to Socket Programming
(Continued from previous question...)
How do sockets work?
A socket has a typical flow of events. In a connection-oriented client-to-server
model, the socket on the server process waits for requests from a client. To do this,
the server first establishes (binds) an address that clients can use to find the server.
When the address is established, the server waits for clients to request a service.
The server performs the client’s request and sends the reply back to the client. The
two endpoints establish a connection, and bring the client and server together. The
client-to-server data exchange takes place when a client connects to the server
through a socket.
1. The socket() function creates an endpoint for communications and returns a
socket descriptor that represents the endpoint.
2. When an application has a socket descriptor, it can bind a unique name to the
socket. Servers must bind a name to be accessible from the network.
3. The listen() function indicates a willingness to accept client connection
requests. When a listen() is issued for a socket, that socket cannot actively
initiate connection requests. The listen() API is issued after a socket is allocated
with a socket() function and the bind() function binds a name to the socket. A
listen() function must be issued before an accept() function is issued.
4. The client application uses a connect() function on a stream socket to establish
a connection to the server.
5. Servers use stream sockets to accept a client connection request with the
accept() function. The server must issue the bind() and listen() functions
successfully before it can issue an accept() function to accept an incoming
connection request.
6. When a connection is established between stream sockets (between client and
server), you can use any of the socket API data transfer functions. Clients and
servers have many data transfer functions from which to choose, such as
send(), recv(), read(), write(), and others.
7. When a server or client wants to cease operations, it issues a close() function to
release any system resources acquired by the socket.
(Continued on next question...)
Other Interview Questions
|