UDP uses two functions in the sendto () and a recvfrom ()

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/leon_zeng0/article/details/90140605

Function prototype

int sendto (int s, const void *buf, int len, unsigned int flags, const struct sockaddr *to, int tolen);

int recvfrom(int s, void *buf, int len, unsigned int flags, struct sockaddr *from, int *fromlen);

Function Description

sendto (), the UDP datagram is sent to the specified address; recvfrom () is the received UDP datagram from the specified address.

Parameter Description

s: socket descriptor.
buf: UDP datagram buffer address.
len: UDP datagram length.
flags: This parameter is usually 0.
to: sendto () function parameters, struct sockaddr_in type, indicating where to send UDP data packets.
tolen: address length the other, generally: sizeof (struct sockaddr_in).
fromlen: recvfrom () function parameters, struct sockaddr_in type, indicating where the received UDP datagram.
Function return value

For the sendto () function, the number of characters actually sent out successfully is returned, return -1 failure, the cause of the error stored in errno.

For recvfrom () function, if successful returns the number of characters received, -1 on failure, the cause of the error stored in errno.

struct sockaddr_in structure

Quote from: https://blog.csdn.net/angle0615303/article/details/7657267

First, the structure struct sockaddr_in, struct sockaddr, struct in_addr

struct sockaddr_in, struct sockaddr, struct in_addr, which is commonly used in network programming structure, always remember what their individual members, need temporary check, convenient for viewing later, summarized here under.

the sockaddr {struct
unsigned Short sa_family like; / * address family, AF_xxx * /
char sa_data [14]; / * 14-byte protocol address * /
};

The above are generic socket address, specific to the Internet socket, with the following structure, both types may be converted
struct {the sockaddr_in
Short int the sin_family; / * address family * /
unsigned int The sin_port Short; / * port number * /
struct in_addr sin_addr ; / * the Internet address * /
unsigned char sin_zero for [. 8]; / * struct sockaddr with the same length * /
};

struct in_addr is the 32-bit IP address.
in_addr {struct
unsigned Long s_addr;
};

There
struct in_addr {
Union {
struct {u_char S_B1, s_b2, s_b3, s_b4;} S_un_b;
struct {u_short s_w1, s_w2;} S_un_w;
u_long s_addr; // members s_addr elongated shaping structure
} S_un;
};
using u_long htonl ( u_long hostlong); host byte sequence is converted to TCP / IP network byte order.
using u_short htons (u_short hostshort); converting the byte order for the host TCP / IP network byte order.

the inet_addr () is a point-point IP address (e.g., 192.168.0.1) is converted to 32-bit IP address (0xC0A80001) required in the above-described configuration.

The generic form is:
int sockfd;
struct sockaddr_in my_addr;
sockfd = socket (AF_INET, SOCK_STREAM, 0); / * do some error checking * /!

my_addr.sin_family = AF_INET; / * host byte order * /
my_addr.sin_port = the htons (MyPort); / * Short, network byte order * /
my_addr.sin_addr.s_addr the inet_addr = ( "192.168.0.1");

bzero (& (my_addr.sin_zero), 8); / * ZERO at The REST of at The struct * /
/ * do not forget to do error checking to the bind (): * /
the bind (sockfd, (struct sockaddr *) & my_addr, sizeof ( struct sockaddr));

二, inet_addr, inet_aton, inet_ntoa

inet_addr: The network address into a binary digital network, the IP address returned is the order of the network. Function prototype: unsigned long in inet_addr (const char * cp)

inet_aton: the network address of the network into a binary number, the difference between inet_addr is not the result as a return value, but stored parameter in_addr structure referred to in inp. Function prototype: int inet_aton (cont char * cp, struct in_addr * inp)

inet_ntoa: binary digital network into a network address, the function prototype is: char * inet_ntoa (struct in_addr in)

Third, there are two update functions inet_pton and inet_ntop

These two functions can be processed ipv4 and ipv6, prototype is

int inet_pton (int af, const char * src, void * dst);
This function converts the string to the network address, the first parameter is the address family af, is present after conversion of dst

inet_pton is inet_addr extension, support multiple address families with the following:

AF_INET: first address to point to the character address src, i.e. ASCII address (ddd.ddd.ddd.ddd format), the address translation function to the structure in_addr and the copy * dst

AF_INET6: rc ,, the IPV6 address to point to the address translation function to the structure in6_addr and the copy * dst

If the function returns a negative error, errno set EAFNOSUPPORT, if the parameters specified by the address family af and src wrong format, the function returns 0.

Function inet_ntop vice-versa following prototype
const char * inet_ntop (int af, const void * src, char * dst, socklen_t cnt);
This function converts the network binary structure to effect ASCII type of address, parameters, and the same as above, but more a parameter socklen_t cnt, he is the buffer size dst point, to avoid overflow, if the buffer is too small to store the address value, a null pointer is returned, with errno set to ENOSPC
 

Guess you like

Origin blog.csdn.net/leon_zeng0/article/details/90140605
Recommended