Please enable JavaScript.
Coggle requires JavaScript to display documents.
Chapter 15
Sockets
Beginning Linux Programming, Fourth Edition (Socket…
Chapter 15
Sockets
Beginning Linux Programming, Fourth Edition
Is a communication mechanism that allows client/server systems to be developed either locally, on a single machine, or across networks.
Creating a socket queue
int listen(int socket, int backlog);
Accepting connections
int accept(int socket, struct sockaddr address, size_t address_len);
Naming a socket
int bind(int socket, const struct sockaddr *address, size_t address_len);
Requesting connections
int connect(int socket, const struct sockaddr *address, size_t address_len);
Socket attributes
Scket domains
Specify the network medium that the socket communication will use. The most common socket domain is AF_INET, which refers to Internet Networking that's used on many linux local area networks and internet itself.
Socket types
-
Datagram sockets
Are implemented in the AF_INET domain by UDP/IP connections and provide an unsequenced, unreliable service.
-
-
-
Yo can add your server to the list of known services in /etc/services, which assigns a name to port numbers so that clients can use symbolic services rather than numbers
The fact that the original socket is still available and that sockets behave as file descriptors gives you a method of serving multiple clients at the same time. If the server calls fork to create a second copy of itself, the open socket will be inherited by the new child process. It can then communicate with the connecting client while the main server continues to accept further client connections.
Select
The select system call allows a program to wait for input to arrive (or output to complete) on a number of low-level file descriptors at once. This means that the terminal emulator program can block until there is something to do. Similarly, a server can deal with multiple clients by waiting for a request on many open sockets at the same time.
The daytime service used in getdate.c earlier provides a good example. You create a socket, make a connection, read a single response, and close the connection. That’s a lot of operations just to get the date.
The daytime service is also available by UDP using datagrams. To use it, you send a single datagram to the service and get a single datagram containing the date and time in response. It’s simple.
-