The difference between a socket under Windows and Linux

Source: Internet
Author: User

Windows to the Linux problems with Code migration

1 , porting of some common functions http://www.vckbase.com/document/viewdoc/?id=1586

2 , Network ------ reproduced & modify (to be sorted) Socket Related programs from Windows ported to Linux needs to be noted below.

1) header File Windows under winsock.h/winsock2.h Linux under sys/socket.h Error Handling: errno.h

2) Initialize Windows need to use the WSAStartup Linux do not need the

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

4) type Windows under SOCKET Linux under int as some of the macros I used:



5) Get the error code Windows under GetLastError ()/wsagetlasterror () Linux under errno variables

6) set non-blocking Windows under ioctlsocket () Linux under Fcntl ()

7) the last parameter of the Send function under windows is generally set to 0 under Linux preferably set to Msg_ Nosignal, if not set, can cause the program to exit after sending an error .

8) millisecond-time acquisition Windows under GetTickCount () Linux under Gettimeofday ()

3 , multithreading Multithreading : (Win) Process.h-- ) (Linux) pthread.h _beginthread--pthread_create _endthread--Pthread_exit



about this topic is circulated on the Internet is the same version, that is the first item is the difference between the header file, but the following list of header files only #include not (it is estimated that the original in the process of continuous reprint someone accidentally forgot to escape the angle brackets and let the browser HTML The one whose tag parsing is gone). Now we have a tidy up, and we will continue to add content later.

1) header file Windows under winsock.h Winsock2.h Linux under Netinet/in.h (most of them are here), unistd.h ( Close sys/socket.h (in the In.h Yes, you can save it.

2) Initialize Windows need to use the WSAStartup Start Ws2_32.lib , and to use #pragma comment (lib, "ws2_32") to tell the compiler to link the Lib . not required under Linux

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

4) type Windows under SOCKET Linux under int (I like to use Long , so that the guarantee is 4byte , because -1 I always like to write 0xFFFF )

5) Get the error code Windows under GetLastError ()/wsagetlasterror () Linux failed to execute successfully. Socket operation will return -1 ; if it contains errno.h , you will set errno variables

6) set non-blocking Windows under ioctlsocket () Linux under fcntl () , a header file is required fcntl.h

7) the last parameter of the Send function under windows is generally set to 0 under Linux preferably set to Msg_nosignal, if not set, may cause the program to exit after sending an error

8) millisecond-time acquisition Windows under GetTickCount () Linux under Gettimeofday ()

9) Multithreading Windows under contains process.h , using _beginthread and the _endthread Linux under contains pthread.h , using pthread_create and the Pthread_exit

Ten)withIPDefine an address (sockaddr_inthe difference of the structure)Windowsunderaddr_var.sin_addr. S_un. S_ADDR Linuxunderaddr_var.sin_addr.s_addrandWinsockthe last one.32bitof thes_addrThere are also several with union (Union) in the form of a member variable that shares memory space with it (it is easy to assign values in other ways), andLinuxof theSocketwithout this union, is a32bitof thes_addr. Met the kind of got to be4aCharof theIPthe form (e.g.127One,0One,0a and the1a total of fourChar),WinSockcan be used directly4aS_bto assign a value tos_addr, while inLinux, you can shift the left side with the8bit, total) side The added method assigns a value.

One ) Exception Handling Linux when the connection is broken and the data is sent, it is not only Send () return value will be reflected, but also like the system sends an exception message, if not processed, the system will be Brokepipe , the program exits. To do this, the last parameter of the send () function can be set msg_nosignal, prohibit send () The function sends an exception message to the system.

 

Socket differences under Windows and Linux

1) header File
Windows Next Winsock.h/winsock2.h
Linux Next sys/socket. h error Handling:errno.h

2) Initialize
Windows Need to use the WSAStartup

wsadata wsadata;

err = WSAStartup (0x202,&wsadata); if (err! = 0)
{
return 0;
}
else if (Lobyte (wsadata.wversion)! = 2 | | Hibyte (wsadata.wversion)! = 2)// detect if this version of the socket is supported
{
WSACleanup ();
return 0;
}

the corresponding exit cleaning with wsacleanup ();
Linux Do not need the

3) socket
Span style= "Color:black;background:none repeat scroll 0% 0% #FFFF66;" >windows under closesocket (...)
linux under Close (...)

4) Socket type
Windows Lower SOCKET
Linux Under int

5) Get the error code
Windows Next wsagetlasterror ()

Linux Next errno variable

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

6) windows under ioctlsocket (Server_ , fionbio,&ul);     int ul = 1
linux under fcntl (Server_ socket , F_SETFL, O_nonblock);    


7) The last parameter of the Send function
Windows Under General settings of 0
Linux Must be the next 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
Windows Next GetTickCount ()
Linux Next gettimeofday ()

9) Compile connection

Windows Next 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.

10) Socket operation error return value

all for SOCKET_error, his value is -1

One ) exception handling

Windows there is no exception handling for send(), 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 thesend () function can be set msg_nosignal, and the Send () function is forbidden to send exception messages to the system.

12) WSA macro

Windows Operations related to the WSA

Linux The following WSA -related operations are not necessary in Linux and can be removed directly


)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.