Skip to content

External functions

Iker González edited this page Dec 1, 2022 · 4 revisions

Definition

They are used to convert values between host and network byte order.

host byte order =====> network byte order

- htonl: converts the unsigned integer hostlong from host byte order to network byte order.

- htons: converts the unsigned short integer hostshort from host byte order to network byte order.

network byte order =====> host byte order

- ntohl: converts the unsigned integer netlong from network byte order to host byte order.

- ntohs: converts the unsigned short integer netshort from network byte order to host byte order.

Definition

Allows a program to monitor multiple file descriptors, waiting until one or more of the file descriptors become "ready" for some class of I/O operation (e.g., input possible). A file descriptor is considered ready if it is possible to perform a corresponding I/O operation.

File descriptor sets

The principal arguments of select() are three "sets" of file descriptors (declared with the type fd_set), which allow the caller to wait for three classes of events on the specified set of file descriptors. Each of the fd_set arguments may be specified as NULL if no file descriptors are to be watched for the corresponding class of events.

The contents of a file descriptor set can be manipulated using the following macros:

- FD_SET: adds the file descriptor fd to set. - FD_CLR: removes the file descriptor fd from set. - FD_ISSET: returns nonzero if the file descriptor fd is present in set, and zero if it is not. - FD_ZERO: clears/removes all file descriptors from set.

Arguments

- readfds: the file descriptors in this set are watched to see if they are ready for reading. A file descriptor is ready for reading if a read operation will not block.

- writefds: the file descriptors in this set are watched to see if they are ready for writing. A file descriptor is ready for writing if a write operation will not block. A large write may still block even if it is writable.

- exceptfds: the file descriptors in this set are watched for "exceptional conditions".

- nfds: the indicated number of file descriptors in each set that are checked. This argument should be set to the highest-numbered file descriptor in any of the three sets, plus 1.

- timeout: timeval structure that specifies the interval that select() should block waiting for a file descriptor to become ready.

Return value

  • Number of file descriptors contained in the three returned descriptor sets (read, write, except).
  • 0 if the timeout expired before any file descriptors became ready.
  • -1 on error.

After select() has returned, read, write and except fds are cleared except for those that are ready to perform the corresponding I/O operation.

Performs a similar task to SELECT: it waits for one of a set of file descriptors to become ready to perform I/O. The Linux-specific epoll API performs a similar task, but offers features beyond those found in poll().

Monitors multiple file descriptors to see if I/O is possible on any of them.

The central concept of the epoll API is the epoll instance, an in-kernel data structure which, from a user-space perspective, can be considered as a container for two lists:

  • Interest list: the set of file descriptors that the process has registered an interest in monitoring.
  • Ready list: the set of file descriptors that are "ready" for I/O. It is a subset of the interest list.

The following system calls are provided to create and manage an epoll instance:

  • epoll_create: creates a new epoll instance and returns a file descriptor referring to that instance.
  • epoll_ctl: registers interest in particular file descriptors, adding the items to the interest list of the epoll instance.
  • epoll_wait: waits for I/O events, blocking the calling thread if no events are currently available.

Definition

It is used to manipulate file descriptors.

int socket(int domain, int type, int protocol);  
  • domain : communication domain AF_UNSPEC; // use IPv4 or IPv6, whichever

  • type : communication type. SOCK_STREAM: TCP(reliable, connection oriented) SOCK_DGRAM: UDP(unreliable, connectionless)

  • protocol: Protocol value for Internet Protocol(IP), which is 0.

  • bind:

Associate that socket with a port on your local machine

sockfd: socket file descriptor returned by socket(). struct sockaddr that contains information about your address. (port and IP address) addrlen is the length in bytes of that address_. sizeof(struct sockaddr)_

int bind(int sockfd, struct sockaddr *my_addr, int addrlen); SOCKADDR Holds socket address information for many types of sockets.

struct sockaddr_in {
    short int          sin_family;  // Address family, AF_INET
    unsigned short int sin_port;    // Port number
    struct in_addr     sin_addr;    // Internet address
    unsigned char      sin_zero[8]; // Same size as struct sockaddr
};
  • sin_family = AF_INET

  • sin_port = htons(port)

  • sin_addr =htons(IP)

  • sin_zero = memset()

  • accept:

  • listen:

  • send:

  • recv:

  • bind: Associate that socket with a port on your local machine

  • [ ]

  • connect:

  • inet_addr:

  • setsockopt: