Introduction to Windows Socket

Source: Internet
Author: User

There are many socket functions in windows. I have not introduced them in detail. The functions here are a brief description of their usage. For detailed usage, refer to msdn.
The main purpose here is to serve the following three application services.

Function Description:
---------

Wsastartup Function
Initialize Winsock
[Statement]
Int wsastarup (word wversionrequested, lpwsadata lpwsa

Data );
[Parameters]
Wversionrequested-the minimum version number of Winsock is required.
Lpwsadata-Winsock details
[Return value]
0 is returned when the function is successfully called.
A non-0 value is returned when the failure fails.
---

Socket Functions
Used to generate socket (soket descriptor)
[Statement]
Socket socket (int af, int type, int Protocol );
[Parameters]
Af-address family (usually used: af_inet)
Type-socket type
Sock_stream: used for TCP protocol
Sock_dgram: used for UDP protocol
Protocol-used protocol
[Return value]
When the function is successfully called, a new socket (socket descriptor) is returned)
Invalid_socket.
---

Inet_addr Function
Converts an IP address of "a. B .c.d" to a 32-bit long integer.
[Statement]
Unsigned long inet_addr (const char far * CP );
[Parameters]
CP-pointer to the IP address string
[Return value]
When the function is successfully called, the IP address represented by a 32-bit integer is returned.
Inaddr_none.
---

Gethostbyname Function
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)

---

Recv Function
Use SOCKET to accept data.
[Statement]
Int Recv (socket S, char far * Buf, int Len, int flags );
[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_peek or MSG_OOB)
[Return value]
The number of bytes received when the request is successful.
If the connection is interrupted, 0 is returned.
Socket_error is returned when a failure occurs.

---

Sendto Function
Send data.
[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.

Related Article

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.