This document explains the basics of Linux socket programming, including the concept of a socket and byte order, as well as some common structures and functions.
This article is logged in the NetEase cloud classroom learning process, the teacher is pretty good, I recommend onlookers.
Linux Network Programming Linux Network Programming | Things Artificial Intelligence
1. Concept
1.1 socket (socket)
Socket (socket) is a communications mechanism that contains definitions call interface and data structure of a set, it gives application process provides a means such as TCP / UDP network protocols for network communication.
The Linux network programming through socket interface, socket both a special IO, to provide the corresponding file descriptor. A complete socket has an associated description, referred to quintuple (protocol, local address, local port, remote address, remote port), each socket has a unique local socket, assigned by the operating system.
1.2 endian
Into big-endian byte order and little-endian, network using a network protocol i.e. big endian byte order.
Byte internal network byte order and a sequence of different machines to the data conversion.
2 endian conversion function used
uint32_t htonl(uint32_t hostlong); |
A 32-bit integer will be converted by the host to network byte order byte sequence |
uint16_t htons(uint16_t hostshort); |
A 16-bit integer will be converted by the host to network byte order byte sequence |
uint32_t ntohl(uint32_t netlong); |
A 32-bit integer in network byte order by a switch to a host byte sequence |
uint16_t ntohs(uint16_t netshort); |
A 16-bit integer will be converted by the host to network byte order byte sequence |
3 Data Structure
3.1 General address structure (not generally used, but the kernel is used, it is necessary to cast such a data structure)
#include <sys/socket.h>
struct sockaddr{
unsigned short sa_family;
char sa_data [14];
};
sa_data: contains the number of addresses, the port number of the remote computer and the socket, inside it is mixed together with the data.
sa_family: general use IPv4 AF_INET.
When the function is passed to the address structure required, converting into a pointer to the structure struct sockaddr * passing in.
3.2 Internet address structure
struct in_addr{
in_addr_t s_addr // IPv4 address
};
struct scokaddr_in{
short int sin_family; // Internet address family AF_INET as (host byte order)
unsigned short int sin_port; // port number, 16bit (Network Byte Order)
struct in_addr sin_addr; // Internet address, 32bit IPv4 address (Network Byte Order)
unsigned char sin_zero [8]; // add 0 (padding bits for format thereof)
};
Address and Internet address of generic structure equivalent structure data type, can be converted each other, generally more convenient to use sockaddr_in.
4. The basic functions
4.1 Creating socket
#include <sys/socket.h>
int socket(int domain, int type, int protocal)
Returns: successful returns a file descriptor, error return -1.
Create a socket in the kernel, if successful return created kernel socket descriptor file description table.
parameter:
Domain: AF_INET IPv4 Internet domain , AF_INET6 IPv6 Internet domain, AF_UNIX unix domain, AF_UNSPEC not specified.
protocol: generally 0, expressed as a given field to select the default protocol and socket type
type: SOCK_STREAM stream socket, provide a reliable, connection-oriented traffic flows, which uses the TCP protocol, TCP sequence and ensure the accuracy of data transmission.
SOCK_DGRAM datagram socket, defines a connectionless service, data is transferred through packets independently, is disordered, and does not guarantee reliable, error-free, using a datagram protocol UDP protocol. SOCK_RAW raw socket, such as IP ICMP allows direct access to the low-level protocol or mainly for testing new network protocol, and the like. SOCK_SEQPACKET fixed length, orderly, reliable connection-oriented message delivery.
Transfer function between 4.2.IPv4 address family and address character
#include <arp/inet.h>
Function: the network byte order conversion site decimal
const char* inet_ntop(int domain, const void *restrict_addr, char *restrict_str, socklen_t size);
Function: the decimal point is converted to network byte order
const char* inet_pton(int domain, const void *restrict str, char *restrict addr);
parameter:
domain: Internet address family, such as AF_INET
addr: Internet address, 32-bit IPv4 address (Network Byte Order)
str: string address (dotted decimal) Pointer
size: size of the address string
4.3 Fill IPv4 address family structure Case
struct sockaddr_in sin; // define a structure sockaddr_in
char buf[16];
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET; // fill in the Internet address family
sin.sin_port = htons ((short) 3001); // Fill port number (Network Byte Order)
//填写sin_addr
if (inet_pton(AF_INET, "192.168.2.1", &sin.sin_addr.s_addr) <=0)
{
//错误处理
}
printf("%s\n", inet_ntop(AF_INET, &sin.sin_addr.s_addr, buf, sizeof(buf)));