"MFC network programming" study diary 1

1. Create a socket - socket()
function: Create a new socket before use.
Format: SOCKET PASCAL FAR socket(int af,int type,int procotol);
Parameters: af: The area where the communication occurs
type: to Established socket type
procotol: the specific protocol used
2. Specify the local address - bind()
function: associate the socket address with the created socket number.
Format: int PASCAL FAR bind(SOCKET s, const struct sockaddr FAR name, int
namelen);
Parameters: s: The socket descriptor (socket number) returned by socket() call and not connected.
Others: 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()
functions: complete the connection work together
格式: int PASCAL FAR connect(SOCKET s,const struct sockaddr FAR
name,int
namelen);
SOCKET PASCAL FAR accept(SOCKET s,struct sockaddr FAR name,int FAR
addrlen);

4. Listening for connections - listen()
function: used for connection-oriented servers, indicating that it is willing to receive connections.
Format: int PASCAL FAR listen(SOCKET s, int backlog);

5. Data transmission - send() and recv()
functions: data transmission and reception
format: intPASCAL FARsend(SOCKETs,const charFAR buf,int len,int flags);
int PASCAL FAR recv(SOCKETs,const char FAR
buf ,int len,int flags);
Parameters: buf: Pointer to the buffer that stores the transmission 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 FAR
writefds,
fd_set FAR exceptfds,const struct timeval FAR timeout);
parameters: readfds: pointer to read detection
writefds: pointer to write detection
exceptfds : Pointing to the pointer to detect whether there is an error
timeout: maximum waiting time
7. Close the socket - closesocket()
function: close the socket s
format: BOOL PASCAL FAR closesocket(SOCKET s) ;

2.2 Socket call sequence diagram of connectionless protocol

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324863455&siteId=291194637