This topic is circulated on the Internet in the same version, that is, the first item is the difference between header files, but the header files listed later are only # include (it is estimated that some people accidentally forgot to escape the angle brackets during the original reprinting process, so that the browser does not parse the HTML Tag). Now I have sorted it out and will continue to add content later.
1) header file
Winsock. h or winsock2.h in Windows
In Linux, netinet/in. H (mostly here), unistd. H (close function here), sys/socket. H (in. h already exists. You can save it)
2) initialization
In Windows, wsastartup must be used to start ws2_32.lib, and # pragma comment (Lib, "ws2_32") must be used to notify the compiler to link this Lib.
Not required in Linux
3) Disable socket
Closesocket (...) in Windows (...)
Close (...) in Linux (...)
4) Type
Socket in Windows
Int in Linux (I like to use long to ensure that it is 4 byte, because-1 I always like to write 0 xFFFF)
5) Get the error code
Getlasterror ()/wsagetlasterror () in Windows ()
In Linux,-1 is returned for a socket operation that fails to be executed. If errno. H is included, the errno variable is set.
6) set non-blocking
Ioctlsocket () in Windows ()
Fcntl () in Linux requires the header file fcntl. h
7) The last parameter of the send Function
In Windows, it is generally set to 0.
In Linux, it is best to set msg_nosignal. If this parameter is not set, it may result in an error in sending.ProgramExit
8) millisecond-level acquisition
Gettickcount () in Windows ()
Gettimeofday () in Linux ()
9) Multithreading
Process. H is included in windows, and _ beginthread and _ endthread are used.
Linux contains pthread. h and uses pthread_create and pthread_exit.
10) define an address with an IP address (difference in the structure of sockaddr_in)
Addr_var.sin_addr.s_un.s_addr in Windows
Addr_var.sin_addr.s_addr in Linux
In addition, the 32bit s_addr in Winsock also has several member variables that share the memory space with it in the form of union (which facilitates assignment in other ways ), the Linux socket does not have this combination, it is a 32bit s_addr. In the format of four char IPS (for example, 127, 0, 0, and 1 ), winsock can be assigned to s_addr using four s_ B values. In Linux, you can assign values by adding edges to the left (eight bits in total.
11) Exception Handling
In Linux, when the connection is disconnected and data is sent, not only will the return value of send () be reflected, but it will also send an exception message like the system. If it is not processed, the system will output brokepipe, the program exits. For this reason, the last parameter of the send () function can be set to msg_nosignal, which prohibits the send () function from sending exception messages to the system.