Socket

SOCKET WSAAPI socket(
  int af,//协议地址族
  int type,//协议套接字类型
  int protocol//传输层协议
);

Af

Meaning

AF_UNSPEC
0

The address family is unspecified.

AF_INET
2

The Internet Protocol version 4 (IPv4) address family.

AF_NETBIOS
17

The NetBIOS address family. This address family is only supported if a Windows Sockets provider for NetBIOS is installed.

AF_INET6
23

The Internet Protocol version 6 (IPv6) address family.

AF_IRDA
26

The Infrared Data Association (IrDA) address family. This address family is only supported if the computer has an infrared port and driver installed.

AF_BTM
32

The Bluetooth address family. This address family is only supported if a Bluetooth adapter is installed on Windows Server 2003 or later.

 

 

Type

Meaning

SOCK_STREAM
1

Provides sequenced, reliable, two-way, connection-based byte streams with an OOB data transmission mechanism. Uses the Transmission Control Protocol (TCP) for the Internet address family (AF_INET or AF_INET6).

SOCK_DGRAM
2

Supports datagrams, which are connectionless, unreliable buffers of a fixed (typically small) maximum length. Uses the User Datagram Protocol (UDP) for the Internet address family (AF_INET or AF_INET6).

SOCK_RAW
3

Provides a raw socket that allows an application to manipulate the next upper-layer protocol header. To manipulate the IPv4 header, the IP_HDRINCL socket option must be set on the socket. To manipulate the IPv6 header, the IPV6_HDRINCL socket option must be set on the socket.

SOCK_RDM
4

Provides a reliable message datagram. An example of this type is the Pragmatic General Multicast (PGM) multicast protocol implementation in Windows, often referred to as reliable multicast programming.

SOCK_SEQPACKET
5

Provides a pseudo-stream packet based on datagrams.

 

 

protocol

Meaning

IPPROTO_TCP
6

The Transmission Control Protocol (TCP). This is a possible value when the af parameter is AF_INET or AF_INET6 and the type parameter is SOCK_STREAM.

IPPROTO_UDP
17

The User Datagram Protocol (UDP). This is a possible value when the af parameter is AF_INET or AF_INET6 and the type parameter is SOCK_DGRAM.

IPPROTO_RM
113

The PGM protocol for reliable multicast. This is a possible value when the af parameter is AF_INET and the type parameter is SOCK_RDM. On the Windows SDK released for Windows Vista and later, this value is also called IPPROTO_PGM.

 

 

struct sockaddr {
        ushort  sa_family;
        char    sa_data[14];
};
struct sockaddr_in {
        short   sin_family;
        u_short sin_port;
        struct  in_addr sin_addr;//把IP地址保存为4字节数
        char    sin_zero[8];//填充项,使其与sockaddr结构一样长
};

//字节排序:主机对字节编号方式和网络不同
net to host long short

ntohl() ntohs()

htons() htonl()

 

sockaddr_in inter;

int nport=5050;

inter.sin_family=AF_INET;

inter.sin_addr.s_addr=inet_addr("127.0.0.1");

//IP地址→位无符长整形

inter.sin_port=htons(nport);

cout<<inter.sin_port;

你可能感兴趣的:(socket)