Recently wrote a program about sockets that need to be ported from Windows to Linux. Now the useful stuff to collect and record down.
1. Header files
Windows under Winsock.h or winsock2.h;
Linux under Netinet/in.h (mostly here), unistd.h (Close function here), sys/socket.h (already contained in In.h, can be saved).
2. Initialization
Windows needs to use WSAStartup to start ws2_32.lib, and to use #pragma comment (lib, "Ws2_32") to tell the compiler to link to the Lib;
Linux is not required.
3. Close the socket
Under Windows: Closesocket ()
Under Linux: Close ()
4. Type
Under Windows: SOCKET
Linux: Int (also available long, so that 4byte,-1 can be written as 0xFFFF)
5. Get the error code
Windows under GetLastError ()/wsagetlasterror ();
Under Linux, a socket operation that failed to execute successfully returns 1, and if Errno.h is included, the errno variable (strerror (errno)) is set.
6. Set non-blocking
Under Windows: Ioctlsocket ()
Linux under: Fcntl () (Requires header file fcntl.h)
The last parameter of the 7.send function
The general setting under Windows is 0;
Linux is best set to msg_nosignal, if not set, after sending an error may cause the program to exit.
8. Millisecond-time acquisition
Under Windows: GetTickCount ()
Linux under: Gettimeofday ()
9. Multithreading
Windows contains Process.h, using _beginthread and _endthread, or createthread, etc.
Linux contains Pthread.h, using Pthread_create and Pthread_exit
For example:
Windows:
if 0, Processtread, (void0, &dwthreadid) = = NULL) {printf (" Error:createthread failed! \ n"); return -1
Linux:
if (Pthread_create (&thread, NULL, Processtread, (void*) &Clientsockinf)) { printf (" Error:createthread failed! \ n") ; return -1;}
Compile with parameter-pthread: for example g++ ftpserver.cpp-pthread
10. Define an address with IP (the difference between the structure of the sockaddr_in)
Under Windows: Addr_var.sin_addr. S_un. S_addr
Linux under: addr_var.sin_addr.s_addr
And the last 32bit s_addr in Winsock has several member variables that share memory space in the form of Union (which facilitates other assignments), and the socket for Linux does not have this union, which is a 32bit s_addr. In the form of an IP that got 4 char (i.e. 1271, 01, 01, and 11 Total char), Winsock can be assigned to S_ADDR directly with four S_b, while under Linux it can be shifted to the left by the side (8bit, a total of four) The method of adding the edges is assigned a value.
11. Exception Handling
Linux, when the connection is disconnected, the data is sent, not only the return value of Send () will be reflected, but also like the system sends an exception message, if not processed, the system will be out of Brokepipe, the program will exit. To do this, the last parameter of the Send () function can be set to msg_nosignal, which prevents the Send () function from sending an exception message to the system.
12. Compare the first n characters of two strings
windows:stricmp,strnicmp
linux:strcasecmp,strncasecmp
13. Functions related to the current working directory
Windows:getcurrentdirectory (get current working directory), SetCurrentDirectory (change current working directory)
Linux:getcwd,chdir
14.setsockopt function
Windows:
bool optval = true;setsockopt (s,sol_socket,so_reuseaddr, (Char*) &optval,sizeof (optval))
Linux:
struct timeval optval;setsockopt (s,sol_socket,so_reuseaddr, (Char*) &optval,sizeof( Optval))
15. About the path separator "/" and "\":
The "\" path delimiter is not supported on Linux and needs to be changed to "/"
When a Windows program is ported to Linux, two operating systems are very different in how many data types are named, such as u+x for unsigned data types in Windows, like uint, UCHAR, ULONG, and Linux on a regular basis, are unsigned +x; In addition, there are some data types that Windows has, while Linux does not, like DWORD, HANDLE, LPSTR, and so on. So here is a more useful header file that can be downloaded.
C and C + + Windows code porting to linux more useful stuff: http://download.csdn.net/detail/lsmallstop/7747981
It contains two: a Windows data type in Linux for the conversion of the header file, when needed to directly introduce this header file (#include "WinToLinux.h"), more practical. The other is a document description that is ported from Windows to Linux for C and C + + code, and is also useful.