Socket—— Network IPC(1st)


Socket—— Network IPC(1st)


When I study the source code of wrk, I found that I need to learn about Socket. Socket is a very very important concept

I recommend you to read Advanced Programming in the UNIX Environment. This book is very classic.


Socket is one method of many IPCs like pipes, FIFOs, message queues, semaphores , and shared memory.

But Socket can run in different computers to communicate with each other——called Network IPC.

One of the design goals of the socket interface is to make processes to communicate with other process, regardless of where they are running in the same machine or different machines. Of course, Socket can use many kinds of network protocols but here I restrict our disscussion to the TCP/IP protocol because of it's common use.

What is Socket?

A socket can be recognized as a abstraction of a communication endpoint. You can think of a "bank counter" where if you go to the bank and you must stand in front of. By the "bank counter", you can communicate with the bank teller.

In the UNIX System, a file is described as a number called "file descriptor". Just as file descriptor, applications use socket descriptors to access sockets. "Sockets descriptors are implemented as file descriptors in the UNIX System".

So you can use many functions that works in file descriptos the same in socket descriptors, such as read and write.

To create a socket, use function “socket()” , before that you should include socket.h header file:

#include
int socket(int domain, int type, int protocol); // if created successfully, return -1, negetive one.

There are four domains in this function:

AF_INET //IPv4
AF_INET6 //IPv6
AF_UNIX //UNIX, refer to APUE Section 17.3
AF_UNSPEC //unspecified, represents any domain.

Generally we use IPv4 more.

The second parameter--type, determines the type of the socket:

Type Description
SOCK_DGRAM fixed-length, connectionless, unreliable messages
SOCK_RAW datagram interface to IP (optional in POSIX.1)
SOCK_SEQPACKET fixed-length, sequenced, reliable, connection-oriented messages
SOCK_STREAM sequenced, reliable, bidirectional, connection-oriented byte streams

You can also add your addtional types freely.

The protocol argument usually is zero. Zero represents that default protocols are used by the given domain and type. Usually, the default protocols are muliple. So If you just want one , you can enter a particular parameter for single.

"socket(AF_NET, SOCK_STREAM, 0)" means it uses TCP(Transmission Control Protocol). And "Socket(AF_INET, SOCK_DGRAM)" means it uses UDP(User Datagram Protocol).

Sending a datagram with SOCK_DGRAM is analogous to sending a mail to somebody. The letters may lost the delivery and be out of order. You can't guarantee the stability of the connection. Each datagram contains the independent address of recipient that can even go to different recipients.

In contrast, the connection-oriented protocol is like making a phone call. First you must establish a connection call. The connection is a peer-to-peer. You and your communicater share the channel, you say a letter and he returns you a letter. each of your words contains no addressing information.

With using a SOCK_STREAM socket, the socket provides a byte stream service. So when we read data from the socket, may get different bytes number from the process sending us data because the number of bytes that are readed will increase over time.

A SOCK_SEQPACKET socket provides a message-based service instead of a byte-stream service. It means that we can get the same size messages from who sends to us. The socket use Stream Control Transmission Protocol(SCTP).

A SOCK_RAW socket provides a interface for datagram directly to the underlying network layer.

Function Behavior with socket
close (Section 3.3) deallocates the socket
dup , dup2 (Section 3.12) duplicates the file descriptor as normal
fchdir (Section 4.22) fails with errno set to ENOTDIR
fchmod (Section 4.9) unspecified
fchown (Section 4.11) implementation defined
fcntl (Section 3.14) some commands supported, including F_DUPFD, F_GETFD, F_GETFL, F_GETOWN, F_SETFD, F_SETFL, and F_SETOWN
fdatasync , fsync (Section 3.13) implementation defined
fstat (Section 4.2) some stat structure members supported, but how left up to the implementation
ftruncate (Section 4.13) unspecified
getmsg , getpmsg (Section 14.4) works if sockets are implemented with STREAMS (i.e., on Solaris)
ioctl (Section 3.15) some commands work, depending on underlying device driver
lseek (Section 3.6) implementation defined (usually fails with errno set to ESPIPE)
mmap (Section 14.9) unspecified
poll (Section 14.5.2) works as expected
putmsg , putpmsg (Section 14.4) works if sockets are implemented with STREAMS (i.e., on Solaris)
read (Section 3.7) and readv (Section 14.7) equivalent to recv (Section 16.5) without any flags
select (Section 14.5.1) works as expected
write (Section 3.8) and writev (Section 14.7) equivalent to send (Section 16.5) without any flags

Destroy the socket——The Communication on a socket is bidirectional and we can use "shutdown" function to end the socket I/O.

#include

int shutdown(int sockfd, int how); //return 0 OK or other error.

Detailed usage can be getted from APUE.