Windows Socket-related functions
Windows Socket related functions
Author: vpoetMail: 18200268879@163.com
We learn that TCP/IP protocol is nothing more than using these protocols for communication development. However, if we want to directly follow the protocol rules and Protocol formatsNetwork development is undoubtedly a very painful thing. Obviously, to reduce the programmer's development burden, windows provides us with a set of Network DevelopmentThe API family is called the socket library.
But what is the relationship between the socket and the TCP/IP protocol.
We can understand this for the time being ,:
So OK. we can't understand it now.
Next, let's talk about the main use of APIS for socket programming.
1: socketMSDN:
The Windows SocketsSocketFunction 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 twoTypeSpecifications supported for Windows Sockets 1.1:
| Type |
Explanation |
| SOCK_STREAM |
Provides sequenced, reliable, two-way, connection-based byte streams with an OOB data transmission mechanic. Uses TCP for the Internet address family. |
| SOCK_DGRAM |
Supports extends rams, which are connectionless, unreliable buffers of a fixed (typically small) maximum length. Uses UDP for the Internet address family. |
In Windows Sockets 2, when 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 throughWSAEnumProtocolsFunction. 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.
This function creates a socket. The parameter af is of the protocol family AF_INET and is of the TCP/IP protocol family.
Type is the socket type, and there are two types of communication similar to SOCK_STREAM: use TCP for communication SOCK_STREAM for UDP
Protocol can be IPPROTO_TCP or IPPROTO_IP. The English type of this parameter is consistent.
Example: socket s;
S = socket (AF_INET, SOCK_STREAM, IPPOTO_TCP) indicates that TCP is used for communication.
2. bind
As the name implies, this function is bound. Anyone who has used socket programming knows that this function is required when writing a server program.
MSDN:
The Windows SocketsBindFunction 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
SOCKADDRStructure.
-
Namelen
-
[In] Length of the value in
NameParameter.
Return Values
If no error occurs,BindReturns zero. Otherwise, it returns SOCKET_ERROR, and a specific error code can be retrieved by callingWSAGetLastError.
This function binds a local address to a socket. If the binding is not misplaced, 0 is returned; otherwise, SOCKET_ERROR is returned.
The parameter s is the socket to be bound.
The parameter * name is a constant sockaddr structure, but we usually use the sockaddr_in structure.
Its structure is defined as follows:
Struct sockaddr_in {
Short int sin_family;/* Address family */
Unsigned short int sin_port;/* Port number */
Structsin_addr sin_addr;/* Internet address */
Unsigned char sin_zero [8];/* Same size as struct sockaddr */
};
Sin_family: refers to the protocol family. It can only be AF_INET in socket programming.
Sin_port: storage port number (in bytes)
Sin_addr: storage IP address. Use the in_addr data structure.
Sin_zero: it is an empty byte reserved to keep the data structures of sockaddr and sockaddr_in the same size.
Specifically, sin_addr is defined.
Typedef struct in_addr {
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;
Generally, we use
Addrs. sin_addr.S_addr = inet_addr ("192.168.1.110 ")
The inet_addr function can directly convert a string to the in_addr type.
You can also use inet_ntoa () to convert the in_addr type to the string type.
The namelen parameter is the name length, that is, sizeof (name)
A typical example:
SOCKET s;
S = socket (AF_INET, SOCK_STREAM, IPPOTO_TCP );
Sockaddr_in addrbindto;
Addrbindto. sin_family = AF_INET;
Addrbindto. port = htons (8001) htons converts the u_short type to the network byte sequence, and htonl converts the u_long type to the network byte sequence.
Addrbindto. sin_addr_S_addr = inet_addr ("192.168.1.110 ")
bind(s,(const struct sockaddr)&addrbindto,sizeof(addrbindto))
3. listen
The Windows SocketsListenFunction 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
SWill 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,ListenReturns zero. Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by callingWSAGetLastError.
This function listens for connection requests on the specified socket. parameter s is the socket for listening, and backlog indicates the maximum number of connections allowed.
Example:
Listen (s, 5 );
4. connect
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 shoshould be established.
-
Namelen
-
[In] Length
Name.
This function establishes a connection to a specified socket.
The parameter s is the socket to establish a connection.
Name is a sockaddr pointer.
Namelen is sizeof (name)
Example:
SOCKET s;
Sockaddr_in conaddr;
S = socket (AF_INET, SOCK_STREAM, IPPOTO_TCP );Conaddr. sin_family = AF_INET;
Conaddr. port = htons (8001) htons converts the u_short type to the network byte sequence, and htonl converts the u_long type to the network byte sequence.
Conaddr. sin_addr_S_addr = inet_addr ("192.168.1.110 ")
Connect (s ,(
Const struct sockaddr) & Conaddr, sizeof (
Conaddr))
5. send
The Windows SocketsSendFunction 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.
This function is used to send data to connected sockets.
The parameter s is a connected socket.
The buf parameter is the cache for sending data.
The len parameter is the length of the cached data to be sent.
Example:
Char buf [20] = "hello123 ";
Send (s, (const char *) buf, sizeof (buf ))
5. recv
The Windows Sockets recv function extends es 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
Buf.
This function is an accepted function:
Parameter and send type, not described in detail
6. recvfrom, sendto
These two functions are similar to send and recv functions, but they are used for UDP communication.
OK. The main functions will be introduced here first. In the next blog, We will write a simple TCP-based CS communication Demo.