Note folks, the below contents are arbitrary collated information about sockets.
- sockets are said to be connection end points. They are compared to the telephone systems installed present at the end users
- unix treats sockets identical to the files, socket descriptor returned for a socket call is next available file descriptor
- by default sockets are full-duplex communication mode
- seek operation, valid on files cannot be performed on socket
- On closing a socket, remote process gets an EOF indication
- shutdown contrast to close is used to perform partial closure of socket.
HOW | Significance |
0 (SHUT_RD) | No further read allowed |
1 (SHUT_WR) | No further write allowed |
2 (SHUT_RDWR) | No further read and write allowed (equiv close) |
- performing a shutdown of write, makes the kernel to flush all the pending data to be sent, send immediately. Causes an EOF indication, to be sent to the remote socket as an indication that no more data would be sent on this socket
- if the socket descriptor is duplicated then only after last close is that the actual socket closure is performed
- shutdown on a duplicated socket, results on an immediate affect
- sockets created prior to the fork invocation, are duplicated in the child process also
- shutdown of the read on socket, causes any pending read data to be silently ignored.
- int socket (int domain, int connection_type, int protocol);
domain => selects the address family (AF_INET)
connection_type => selects the type of the connection i.e. connected, connectionless
SOCK_DGRAM, SOCK_STREAM, SOCK_RAW
protocol => 0 if selected by default then OS takes care of selecting the protocol type
Generic socket address sizeof 16 bytes
struct sockaddr
{
sa_family_t sa_family;
char sa_data[14];
};
internet socket IPV4
socket sockaddr_in
{
sa_family sin_family;
u16 sin_port;
struct in_addr sin_addr; // holds the IP address in the network byte order
u8 sin_zero[8];
};
internet socket IPV4
socket sockaddr_in
{
sa_family sin_family;
u16 sin_port;
struct in_addr sin_addr; // holds the IP address in the network byte order
u8 sin_zero[8];
};
struct in_addr
{
u32 s_addr;
}
{
u32 s_addr;
}
- network byte order is big endian format. It is fixed to a particular format to attend harmony between various systems using either little or big endian format
- different functions to convert host to network order and vice versa
unsigned long htonl(unsigned long hostlong);
unsigned short htons(unsigned short hostshort);
unsigned long ntohl(unsigned long netlong);
unsigned short ntohs(unsigned short netshort);
- assigning sin_port with zero, implies a request to kernel to assign an local port number for the socket connection
- sin_addr is a byte by byte representation of ip address in the hexal formal. Implies 127.0.0.1 would yield 7F000001
IP Addressing
- IP address follow dotted decimal notation and is of format a.b.c.d where a,b,c,d are unsigned value of a byte
- IP consist of 2 components
2. Host number => identifies the host out of the several on a particular network
- netmask is a value which when bitwise ored with the IP address give the network address of a corresponding network.
- certain network addresses are reserved for private addressing i.e. strictly to be used within an organization
SOCKET contd ....
- inet_addr function converts the ip address from the string format to long
// returns INADDR_NONE if the input ip address is not in the dotted decimal notation
- ipnet_aton is another function that can be used to convert the ip address from string notation to the corresponding long format
- inet_ntoa is a function used to convert the hexal representation to corresponding ip address notation in string
- inet_network converts the ip address to host order hex val
- inet_netof returns the network address in hex for an address
- inet_lnaof returns the host address in hex for an address
- inet_makeaddr returns the ip address in hex, given network addr and host addr in hex
No comments:
Post a Comment