1. Create Socket--socket ()
Function: Create a new socket before use
Format: Socket PASCAL far socket (int af,int type,int procotol);
Parameter: AF: area where communication occurs
Type: the kind of socket to be established
Procotol: The specific protocol used
2. Specify local address--bind ()
Function: Connect the socket address to the created socket size.
Format: int PASCAL far bind (SOCKET s,const struct sockaddr far
Name,int
Namelen);
Parameter: s: is the socket descriptor (socket size) returned by the socket () call and not connected.
Other: No error, bind () returns 0, otherwise socket_error
Address Structure Description:
struct SOCKADDR_IN
{
Short Sin_family;//af_inet
U_short SIN_PORT;//16 bit port number, network byte order
struct in_addr sin_addr;//32 bit IP address, network byte order
Char sin_zero[8];//reserved
}
3. Establish socket connection--connect () and accept ()
Function: Work together to complete the connection
Format: int PASCAL far connect (SOCKET s,const struct sockaddr farName,int
Namelen);
Socket PASCAL far accept (socket s,struct sockaddr FAR
Name,int Far
Addrlen);
4. Monitor Connection--listen ()
Function: Used for a connection-oriented server, indicating that it is willing to receive connections.
Format: int PASCAL far Listen (SOCKET s, int backlog);
5. Data transmission--send () and recv ()
Function: Data sending and receiving
Format: intpascal farsend (sockets,const charfar buf,int len,int flags);
int PASCAL far recv (SOCKET s,const char far buf,int len,int flags);
Parameter: BUF: Pointer to the buffer that contains the transmitted data.
6. Multiplexing--select ()
Function: Used to detect one or more socket states.
Format: int PASCAL far Select (Int. nfds,fd_set far readfds,fd_set
Writefds,
Fd_set far exceptfds,const struct timeval far timeout);
Parameter: Readfds: pointer to read detection
WRITEFDS: Pointer to write-detect
Exceptfds: Pointer to check for errors
Timeout: Maximum wait time
7. Close Socket--closesocket ()
Function: Close socket s
Format: BOOL PASCAL far closesocket (SOCKET s);
2.2 Socket call timing diagram with no connection protocol
MFC Network Programming learning Diary 1