My colleague told me I need to create socket file for the AF_INET
Your colleague is wrong. Taking a look at manpages of bind(2)
you can see that different socket types have rules:
The rules used in name binding vary between address families. Consult
the manual entries in Section 7 for detailed information. For AF_INET
,
see ip(7)
; for AF_INET6
, see ipv6(7)
; for AF_UNIX
, see packet(7)
; for AF_X25
, see x25(7)
; and for AF_NETLINK
, see netlink(7)
.
You will see at ip(7)
that there is no invocation involving AF_INET
that can create a file. There is also this great IBM paper about this socket structure and some history about it. Legacy structure form(BSD 4.4/Unix 98):
struct sockaddr_in {
uint8_t sin_len;
sa_family_t sin_family;
u_short sin_port;
struct in_addr sin_addr;
char sin_zero[8];
};
Current struct:
struct sockaddr_in {
sa_family_t sin_family; /* address family: AF_INET */
in_port_t sin_port; /* port in network byte order */
struct in_addr sin_addr; /* internet address */
};
/* Internet address. */
struct in_addr {
uint32_t s_addr; /* address in network byte order */
};
See, no mention of file creation whatsoever.
Quoting other part of ip(7)
manpages about how this kind of socket(AF_INET
) works:
When a process wants to receive new incoming packets or connections,
it should bind a socket to a local interface address using bind(2)
. In
this case, only one IP socket may be bound to any given local
(address, port) pair. When INADDR_ANY
is specified in the bind call,
the socket will be bound to all local interfaces. When listen(2)
is
called on an unbound socket, the socket is automatically bound to a
random free port with the local address set to INADDR_ANY
. When
connect(2)
is called on an unbound socket, the socket is automatically
bound to a random free port or to a usable shared port with the local
address set to INADDR_ANY
.
--snip--
Address format
An IP socket address is defined as a combination of an IP interface
address and a 16-bit port number. The basic IP protocol does not
supply port numbers, they are implemented by higher level protocols
like udp(7) and tcp(7). On raw sockets sin_port is set to the IP
protocol.
But if you look at unix(7)
manpages you will see some examples of AF_UNIX
socket type and its basic scructure. There is even a field called sun_path
that is the path for socket files(see Related Stuff below):
#define UNIX_PATH_MAX 108
struct sockaddr_un {
sa_family_t sun_family; /* AF_UNIX */
char sun_path[UNIX_PATH_MAX]; /* pathname */
};
tl,dr: AF_UNIX
is for sockets(and they use files) while AF_INET
is for binding to ip addresses, and creating communications on its various forms(unicast, multicast, broadcast...).
Related Stuff: