windows套接字相关函数
The Windows Sockets socket function creates a socket that is bound to a specific service provider.
SOCKET socket( int af, int type, int protocol );
Parameters
- af
- [in] Address family specification.
- type
- [in] Type specification for the new socket.
The following are the only two type specifications supported for Windows Sockets 1.1:
Type Explanation SOCK_STREAM Provides sequenced, reliable, two-way, connection-based byte streams with an OOB data transmission mechanism. Uses TCP for the Internet address family. SOCK_DGRAM Supports datagrams, which are connectionless, unreliable buffers of a fixed (typically small) maximum length. Uses UDP for the Internet address family.
In Windows Sockets 2, many new socket types will be introduced and no longer need to be specified, since an application can dynamically discover the attributes of each available transport protocol through the WSAEnumProtocols function. Socket type definitions appear in Winsock2.h, which will be periodically updated as new socket types, address families, and protocols are defined.
- protocol
- [in] Protocol to be used with the socket that is specific to the indicated address family.
The Windows Sockets bind function associates a local address with a socket.
int bind( SOCKET s, const struct sockaddr FAR *name, int namelen );
Parameters
- s
- [in] Descriptor identifying an unbound socket.
- name
- [in] Address to assign to the socket from the SOCKADDR structure.
- namelen
- [in] Length of the value in the name parameter.
Return Values
If no error occurs, bind returns zero. Otherwise, it returns SOCKET_ERROR, and a specific error code can be retrieved by calling WSAGetLastError.
short int sin_family; /* Address family */
unsigned short int sin_port; /* Port number */
struct sin_addr sin_addr; /* Internet address */
unsigned char sin_zero[8]; /* Same size as struct sockaddr */
};
sin_family:指代协议族,在socket编程中仅仅能是AF_INET
sin_port:存储端口号(使用网络字节顺序)
sin_addr:存储IP地址。使用in_addr这个数据结构
sin_zero:是为了让sockaddr与sockaddr_in两个数据结构保持大小同样而保留的空字节。
union {
struct{ unsigned char s_b1,s_b2, s_b3,s_b4;} S_un_b;
struct{ unsigned short s_w1, s_w2;} S_un_w;
unsigned long S_addr;
} S_un;
} IN_ADDR;
bind(s,(const struct sockaddr)&addrbindto,sizeof(addrbindto))
The Windows Sockets listen function places a socket a state where it is listening for an incoming connection.
int listen( SOCKET s, int backlog );
Parameters
- s
- [in] Descriptor identifying a bound, unconnected socket.
- backlog
- [in] Maximum length of the queue of pending connections. If set to SOMAXCONN, the underlying service provider responsible for socket s will set the backlog to a maximum reasonable value. There is no standard provision to obtain the actual backlog value.
Return Values
If no error occurs, listen returns zero. Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling WSAGetLastError.
该函数在指定的套接字上监听连接请求,參数s为监听的套接字,backlog表示表示同意的最大连接数The Windows Sockets connect function establishes a connection to a specified socket.
int connect(
SOCKET s,
const struct sockaddr FAR *name,
int namelen
);
Parameters
- s
- [in] Descriptor identifying an unconnected socket.
- name
- [in] Name of the socket to which the connection should be established.
- namelen
- [in] Length of name.
The Windows Sockets send function sends data on a connected socket.
int send( SOCKET s, const char FAR *buf, int len, int flags );
Parameters
- s
- [in] Descriptor identifying a connected socket.
- buf
- [in] Buffer containing the data to be transmitted.
- len
- [in] Length of the data in buf.
- flags
- [in] Indicator specifying the way in which the call is made.
The Windows Sockets recv function receives data from a connected socket.
int recv(
SOCKET s,
char FAR *buf,
int len,
int flags
);
Parameters
- s
- [in] Descriptor identifying a connected socket.
- buf
- [out] Buffer for the incoming data.
- len
- [in] Length of buf.