The difference between socket programming under windows and linux

1.Linux header files mainly include
  #include <sys/socket.h>
  #include <netinet/in.h>
  #include <netdb.h>
  #include <arpa/inet.h>
  
  and windows include
  #include < Winsock2.h>
  #pragma comment(lib,"Ws2_32.lib")

2.
WSAStartup() is required for initializing windows; not required for linux;
WSACleanup() for corresponding exit cleanup; not required for linux

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

4. Socket socket type The
socket is plastic in Linux,
 and it is a socket type in Windows.
 Because the socket in Linux is the same as the ordinary fd, you can directly use read and write when sending and receiving data in the TCP socket. Windows can only use recv and send.

5. Get the error code
WSAGetLastError() under windows;
set the errno variable under linux to get the error code with geterror();

6. Set the socket attribute
to fcntl (fd, F_SETFL, flag | O_NONBLOCK) under Linux;
ioctlsocket (fd, FIONBIO, (unsigned long *) &flag) under Windows;

7. Get the time in milliseconds
: GetTickCount()
under windows , gettimeofday() under linux

8.
WSA macro WSA-related operations
under windows WSA-related operations under linux are unnecessary in Linux, just remove them directly

9. Multithreading
Multithreading: (win)process.h -->(linux)pthread.h
_beginthread --> pthread_create
_endthread --> pthread_exit

Guess you like

Origin blog.csdn.net/Fengfgg/article/details/112229986