Ceng204 w7 Systems Programming2024 Spring
Ceng204 w7 Systems Programming2024 Spring
3
Servers and sockets
Socket programming
The processes that use a socket can reside on the
same system or different systems on different
networks.
Sockets are useful for both stand-alone and network
applications.
Sockets allow you to exchange information between
processes on the same machine or across a
network, distribute work to the most efficient
machine, and they easily allow access to centralized
data.
4
Servers and sockets
Socket programming
Socket application program interfaces (APIs) are the
network standard for TCP/IP.
A wide range of operating systems support socket
APIs.
Socket system functions and the socket network
functions are threadsafe.
5
Servers and sockets
How sockets work
• Sockets are commonly used for client and server interaction.
• Typical system configuration places the server on one
machine, with the clients on other machines.
• The clients connect to the server, exchange information, and
then disconnect.
6
Servers and sockets
How 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 client-to-server data exchange takes place when a client
connects to the server through a socket.
• The server performs the client's request and sends the reply
back to the client.
7
Servers and sockets
The figure shows the
typical flow of events
(and the sequence of
issued APIs) for a
connection-oriented
socket session.
8
Servers and sockets
1. The socket() API
creates an endpoint for
communications and
returns a socket
descriptor that
represents the endpoint.
9
Servers and sockets
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.
10
Servers and sockets
3. The listen() API indicates a
willingness to accept client
connection requests. When
a listen() API 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() API
and the bind() API binds a
name to the socket.
A listen() API must be issued
before an accept() API is
issued.
11
Servers and sockets
4. The client application
uses a connect() API on
a stream socket to
establish a connection to
the server.
12
Servers and sockets
5. The server application
uses the accept() API to
accept a client
connection request. The
server must issue
the bind() and listen() AP
Is successfully before it
can issue
an accept() API.
13
Servers and sockets
6. When a connection is
established between
stream sockets
(between client and
server), you can use any
of the socket API data
transfer APIs. Clients
and servers have many
data transfer APIs from
which to choose, such
as send(), recv(), read(),
write(), and others.
14
Servers and sockets
7. When a server or
client wants to stop
operations, it issues
a close() API to release
any system resources
acquired by the socket.
15
Servers and sockets
Stages for Server
The server is created using the following steps:
1. Socket Creation: int sockfd = socket(domain, type, protocol)
sockfd: socket descriptor, an integer (like a file handle)
domain: integer, specifies communication domain. AF_ LOCAL as
defined in the POSIX standard for communication between processes
on the same host, on different hosts AF_INET(IPV4) and AF_I NET 6
(IPV6) are used.
type: communication type
SOCK_STREAM: TCP(reliable, connection-oriented)
SOCK_DGRAM: UDP(unreliable, connectionless)
protocol: Protocol value for Internet Protocol(IP), which is 0. This is
the same number that appears on the protocol field in the IP header of
a packet.(man protocols for more details)
16
Servers and sockets
Stages for Server
The server is created using the following steps:
2. Setsockopt
This helps in manipulating options for the socket referred by the file
descriptor sockfd. This is completely optional, but it helps in reuse of
address and port. Prevents error such as: “address already in use”.
int setsockopt(int sockfd, int level, int optname, const void *optval,
socklen_t optlen);
17
Servers and sockets
Stages for Server
The server is created using the following steps:
3. Bind
int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
After the creation of the socket, the bind function binds the socket to
the address and port number specified in addr(custom data structure).
In the example code, we bind the server to the localhost, hence we use
INADDR_ANY to specify the IP address.
18
Servers and sockets
Stages for Server
The server is created using the following steps:
4. Listen
int listen(int sockfd, int backlog);
It puts the server socket in a passive mode, where it waits for the client
to approach the server to make a connection. The backlog, defines the
maximum length to which the queue of pending connections for sockfd
may grow. If a connection request arrives when the queue is full, the
client may receive an error with an indication of ECONNREFUSED.
19
Servers and sockets
Stages for Server
The server is created using the following steps:
5. Accept
int new_socket= accept(int sockfd, struct sockaddr *addr, socklen_t
*addrlen);
It extracts the first connection request on the queue of pending
connections for the listening socket, sockfd, creates a new connected
socket, and returns a new file descriptor referring to that socket. At this
point, the connection is established between client and server, and they
are ready to transfer data.
20
Servers and sockets
Stages for Client
1. Socket connection: Exactly the same as that of server’s socket
creation
2. Connect: The connect() system call connects the socket referred to
by the file descriptor sockfd to the address specified by addr. Server’s
address and port is specified in addr.
int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
21
References
• https://www.ibm.com/docs/en/i/7.3?topic=programming-how-sockets-work
• https://www.geeksforgeeks.org/socket-programming-cc/
• https://inst.eecs.berkeley.edu/~cs162/su19/static/lectures/10.pdf
22