The difference between a socket under Windows and Linux

Source: Internet
Author: User

The original: The difference between a socket under Windows and Linux

1) header File
Under Windows Winsock.h/winsock2.h
Linux under sys/socket.h error handling: errno.h

2) Initialization
Need to use WSAStartup under Windows

Wsadata Wsadata;

err = WSAStartup (0x202,&wsadata); if (err! = 0) {return 0;} else if (Lobyte (wsadata.wversion)! = 2 | | Hibyte (wsadata.wversion)! = 2)//Detect support for this version of socket {wsacleanup (); return 0;}

The corresponding exit cleaning with WSACleanup ();
Not required under Linux

3) Close Socket
Windows Closesocket (...)
Linux under Close (...)

4) Socket type
Under Windows socket
Linux under int

5) Get the error code
Under Windows WSAGetLastError ()

Linux under errno variable

extern int errno;
int GetError () {return errno;}

6) Set non-blocking
Windows under Ioctlsocket (Server_socket,fionbio,&ul); int ul = 1
Linux under Fcntl (SERVER_SOCKET,F_SETFL, O_nonblock); <fcntl.h>


7) The last parameter of the Send function
Under Windows general set to 0
Linux must be the last few parameters used in the socket, the maximum number of values (integer) plus 1 (the other is set to msg_nosignal)


8) Millisecond-time acquisition
Under Windows GetTickCount ()
Linux under Gettimeofday ()

9) Compile the connection

Under Windows Ws2_32.lib
Linux under

The connection is using parameters:-LSTDC
The runtime requires libstdc++.so.5, and a link can be created in the/usr/lib directory.

Socket operation Error return value

All for Socket_error, his value is-1

11) Exception Handling

There is no exception handling for Send () under Windows, the last parameter is invalid, write 0.

Linux under

When the connection is broken, 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 msg_nosignal, and the Send () function is forbidden to send exception messages to the system.

WSA macro

Actions related to WSA under Windows

Linux under the WSA-related operations in Linux unnecessary, directly removed can


3. Multithreading
Multithreading: (Win) Process.h--〉 (Linux) pthread.h
_beginthread-Pthread_create
_endthread-Pthread_exit

The difference between a socket under Windows and Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.