The steps to write a Web application server using Winsock are outlined below
WSAStartup initializing the Network programming library
Socket creation Socket
BIND specifies address, port, bound socket
Listen into the Listening state
Accept waits to receive new connections
Send/recv Sending and receiving data
Closesocket Critical sockets
WSAStartup release of the use of dynamic libraries
Each API is described in detail below
1. Winsock initialization
Call int WSAStartup (WORD wversionrequested, Lpwsadata lpwsadata) function
WSAStartup, which is the start command for the WSA (Windows socknds asynchronous,windows asynchronous sockets). is a network programming interface under Windows software Winsock1 or Winsock2 a command (Ps:winsock is developed by the BSD socket under UNIX, is a network protocol-independent programming Interface).
In order to invoke any of the Winsock API functions in the application, the first thing to do is to initialize the Winsock service through the WSAStartup function, so the WSAStartup function needs to be called. A program that uses the socket must call the WSAStartup function before using the socket.
WORD wversionrequested;
Wsadata Wsadata;
Wversionrequested=makeword (2,2);
if (WSAStartup (wversionrequested,&wsadata)!=0)
{
Initialization failed
}
if (wsadata.wversion! = wversionrequested)
{
Version mismatch
}
2. Creating Sockets
int socket (int domain, int type, int protocol);
Function Description:
The first parameter specifies the protocol family of the communication protocol used by the application, and for the TCP/IP protocol family, the parameter is af_inet;
The second parameter specifies the type of socket to be created, the stream socket type is SOCK_STREAM, the datagram socket type is SOCK_DGRAM, the original socket SOCK_RAW (the Winsock interface does not apply to a particular protocol to encapsulate it, Instead, the program handles the packet itself and the protocol header);
The third parameter specifies the communication protocol used by the application. This parameter can specify different transport protocols in a single protocol family. In the Internet communications domain, this parameter is typically 0, and the system determines which transport layer protocol should be used, depending on the type of socket.
Return value: The socket descriptor was successfully returned, otherwise Invalid_socket is returned.
Example:
if (M_sk = socket (af_inet, sock_stream, 0)) = = Invalid_socket)
{
Failed to create socket
}
3. binding Sockets
Definition of Address structure:
/*
* Socket address, Internet style.
*/
struct SOCKADDR_IN {
Short sin_family; Protocol family
U_short Sin_port; Port number
struct IN_ADDR sin_addr; Address information
Char Sin_zero[8];
};
/*
* Internet address (old style ... should be updated)
*/
struct IN_ADDR {
Union {
struct {U_char s_b1,s_b2,s_b3,s_b4;} S_un_b;
struct {u_short s_w1,s_w2;} S_un_w;
U_long s_addr;
} S_un;
#define S_ADDR S_un. S_addr
/* Can is used for most TCP & IP code */
/*
* Structure used by kernel to store most
* Addresses.
*/
struct SOCKADDR {
U_short sa_family; /* Address family */
Char sa_data[14]; /* up to bytes of direct address */
};
Note: S_ADDR is a macro that makes it easy to set up an address
#define S_ADDR S_un. S_addr
/* Can is used for most TCP & IP code */
/*
* Structure used by kernel to store most
* Addresses.
*/
struct SOCKADDR {
U_short sa_family; /* Address family */
Char sa_data[14]; /* up to bytes of direct address */
};
Note: S_ADDR is a macro that makes it easy to set up an address
The above definitions are in Winsock2.h
Binding function Prototypes:
int bind (int sockfd, const struct SOCKADDR * my_addr, socklen_t Addrlen);
Function Description:
In the argument list, SOCKFD represents the socket number (descriptor) that has been established;
MY_ADDR is a pointer to the type of sockaddr struct;
The parameter Addrlen represents the length of the MY_ADDR structure, which can be obtained with the sizeof function.
Example:
Sockaddr_in addr;
addr.sin_family = af_inet; Use Internet Protocol, IP protocol
Addr.sin_port = htons (port);
Addr.sin_addr. S_un. S_ADDR = htonl (Inaddr_any);
The IP address parameter is Inaddr_any, which is automatically specified by the system kernel.
Port is 0, the system automatically assigns a unique port number between the 1024~5000
if (Bind (M_sk, (SOCKADDR *) &addr, sizeof (addr)) = = Socket_error)
{
Binding error
}
4. Enter the listening state
int listen (int s, int backlog);
Function Description:
S: Bound sockets
Backlog: The maximum number of connections that can be processed at the same time
Listen () did not start to receive the connection, just set the socket to Listen mode, the actual receiving client connection is accept (). Usually listen () is called after the socket (), bind (), and then the Accept () is called.
5. Accept the connection
int accept (int s,struct sockaddr * addr,int * addrlen);
When a new connection arrives, accept returns a new socket descriptor. Information about the connection request side is stored in addr.
Parameter SOCKFD
The parameter sockfd is a listening socket, which is used to listen to a port that uses this port number when a client connects to the server, and this port number is associated with the socket. Of course, the customer does not know the details of the socket, it only knows an address and a port number.
Parameter addr
This is a result parameter, it is used to accept a return value, this return value specifies the address of the client, of course, this address is described by an address structure, the user should know what the address structure. If you are not interested in the customer's address, you can set this value to NULL.
Parameter len
is also the parameter of the result, which is used to accept the size of the structure of the above addr, which indicates the number of bytes that the addr structure occupies. Similarly, it can also be set to null.
If accept returns successfully, the server and the customer have established a connection correctly, and the server completes the communication with the customer via the socket returned by the Accept
6. send and receive data
int send (int s, const void *buf, int len, int flags);
int recv (int s, void *buf, int len, int flags);
The function returns the number of bytes actually sent and received. Error returns-1, this connection needs to be closed.
The function defaults to the blocking function until the Send/receive is complete.
Note: If the Send function return value is not equal to the parameter Len, the remaining unsent information needs to be sent again
Recv will not add ' \ ' at the end after receiving the data.
7. Other APIs
WSAGetLastError ()
Gets the error status of the last failed operation
closesocket (int socket)
Close socket
WSACleanup ()
Terminating the use of the Winsock 2 DLL (Ws2_32.dll)
Windows network programming [GO]