Linux-to-Windows program porting for network programming in Windows

Source: Internet
Author: User

1) To prevent redefinition, the header file inclusion sequence in Windows code is as follows:

# Include <winsock2.h>
# Define win32_lean_and_mean
# Include <windows. h>

Reference: http://blog.sina.com.cn/s/blog_658d267b0100jb2l.html

// ================================================ ================================================== ========================

2) sockaddr

Windows:

struct sockaddr {    unsigned short    sa_family;    // address family, AF_xxx    char              sa_data[14];  // 14 bytes of protocol address};


Linux:

Struct sockaddr {

Unsigned short

Sa_family;/* address family, af_xxx */

Char sa_data [14];/* 14-byte Protocol address */

};

Sa_family is generally af_inet, which represents the Internet (TCP/IP) address family. sa_data contains the IP address and port number of the socket.


3) sockaddr_in

Windows:

struct sockaddr_in {    short            sin_family;   // e.g. AF_INET, AF_INET6    unsigned short   sin_port;     // e.g. htons(3490)    struct in_addr   sin_addr;     // see struct in_addr, below    char             sin_zero[8];  // zero this if you want to};

Linux:

Struct sockaddr_in {

Short int sin_family;/* address family */

Unsigned short int sin_port;/* Port Number */

Struct in_addr sin_addr;/* IP Address */

Unsigned char sin_zero [8];/* fill in 0 to keep the same size as struct sockaddr */

};

This structure is more convenient to use. Sin_zero is used to fill the sockaddr_in structure with the same length as struct sockaddr. It can be set to zero using the bzero () or memset () function.

The pointer to sockaddr_in and the pointer to sockaddr can be converted to each other, which means that if the parameter type required by a function is sockaddr, you can call

Converts a pointer to sockaddr_in to a pointer to sockaddr, or vice versa.

// ================================================ ================================================== ========================

4) socket ()

Linux:

Int socket (int af, int type, int Protocol );

Windows:

Socket socket (int af, int type, int Protocol); // here Windows and Linux are the same, socket type is int type (16-bit unsigned integer)
5) bind ()

Linux:

Int BIND (intSockfd, Struct sockaddr *My_addr, Socklen_tAddrlen); 

Windows:

Int BIND (socketSockfd, Struct sockaddr *My_addr, IntAddrlen);// Socket_len is of the int type. ------ I'm not sure about it here. It's true !!

6) recvfrom ()

Windows:

Int recvfrom (socket sockfd, char * Buf, int Len, int flags, struct sockaddr * From, int * fromlen );

Linux:

Int recvfrom (INT sockfd, void * Buf, int Len, unsigned int flags, struct sockaddr * From, socket_t * fromlen );

7) sendto ()

Windows:

Int sendto (socket S, const char * MSG, int Len, int flags, const struct sockaddr * To, int tolen );

Linux:

Int sendto (int s, const void * MSG, int Len, int flags, struct sockaddr * To, socklen_t tolen );

8) Select ()

Linux:


Windows:

9) addinfo

Windows:

typedef struct addrinfo {  int             ai_flags;  int             ai_family;  int             ai_socktype;  int             ai_protocol;  size_t          ai_addrlen;  char            *ai_canonname;  struct sockaddr  *ai_addr;  struct addrinfo  *ai_next;} ADDRINFOA, *PADDRINFOA;

Linux:

Struct addrinfo {int ai_flags; int ai_family; int ai_socktype; int ai_protocol; size_t ai_addrlen; struct sockaddr * ai_addr; char * ai_canonname; // differences !!! Struct addrinfo * ai_next ;};


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.