The difference between socket programming under windows and linux<Reprint>

If there is no other explanation, the Linux mentioned in this article refers to the 2.6 kernel Linux and the GCC compiler, and the Windows refers to the Windows XP system and the Visual Studio 2005 sp1 compilation environment.
The following is from the reposted blog park

The following is roughly divided into several aspects:

Linux to include

[cpp]

#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>

and other header files, while under windows it contains

[cpp]
#include <winsock.h>

.

The socket in Linux is an integer, and in Windows it is a SOCKET.
Close the socket in Linux and closesocket in Windows.
There is a variable socklen_t in Linux, and it is directly int in Windows.
Because the socket in Linux is the same as the ordinary fd, you can use read and write directly when sending and receiving data in the TCP socket. Windows can only use recv and send.
Set socket options, such as setting the socket to be non-blocking. Under Linux, it is

[cpp]

flag = fcntl (fd, F_GETFL);
fcntl (fd, F_SETFL, flag | O_NONBLOCK);

, under Windows it is
[cpp]

flag = 1;
ioctlsocket (fd, FIONBIO, (unsigned long *) & amp;flag);

.
When a non-blocking socket TCP connection is in progress, the error number for Linux is EINPROGRESS and the error number for Windows is WSAEWOULDBLOCK.

file
Under Linux, the file newline is “\\
“, while under Windows it is “\r\\
“.
Under Linux, the directory separator is “/”, while under Windows it is “”.
Under both Linux and Windows, you can use stat calls to query file information. However, Linux only supports 2G size, while Windows only supports 4G size. In order to support larger file query, you can add

_FILE_OFFSET_BITS=64 is defined, and is called using _stat64 under Windows. The input parameter is struct __stat64.
In Linux, the file type can be determined based on the st_mode of stat. There are macros such as S_ISREG and S_ISDIR. There is no such macro in Windows. You need to define the corresponding macro yourself, such as

[cpp]

#define S_ISREG(m) (((m) & amp; 0170000) == (0100000))
#define S_ISDIR(m) (((m) & amp; 0170000) == (0040000))

Deleting files in Linux is unlink, and in Windows it is DeleteFile.

time

In Linux, the time_t structure is a long integer. In Windows, the time_t structure is a 64-bit integer. If you want time_t to be a 32-bit unsigned integer in Windows, you can add a macro definition, _USE_32BIT_TIME_T.
In Linux, the unit of sleep is seconds. In Windows, the unit of Sleep is milliseconds. That is, sleep (1) is required under Linux, and Sleep (1000) is required under Windows environment.
The timecmp macro in Windows does not support greater than or equal to or less than or equal to.
There is no addition and subtraction macro for the struct timeval structure in Windows, and it needs to be defined manually:

[cpp]

#define MICROSECONDS (1000 * 1000)
  
#define timeradd(t1, t2, t3) do {<!-- --> \
  (t3)->tv_sec = (t1)->tv_sec + (t2)->tv_sec; \
  (t3)->tv_usec = (t1)->tv_usec + (t2)->tv_usec % MICROSECONDS; \
  if ((t1)->tv_usec + (t2)->tv_usec > MICROSECONDS) (t3)->tv_sec + + ; \
} while (0)
  
#define timersub(t1, t2, t3) do {<!-- --> \
  (t3)->tv_sec = (t1)->tv_sec - (t2)->tv_sec; \
  (t3)->tv_usec = (t1)->tv_usec - (t2)->tv_usec; \
  if ((t1)->tv_usec - (t2)->tv_usec < 0) (t3)->tv_usec --, (t3)->tv_usec + = MICROSECONDS; \
} while (0)

calling process

Under Linux, you can use system directly to call external programs. It is best to use WinExec for Windows, because WinExec can support whether to open or hide the program window. Use the second input parameter of WinExec to specify, such as

SW_SHOW/SW_HIDE.

Miscellaneous

Linux is srandom and random functions, Windows is srand and rand functions.
snprintf for Linux and _snprintf for Windows.
In the same way, strcasecmp in Linux is _stricmp in Windows.

Error handling

Under Linux, the global variable errno is usually used to represent the error number of function execution. Under Windows, use the GetLastError () call to obtain it.

Only in Linux environment
These functions or macros are not available in Windows at all and require users to implement them manually.
atoll

[cpp]
long long
atoll (const char *p)
{
int minus = 0;
long long value = 0;
if (*p == -’)
{
minus + + ;
p + + ;
}
while (*p >= 0’ & amp; & amp; *p <= 9’)
{
value *= 10;
value + = *p – 0’;
p + + ;
}
return minus ? 0 – value : value;
}
gettimeofday

[cpp]
#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
#define EPOCHFILETIME 11644473600000000Ui64
#else
#define EPOCHFILETIME 11644473600000000ULL
#endif

struct timezone
{
int tz_minuteswest;
int tz_dsttime;
};

int
gettimeofday (struct timeval *tv, struct timezone *tz)
{
FILETIME ft;
LARGE_INTEGER li;
__int64t;
static int tzflag;

if(tv)
{
GetSystemTimeAsFileTime ( & amp;ft);
li.LowPart = ft.dwLowDateTime;
li.HighPart = ft.dwHighDateTime;
t = li.QuadPart; /* In 100-nanosecond intervals /
t -= EPOCHFILETIME; /
Offset to the Epoch time /
t /= 10; /
In microseconds */
tv->tv_sec = (long) (t / 1000000);
tv->tv_usec = (long) (t % 1000000);
}

if(tz)
{
if (!tzflag)
{
_tzset();
tzflag + + ;
}
tz->tz_minuteswest = _timezone / 60;
tz->tz_dsttime = _daylight;
}

return 0;
}

Compilation related
The current function is represented by __FUNCTION__ on Linux and __func__ on Windows.

Socket Programming Problems Encountered in Migrating Windows to Linux Code
1)Header file
winsock.h/winsock2.h under windows
sys/socket.h under linux
Error handling: errno.h

2)Initialization
You need to use WSAStartup under windows
Not required under linux

3)Close socket
closesocket(…) under windows
close(…) under linux

4)Type
SOCKET under windows
int under linux
Some macros I use:
#ifdef WIN32
typedef int socklen_t;
typedef int ssize_t;
#endif

#ifdef LINUX
typedef int SOCKET;
typedef unsigned char BYTE;
typedef unsigned long DWORD;
#define FALSE 0
#define SOCKET_ERROR (-1)
#endif

5) Get error code
getlasterror()/WSAGetLastError() under windows
errno variable under linux

6) Set up non-blocking
ioctlsocket() under windows
fcntl() under linux

7) The last parameter of the send function
Generally set to 0 under windows
It is best to set it to MSG_NOSIGNAL under Linux. If not set, the program may exit after a sending error.

8) Millisecond time acquisition
GetTickCount() under windows
gettimeofday() under linux

3. Multi-threading
Multi-threading: (win)process.h –> (linux)pthread.h
_beginthread –> pthread_create
_endthread –> pthread_exit

The sockets used by windows and linux platforms are all inherited from Berkeley socket (rfc3493). They all support the select I/O model and the use of getaddrinfo and getnameinfo to implement protocol-independent programming. But there are subtle differences,

There are:

Header files and class libraries. Windows uses winsock2.h (needs to be included before windows.h) and links to the library ws2_32.lib; Linux uses netinet/in.h, netdb.h, etc.
Under windows, WSAStartup and WSAClean must be used before and after using socket.
Close the socket. Windows uses closesocket and Linux uses close.
The type of the socket length of the send and recv function parameters is int in Windows and socklen_t in Linux. This difference can be handled in the precompiled instruction. When the platform is Windows, #define socklen_t unsigned int.
The first parameter of the select function is ignored by Windows. Under Linux, this parameter represents the upper limit of the socket in the collection, which is generally set to sockfd (the socket that needs to be selected) + 1.
The return value type of the socket function under Windows is SOCKET (unsigned int), and INVALID_SOCKET(0) is returned when an error occurs. The return value type of the socket function under Linux is int, and -1 is returned when an error occurs.
In addition, if the local loopback address is bound, the sendto function under Windows can pass, but the sendto function under Linux returns an error: errno=22, Invalid argument. Generally, wildcard addresses are bound.