The difference between Windows sockets and Linux socket programming ZZ

Source: Internet
Author: User

Socket related programs to be aware of from Windows porting to 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
Not required under Linux

3) Close Socket

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

4) Type
Under Windows socket
Linux under int
As some of the macros I used:
#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
Windows GetLastError ()/wsagetlasterror ()
Linux under errno variable

6) Set non-blocking
Under Windows Ioctlsocket ()
Linux under Fcntl () <fcntl.h>

7) The last parameter of the Send function
Under Windows general set to 0
Linux is best set to msg_nosignal, if not set, after sending an error may cause the program to exit.

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

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

First, the socket programming under Linux:

1. The client performs the following steps in sequence:

Socket ()

Connect ()

Send () or recv ()

Close ()

Note that the address structure is populated before connect, and the IP address is converted to a network byte order, typically with Inet_aton ().

2. Server side:

Socket ()

Bind ()

Listen ()

Accpet ()

Recv () or send ()

Close ()

(PS: generally by assigning the last parameter of Send () and recv () to 0 or one to distinguish between blocking and non-blocking, where 0 corresponds to blocking, 1 corresponds to non-blocking)

Second, the network programming under Windows:

Anyone who has done Windows network programming knows that Microsoft's MFC encapsulates complex Winsock API functions into classes, making it easier to write Web applications. That is, Windows provides both the upper-level network API functions and the underlying API functions.

1, for the use of the upper-level API function: If you use the CSocket class to define an object obj, then the steps for network programming are as follows:

Client:

Obj. Create ()

Obj. Connect ()

Obj. Receive () or obj. Send ()

Obj. Close ()

Server-side:

First call AfxSocketInit () to detect protocol stack installation

Obj. Create ()

Obj. Listen ()

Obj. Accpet ()

Obj. Receive () or obj. Send ()

Obj. Close ()

2, for the use of the underlying API functions, the steps are as follows:

Client:

WSAStartup ()

Socket ()

Connect ()

Send () or recv ()

Closesocket ()

Server-side:

WSAStartup ()
Socket ()
Bind ()
Listen ()
Accpet ()
Send ()
Recv ()
Closesocket ()

(Ps:windows CSocket class is synchronous mode, there is blocking phenomenon, CAsyncSocket is asynchronous, non-blocking phenomenon.) )

The above comparison shows that network programming under Linux is similar to using the underlying API under Windows, but there are also differences:

Difference one: Need to add WSAStartup () function under Windows

Difference two: Close socket:linux for Close (), Windows for Closesocket ()

Using the upper-level API under Windows, there are generally two types of classes, CSocket and Casynsocket

In this case, the following socket function is typically capitalized in the first letter. The underlying API, whether it's under Windows or Linux, has a lowercase first letter in the socket.

Original link: http://www.linuxidc.com/Linux/2011-03/33324p2.htm

Http://www.cnblogs.com/lcryby/articles/2227440.html

The difference between Windows sockets and Linux socket programming ZZ

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.