Differences between socket Linux and Windows

Source: Internet
Author: User

Socket programming windows to Linux code porting Problems

1. Some common function porting http://www.vckbase.com/document/viewdoc? Id = 1586

2. Network ------ reprint & modify (to be sorted out) socket-related programs need to be noted when porting from windows to Linux

1) the header file Winsock. h/winsock2.h in Windows
How to handle sys/socket. h errors in Linux: errno. h

2) wsastartup is required for Windows initialization.
Not required in Linux

3) Disable socket
Closesocket (...) in Windows (...)
Close (...) in Linux (...)

4) socket in Windows
I use some macros for int in Linux:

#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 the error code getlasterror ()/wsagetlasterror () in Windows ()
Errno variable in Linux

6) set ioctlsocket () in non-blocking windows ()
Fcntl () in Linux ()

7) The last parameter of the send function is usually set to 0 in windows.
In Linux, it is best to set msg_nosignal. If this parameter is not set, the program may exit after an error occurs.

8) Get gettickcount () in Windows in milliseconds ()
Gettimeofday () in Linux ()

3. multithreading:
(WIN) process. h --> (Linux) pthread. h
_ Beginthread --> pthread_create _ endthread --> pthread_exit


--------------------------------------------------------------

SocketInWindowsLower andLinuxDifferences below

1) header file
WindowsWinsock. h/winsock2.h
LinuxSys/Socket. H error handling: errno. h

2) initialization
WindowsWsastartup

Wsadata;

Err = wsastartup (0x202, & wsadata); If (Err! = 0)
{
Return 0;
}
Else if (lobyte (wsadata. wversion )! = 2 | hibyte (wsadata. wversion )! = 2) // check whether this version is supportedSocket
{
Wsacleanup ();
Return 0;
}

Wsacleanup () is used for exit cleaning ();
LinuxNot Required

3) DisableSocket
Windows
Closesocket (...)
LinuxClose (...)

4)SocketType
WindowsLowerSocket
Linux
Lower int

5) Get the error code
WindowsWsagetlasterror ()

LinuxVariable errno

Extern int errno;
Int geterror () {return errno ;}

6) set non-blocking
WindowsLower ioctlsocket (server _Socket, Fionbio, & UL); int ul = 1
LinuxFcntl (server _Socket, F_setfl, o_nonblock); <fcntl. h>

7) The last parameter of the send Function
WindowsGenerally, it is set to 0.
LinuxThe following parameters must be usedSocketMedium, the maximum number of values (integer) plus 1 (set to msg_nosignal)

8) millisecond-level acquisition
WindowsGettickcount ()
LinuxGettimeofday ()

9) Compile the connection

WindowsWs2_32.lib
LinuxLower

-Lstdc
Libstdc ++. so.5 is required during runtime. You can create a link in the/usr/lib directory.

10)SocketOperation error Return Value

AllSocket_ Error. His value is-1.

11) Exception Handling

WindowsNo exception handling for send (). The last parameter is invalid. Write 0.

LinuxLower

When the connection is disconnected and data is sent, not only will the return value of send () be reflected, but it will also send an exception message like the system. If it is not processed, the system will output brokepipe, the program will return. For this reason, the last parameter of the send () function can be set to msg_nosignal to prohibit the send () function from sending exception messages to the system.

12) WSA macro

WindowsWSA-related operations

LinuxWSA-related operations areLinuxNo, just remove it.

13) Multithreading
Multithreading: (WIN) process. h --> --〉(Linux) Pthread. h
_ Beginthread --> pthread_create
_ Endthread --> pthread_exit



Related Article

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.