WINSOCK program design preliminary <4> Winsock function usage

zhaozj2021-02-11  193

WSAStartup ()

Connect the first function of the application and Winsock.dll.

Format:

INT WSASTARTUP (Word WversionRequested, LPWSADATA LPWSADATA)

Parameter:

WVersionRequested Windows Sockets API version wants to use

LPWSADATA pointing to the indicator of WSADATA information

Passage value:

Success - 0

Failure - WSASYSNOTREADY / WSAVERNOTSUPPORTED / WSAEINVAL

Description:

This function "must" is the first application call to the Windows Sockets DLL function.

After a function call is successful, you can call the function of other Windows Sockets DLLs. This function also allows users to specify the Windows Sockets API version to use.

And some information for the designer.

Socket ()

Establish Socket.

Format:

Socket Socket (int AF, INT TYPE, INT Protocol)

Parameter:

AF currently only provides PF_INET (AF_INET)

Type Socket (SOCK_STREAM, SOCK_DGRAM)

Protocol Communication Agreement (if the user is not specified, set to 0)

Passage value:

Success - Socket identification code

Failure - INVALID_SOCKET (Call Wsagetlasterror () can know the reason)

Description:

This function is used to establish a socket and build the resources they use for this Socket.

Socket types can be Stream Socket or DataGram Socket.

Bind ()

Specifies the local address (address) of the SOCKET.

Format:

INT Bind (Socket S, Const Struct Sockaddr Far * Name, Int Namelen);

Parameter:

S socket identification code

Name Socket address value

Namelen Name length

Passage value:

Success - 0

Failure - Socket_ERROR (Call WsageTlasterror () can know the reason)

Description:

This function is the specified local address and port gives a unnamed Socket. If the user is not

At the value of the address or port, then he can set the address as INAddr_any, and port to 0; then Windows Sockets will automatically set it the appropriate address and port (1024 to 5000), the user can here After the SOCKET is really connected, call GetSockName () to know the value set.

Bind () function To specify the address and port, this address must be the IP of the machine to execute this program

Address, so if the reader can set the address to INADDR_any when designing the program, so

The Winsock system will automatically fill the correct address of the machine. If you want the program only on a machine

If executed, then set the address to the IP address of the machine. Since this end is a Server end,

Taking the owners must specify a port number to this Socket.

Listen ()

Set Socket is the listening state, ready to be connected.

Format:

INT Listen (socket S, int backlog);

Parameter:

S socket identification code

BACKLOG is not really completed before the connection (not yet called accept)

Number

Passage value:

Success - 0

Failure - Socket_ERROR (Call Wsagetlasterror () can beware of the reason)

Users can use this function to set the socket to enter the listening state, and how much can be set up to

A connection requirement that does not really complete the end before the connection. (At present, the maximum limit is 5, the minimum is 1)

Connect ()

Request to connect a TCP Socket to the specified other party.

Format:

INT Connect (Socket S, Const Struct SockAddr

Far * name, int namelen;

Parameter:

S socket identification code

Name This Socket wants to connect the other party address

Namelen Name length

Passage value:

Success - 0

Failure - Socket_ERROR (Call WsageTlasterror () can know the reason)

Description:

This function is used to establish a connection to the other party. If the specified other party address is 0, it will pass

Reply to the wrong mist. When the connection is completed, the user can use this socket to use this SOCKET to do the transfer or receive information.

accept ()

Accept a Socket connection requirement to complete the connection of Stream Socket.

Format:

Socket Accept (Scoket S, SockAddr * Addr, Int Far * Addrlen)

Parameters: s socket identification code

Addr's address of the end of the connection

The length of Addrlen Addr

Passage value:

Success - new socket identification code

Failure - INVALID_SOCKET (Call Wsagetlasterror () can know the reason)

Description:

The application calls this function to accept the Socket connection action request required by the Client side.

CloseSocket ()

Close a socket.

Format:

INT CloseSocket (Socket S);

Parameter:

S socket identification code

Passage value:

Success - 0

Failure - Socket_ERROR (Call WsageTlasterror () can know the reason)

Description:

This one is used to close some socket.

WSacleanup ()

End the use of Windows Sockets DLL.

Format:

INT WSACLANUP (VOID);

Parameter: None

Passage: Success - 0

Failure - Socket_ERROR (Call WsageTlasterror () can know the reason)

Description: When the application no longer needs to use the Windows Sockets DLL, you must call this function.

Log out in order to release the resource occupied.

Send ()

Use the connect-type Socket transfer data.

Format:

Int Send (Socket S, Const Char Far * BUF,

INT LEN, INT FLAGS;

Parameter:

S socket identification code

BUF stores the provisional area for information to be transferred

Length of Len Buf

Flags This function is called

Passage value:

Success - the length of the information sent

Failure - Socket_ERROR (Call WsageTlasterror () can know the reason)

Description:

This function is used to send information from this end to the remote end via Socket.

RECV ()

Receive information from Socket.

Format:

Int Recv (Socket S, CHAR FAR * BUF, INT LEN, INT FLAGS);

Parameter:

S socket identification code

BUF stores the length of the temporary area of ​​the received information.

Flags This function is called

Passage value:

Success - received the length of the data (0)

Failure - Socket_ERROR (Call WsageTlasterror () can know the reason)

Description:

This function receives information from a connection-type DataGram Socket or Stream Socket.

For Stream Socket, the masters can receive valid information in the current Input buffer, but their quantity does not exceed the size of the LEN.

WSAStartup ()

Connect the first function of the application with the Windows Sockets DLL.

Format:

INT WSASTARTUP (Word WversionRequested, LPWSADATA LPWSADATA);

Num: WVersionRequested can be used by the highest version of Windows Sockets API

LPWSADATA pointing to the indicator of WSADATA information

Passage value:

Success - 0

Failure - WSASYSNOTREADY / WSAVERNOTSUPPORTED / WSAEINVAL

Description:

This function "must" is the application call to the Windows Sockets DLL function.

The first one, only after this function calls successfully, you can call other Windows Sockets.

DLL function. This function also allows users to specify Windows Sockets to use.

API version, and some information for getting designers.

转载请注明原文地址:https://www.9cbs.com/read-3771.html

New Post(0)