Differences between socket in Windows and Linux

Source: Internet
Author: User

Note the following when porting socket-related programs from windows to Linux:

1) header file
Winsock. h/winsock2.h in Windows
Sys/socket. h in Linux
Error Handling: errno. h

2) initialization
Wsastartup and wsacleanup are required for Windows.
Not required in Linux

3) Disable socket

Closesocket (...) in Windows (...)
Close (...) in Linux (...)

4) Type
Socket in Windows
Int in Linux
For example, some macros I use:
# 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 non-blocking
Ioctlsocket () in Windows ()
Fcntl () in Linux <fcntl. h>

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

8) millisecond-level acquisition
Gettickcount () in Windows ()
Gettimeofday () in Linux ()

9) Multithreading

Process. H is included in windows, and _ beginthread and _ endthread are used.
Linux contains pthread. h and uses pthread_create and pthread_exit.

10) define an address with an IP address (difference in the structure of sockaddr_in)
Addr_var.sin_addr.s_un.s_addr in Windows
Addr_var.sin_addr.s_addr in Linux
In addition, the 32bit s_addr in Winsock also has several member variables that share the memory space with it in the form of union (which facilitates assignment in other ways ), the Linux socket does not have this combination, it is a 32bit s_addr. In the format of four char IPS (for example, 127, 0, 0, and 1 ), winsock can be assigned to s_addr using four s_ B values. In Linux, you can assign values by adding edges to the left (eight bits in total.

11) Exception Handling
In Linux, 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 exits. For this reason, the last parameter of the send () function can be set to msg_nosignal, which prohibits the send () function from sending exception messages to the system.

I. socket programming in Linux:

1. The client performs the following steps in sequence:

Socket ()

Connect ()

Send () or Recv ()

Close ()

Note that the address structure should be filled before connect, and the IP address should be converted to the network byte order. inet_aton () is generally used ().

2. server side:

Socket ()

BIND ()

Listen ()

Accpet ()

Recv () or send ()

Close ()

(PS: Generally, blocking and non-blocking are distinguished by assigning the last parameter of send () and Recv () to 0 or 1, where 0 corresponds to blocking and 1 corresponds to non-blocking)

Ii. Network Programming in Windows:

Anyone who has worked on Windows Network Programming knows that Microsoft's MFC encapsulates complex Winsock API functions into classes, making it easier to write network applications. That is, Windows provides both upper-layer network API functions and underlying API functions.

1. for upper-layer API functions: If the csocket class is used to define an object OBJ, the network programming procedure is as follows:

Client:

OBJ. Create ()

OBJ. Connect ()

OBJ. Receive () or obj. Send ()

OBJ. Close ()

Server:

Call afxsocketinit () to check the protocol stack installation.

OBJ. Create ()

OBJ. Listen ()

OBJ. accpet ()

OBJ. Receive () or obj. Send ()

OBJ. Close ()

2. The steps for using underlying API functions are as follows:

Client:

Wsastartup ()

Socket ()

Connect ()

Send () or Recv ()

Closesocket ()

Server:

Wsastartup ()
Socket ()
BIND ()
Listen ()
Accpet ()
Send ()
Recv ()
Closesocket ()

(PS: in windows, the csocket class is synchronous with blocking. casyncsocket is asynchronous without blocking .)

Through the above comparison, we can find that network programming in Linux is similar to using underlying APIs in windows, but there are also differences:

Difference 1: add the wsastartup () function in windows.

Difference 2: Disable socket: Close () for Linux and closesocket () for Windows ()

In Windows, the upper-layer APIs are used. Generally, there are two types of classes: csocket and casynsocket.

In this case, the first letter of the socket function is generally capitalized. The underlying APIs, whether in Windows or Linux, use lower-case letters for socket functions.

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.