Try to write down your 1.1 million drops-
1. cross-platform development
1) header file inclusion
Use the define switch to include different libraries and define function types
# Ifndef WIN32
# Include "socket_posix.h"
# Else/* WIN32 */
# Include "socket_win32.h"
# Endif /*! WIN32 */
The applicability of if should be indicated at the end.
2) replace functions
# Undef errno
# Define errno WSAGetLastError ()
# Define read (fd, buf, size) recv (fd, buf, size, 0)
2. Common macros
Today we see a very beautiful application:
Macro # is used to initialize IPv4 and IPv6 address information.
# Define FILL_SOCKADDR (AF_INET, sockaddr_in, sin, s ,\
Host, port, count, addrlen, addrs )\
Do \
{\
Struct sockaddr_in * addr ;\
Int I ;\
\
Addrlen = sizeof (struct sockaddr_in );\
\
Addr = (struct sockaddr_in *) calloc (count, addrlen );\
For (I = 0; I <count; ++ I )\
{\
Addr [I]. sin ##_ family = AF_INET ;\
Addr [I]. sin ##_ port = port ;\
Memcpy (& addr [I]. sin ##_ addr. s ##_ addr ,\
ADDR (host, I), host-> h_length );\
}\
\
Addrs = (char *) addr ;\
}\
While (0)
# Define fill_sockaddr (host, port, count, addrlen, addrs )\
FILL_SOCKADDR (AF_INET, sockaddr_in, sin, s ,\
Host, port, count, addrlen, addrs)
# Ifdef AF_INET6
# Define fill_sockaddr6 (host, port, count, addrlen, addrs )\
FILL_SOCKADDR (AF_INET6, sockaddr_in6, sin6, s6 ,\
Host, port, count, addrlen, addrs)
# Endif/* AF_INET6 */
From a Mao pig