Winsock process and function usage

Source: Internet
Author: User

Winsock process description

1. connection-oriented socket programming (TCP)

Server:

(1) create a socket)

(2) bind the socket to a specific IP address and PORT

(3) set the socket to the listening mode and prepare to accept the client's request (listen)

(4) Prepare the customer request to arrive. When the request arrives, accept the connection request and return a new socket (accept) corresponding to the connection)

(5) send/recv)

(6) Return and wait for another customer's request.

(7) Disable socket.

Client:

(1) create a socket)

(2) Send a connection request to the server (connnect)

(3) Communicate with the server (send/recv ).

(4) Disable socket.

2. connection-oriented socket programming

Server:

(1) create a socket)

(2) bind the socket to a specific IP address and PORT

(3) Use the returned socket to communicate with the client (recvfrom)

(4) Disable socket.

Client:

(1) create a socket)

(2) send data to the server (sendto)

(3) Disable socket.

 

 

Ii. Winsock function usage instructions

Wsastartup ()
Link the application to the first function of WinSock. dll.
Format:
Int wsastartup (word wversionrequested, lpwsadata)
Parameters:
Windows Sockets API version to be used by wversionrequested
Lpwsadata indicators pointing to wsadata
Return Value:
Success-0
Failed-wsasysnotready/wsavernotsupported/wsaeinval
Note:
This function must be called by the application to call the first function in the Windows Sockets DLL function before calling other Windows Sockets DLL functions. This function allows you to specify the version of the Windows Sockets API to be used and obtain information about the designer.

Socket ()
Create a socket.
Format:
Socket socket (int af, int type, int Protocol)
Parameters:
AF currently only provides pf_inet (af_inet)
Type socket type (sock_stream, sock_dgram)
Protocol Communication Protocol (set to 0 if not specified by the user)
Return Value:
Success-Socket identifier
Failed-INVALID_SOCKET (call WSAGetLastError () to find the cause)
Note:
This function is used to establish a Socket and establish the resources used by the Socket. The Socket type can be Stream Socket or datemedisocket.

Inet_addr Function
Convert the 10-digit IP address of "xxx. xxx" to a 32-bit integer representation.
[Statement]
Unsigned long inet_addr (const char FAR * cp );
[Parameters]
Cp-pointer to the IP address string represented by 10 in the "xxx. xxx" Format
[Return value]
When the function is successfully called, the IP address represented by a 32-bit integer is returned (in the network byte order)
INADDR_NONE.

Gethostbyname Function
You can obtain host information from the host name.
[Statement]
Struct hostent FAR * gethostbyname (const char FAR * name );
[Parameters]
Name-pointer to the host name string
[Return value]
The host information is returned when the function is successfully called.
NULL (NULL) is returned when a failure occurs)

Bind ()
Specifies the Local Address (Address) of the Socket ).
Format:
Int bind (SOCKET s, const struct sockaddr FAR * name, int namelen );
Parameters:
S Socket identifier
Name Socket address value
Namelen name Length
Return Value:
Success-0
Failure-SOCKET_ERROR (call WSAGetLastError () to learn the cause)
Note:
This function specifies the Local address and Port to a Socket with an unknown name. If the user does not care about the address or Port value, he can set the address to INADDR_ANY and Port to 0; then, Windows Sockets automatically sets the appropriate address and Port (value between 1024 and 5000). After the Socket is actually connected, the user can call getsockname () to obtain the set value.
The bind () function must specify the address and port. The address must be the IP address of the machine on which the program is executed. Therefore, if you design a program, you can set the address to INADDR_ANY, in this way, the Winsock system automatically fills in the correct address of the machine. If you want the program to run on only one machine, set the IP address to the IP address of the machine. Because this end is the Server end, the moderators must specify a port number for this socket.

Listen ()
Set the Socket to the listening status and prepare to be connected.
Format:
Int listen (SOCKET s, int backlog );
Parameters:
S Socket identifier
Maximum number of connections required for a backlog before the connection is completed (before the accept is called)
Return Value:
Success-0
Failure-SOCKET_ERROR (call WSAGetLastError () to learn the cause)
Note:
You can use this function to set the Socket to enter the listening status, and set the maximum number of connection requirements for each end before the connection is completed. (Currently, the maximum value is 5 and the minimum value is 1)

Connect ()
A tcp Socket must be connected to the specified peer.
Format:
Int connect (SOCKET s, const struct sockaddr FAR * name, int namelen );
Parameters:
S Socket identifier
Name the target address of the Socket
Namelen name Length
Return Value:
Success-0
Failure-SOCKET_ERROR (call WSAGetLastError () to learn the cause)
Note:
This function is used to request the other party to establish a connection. If the specified peer address is 0, an error value is returned. After the connection is established, the user can use this Socket to send or receive data.

Select Function
It can be used to investigate the status of one or more sockets.
[Statement]
Int select (int nfds, fd_set FAR * readfds, fd_set FAR * writefds, fd_set FAR * limit TFDs, const struct timeval FAR * timeout );
[Parameters]
Nfds-in windows socket api, this parameter can be ignored. Generally, the NILL value is assigned.
Readfds-pointer to the accepted SOCKET Device
Writefds-pointer of the SOCKET device used to send data
Invalid TFDs-check the error status
Timeout-timeout settings
[Return value]
When a value greater than 0 is returned, it indicates the number of sockets that match the condition.
0 indicates timeout.
SOCKET_ERROR is returned when a failure occurs.

Accept ()
Accept the connection requirements of a Socket to complete the connection of Stream Socket.
Format:
SOCKET accept (SCOKET s, SOCKADDR * addr, int FAR * addrlen)
Parameters:
S Socket identifier
Addr stores the address of the peer connection
Length of addrlen addr
Return Value:
Success-new Socket identifier
Failed-INVALID_SOCKET (call WSAGetLastError () to find the cause)
Note:
Server applications call this function to accept Socket connection requests required by the Client.

Closesocket ()
Close a Socket.
Format:
Int closesocket (SOCKET s );
Parameters:
S Socket identifier
Return Value:
Success-0
Failure-SOCKET_ERROR (call WSAGetLastError () to learn the cause)
Note:
This function is used to close a Socket.

WSACleanup ()
End the use of Windows Sockets DLL.
Format:
Int WSACleanup (void );
Parameter count: None
Return Value:
Success-0
Failure-SOCKET_ERROR (call WSAGetLastError () to learn the cause)
Note:
When an application no longer needs Windows Sockets DLL, you must call this function to cancel use to release the resources it occupies.

Send ()
Use a connector Socket to send data.
Format:
Int send (SOCKET s, const char FAR * buf, int len, int flags );
Parameters:
S Socket identifier
Buf is the temporary storage area for storing the data to be transferred.
Len buf Length
Flags: How this function is called
Return Value:
Success-length of the sent data
Failure-socket_error (call wsagetlasterror () to learn the cause)
Note:
This function is used to send information from the local end to the remote end through socket.

Recv ()
Receive data from socket.
Format:
Int Recv (socket S, char far * Buf, int Len, int flags );
Parameters:
S socket identifier
The temporary storage area where the Buf stores the received data
Len Buf Length
Flags: How this function is called
Return Value:
Success-length of received data (0 if the socket of the other party is disabled)
Failure-socket_error (call wsagetlasterror () to learn the cause)
Note:
This function receives data using a connected datasync socket or stream socket. For stream socket, moderators can receive valid data in the current input buffer, but the number of data files cannot exceed the Len size.

 

 

Sendto Function
Send data using socket for UDP.
[Statement]
Int sendto (SOCKET s, const char FAR * buf, int len, int flags, const struct sockaddr FAR * to, int token );
[Parameters]
S-point to the Socket Descriptor generated by the Socket function
Buf-pointer to the buffer (array) for receiving data
Len-buffer size
Flag-call method (MSG_DONTROUTE, MSG_OOB)
To-pointer to the sender's SOCKET address
Token-size of the sender's SOCKET address
[Return value]
Returns the number of bytes that have been sent when the request is successful.
SOCKET_ERROR is returned when a failure occurs.
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.