Socket network programming review and summary

Source: Internet
Author: User

TCP Section

Header file winsock32.h
Library File ws2_32.lib
(Note: Both the server and client can send and receive the following messages)

Server:

Wsadata;
Int error;

Error = wsastartup (makeword (2, 2), & wsadata );
Then judge the error value! =, Return;
Then determine if it is supported by DLL

Socket (,,)
The first parameter must be af_inet, indicating the address protocol family.
The second parameter: A. sock_dgram, B. sock_stream. A is of the UDP type, and B is of the TCP type.
The third parameter: preferably 0. It will automatically select an appropriate protocol for you based on the address type and socket type.
If the call succeeds, a sock-type data type descriptor is returned. If the call fails, an invalid_socket is returned. The error message can be returned through the wsagetlasterror function.

BIND (,,)
The first parameter is the socket to be bound.
The second parameter specifies the local address information of the socket, which is a pointer variable pointing to the sockaddr structure. For different protocols (so we need to use the third parameter sizeof (sockddr )), in TCP/IP, we can use the socketaddr_in structure to replace sockaddr.
Struct socketaddr_in
{
Short sin_family; // This is fixed as af_inet,
Unsigned short sin_port; // port number. Note that the network byte sequence must be used to call the htons function.
Struct in_addr sin_addr; // ip address. If it is specified as inaddr_any, the socket can send or receive data to or from any IP address assigned to the local machine. For machines with multiple NICs, this simplifies programming. If you only want an IP address to receive data, you must specify a specific IP address. You can use the inet_addr () function. Note that the network byte sequence must also be used here, call the htonl Function
Char sin_zero [8]; // It is only filled to ensure the length of socketaddr_in is the same as that of sockaddr.
}
If the BIND () function is successfully called, 0 is returned. If the function fails, a socket_error is returned. The same error message can be returned through the wsagetlasterror function.

Listen (,)
The first parameter is socket.
Second parameter: Waiting for connection to the longest Request queue

Call accept (,) in an infinite loop // wait for the client to connect
The first parameter is socket.
The second parameter is the client's IP address.
The length of the third parameter address structure, which must have an initial value, can be sizeof (sockaddr)
Returns a socket

Send (,,,)
The first parameter is socket (the return value of accept)
The second parameter contains the buffer of the transmitted data.
Length of the buffer data of the third parameter
The fourth parameter is flag, which affects the send behavior and is set to 0.

Recv (,,,)
The first parameter is socket (the return value of accept)
The second parameter contains the buffer of the received data.
Length of the buffer data of the third parameter
The fourth parameter flag affects the Recv behavior. It is set to 0.

Last closesocket ().

Client:
Wsastartup method call, establish a socket, which is the same as the server

Connect (,,)
The first parameter is socket.
The second parameter refers to the address structure information, which is similar to the BIND function on the server.
Length of the third parameter address Structure

Recv (,,,)
Similar to the server-side Recv Function

Send ()
Similar to the send function on the server

Closesocket
 

 

UDP part

Header file winsock32.h
Library File ws2_32.lib

Server:
The wsastartup method is called to create a socket, which is similar to that of the TCP server (the second parameter is changed to sock_dgram)

Same as bind ()

Recvfrom (,,,,,)
The first parameter is socket.
Second parameter Buf Array
Length of the third parameter Buf
The fourth parameter is flag, set to 0,
Pointer to the destination address structure of the fifth parameter (out)
The sixth integer pointer returns the size of the address structure, (In, out)

Closesocket ()

Wsacleanup ()

Client:
Wsastartup method call to create a socket

Sendto (,,,,)
The first parameter is socket.
Second parameter Buf Array
Length of the third parameter Buf
The fourth parameter is flag, set to 0,
Pointer to the destination address structure of the fifth parameter (out)
The sixth parameter refers to the length of the address struct, (In, out)

Closesocket ()

Wsacleanup ()

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.