(1) Function socket
1 /****************************************************************2 * * Function: Create a socket for communication3 * * Parameter: domain specifies communication protocol family4 * * Type specifies socket type, stream socket Sock_stream5 * * Datagram socket Sockdgram6 * * Raw socket Sockraw7 * * Protocol protocol type (used to fill in 0)8 * * Return value: Successfully returns a nonnegative integer similar to the file descriptor, which we call9 * * Socket descriptor, short socket. Failed to return-1;Ten ****************************************************************/ A#include <sys/types.h> -#include <sys/socket.h> - the intSocketintDomainintTypeintprotocol);
(2) Function bind
1 /*******************************************************************************2 * * Function: Bind a local address to the socket3 * * Parameter: SOCKFD socket function to return the socket4 * * Addr to bind the address5 * * Addrlen address length6 * * Return value: Successful return 0, failed return-17 *******************************************************************************/8#include <sys/types.h>9#include <sys/socket.h>Ten One intBindintSOCKFD,Const structSockaddr *addr, socklen_t Addrlen);
(3) Function listen
1 /*****************************************************************************2 * * Function: Use sockets to listen for incoming connections3 * * Parameter: Socket returned by SOCKFD socket function4 * * Backlog Specifies the maximum number of connections that the kernel queues for this socket5 * * Return value: Successful return 0 failed return-16 ** 7 * * (1) In general, the Listen function should call the Accept function after calling the socket and the bind function.8 * * (2) for a given listener socket interface, the kernel maintains two queues9 * * <1> has been sent by the customer and reached the server, the server is waiting to complete the corresponding TCP three-time handshake processTen * * <2> already completed the connected queue One ** A ******************************************************************************/ -#include <sys/types.h> -#include <sys/sock.h> the - intListenintSOCKFD,intbacklog);
(4) Function accept
1 /*********************************************************************************2 * * Function: The first connection is returned from the completed connection queue and is blocked if the connection queue has been completed empty3 * * Parameter: SOCKFD server socket4 * * Addr will return to the waiting socket address5 * * Addrlen returns the socket address length of the peer6 * * Return value: Successful return non-negative integer, failed return-17 *********************************************************************************/8#include <sys/types.h>9#include <sys/scoket.h>Ten One intAcceptintSOCKFD,structSockaddr *addr, socklen_t *addrlen);
(6) Function connet
1 /***********************************************************************************2 * * Function: Used to establish a connection to the specified socket3 * * Parameter: SOCKFD identifies an disconnected socket4 * * Addr Specifies a pointer to the SOCKADDR struct body to which the socket is to be connected5 * * byte length of addrlen sockaddr struct6 ***********************************************************************************/7#include <sys/types.h>8#include <sys/socket.h> 9Ten intConnectintSOCKFD,ConstStrcut sockaddr *addr, socklen_t Addrlen);
"Linux Network Programming" Common TCP/IP network programming functions