Initial Winsock programming and initial winsock

Source: Internet
Author: User

Initial Winsock programming and initial winsock

1. Socket creation and Closure

You must useSocket FunctionsCreate a socket. If this function is called successfully, a socket handle is returned.

1 SOCKET socket (2 int af, // used to specify the address format used by the SOCKET, Winsock can only use AF_INET3 int type, // used to specify the socket type 4 int protocol // used with the type parameter to specify the protocol type used. It can be IPPROTO_TCP, etc. 5 );

The type parameter specifies the socket type. Socket hasStream socket, datagram socket, and original socket.

The SOCK_STREAM stream socket uses TCP to provide reliable connection transmission.

SOCK_DGRAM datagram socket, uses UDP to provide connectionless and unreliable transmission.

The original socket of SOCK_RAW. The program processes the datagram and the protocol header.

When the type parameter is explicitly SOCK_STREAM and SOCK_DGRAM, the value of protocol can be 0.

  If the function call fails, INVALID_SOCKET (-1) is returned), You can get the error code through WSAGetLastError.

When the created socket is not used, callCLOsesocket FunctionTo close the socket. If no error occurs, 0 is returned; otherwise, SOCKET_ERROR is returned.

1 int closesocket (SOCKET s); // the unique parameter of the function is the SOCKET to be closed.

2. bind the socket to the specified IP address and port number: bind Function

1 int bind (2 SOCKET s, // SOCKET handle 3 const struct sockaddr * name, // The local address to be associated 4 int namelen // The length of the address 5)

The binding function schedules a local name to the unnamed socket and establishes the local association of the socket. The local name consists of three parts:Host address, Protocol Number (TCP and UDP), and port number.

1 sockaddr_in sin;2 sin. sin_family = AF_INET; // address protocol family 3 sin. sin_port = htons (4567); // port 4 sin. sin_addr.S_un.S_addr = INADDR_ANY; // listening IP address 5 if (: bind (sListen, (LPSOCKET) & sin, sizeof (sin) = SOCKET_ERROR) 6 {7 printf ("Failed bind () \ n"); 8 return 0; 9}

3. Set the socket to enter the listening status: listen Function

1 int listen (2 SOCKET s, // SOCKET handle 3 int backlog // The maximum number of unprocessed connections allowed in the listener queue 4 );

Listen is only applicable to sockets that support connection. After the function is successfully executed, socket s enters the passive mode, and the incoming connection will be notified to wait in queue for processing.

Servers that process multiple connection requests at the same time usually use the listen function. If a connection request arrives and the queue is full, the client will receive the WSAECONNREFUSED error.

4. Accept connection requests: Accept Function

1 SOCKET accept (2 SOCKET s, // SOCKET handle 3 struct sockaddr * addr, // a pointer to the sockaddr_in structure, used to obtain the address information of the other party 4 int * addrlen // a pointer to the address Length 5 );

This function works in blocking mode by default. The function retrieves the first connection in the unprocessed connection on s, creates a socket for the connection, and returns its handle. The newly created socket is the socket that processes the actual connection and has the same attributes as s.

The addrlen parameter is used to specify the size of the space referred to by addr and return the actual length of the address. If it is NULL, no information about the remote address is returned.

After creating a socket, the client program must useConnect FunctionRequest to connect to the server.

1 int connect (2 SOCKET s, // SOCKET handle 3 const struc sockaddr FAR * name, // a pointer to the sockaddr_in structure, contains information about the server to be connected 4 int namelen // sockaddr_in structure pointer 5 );

The first parameter refers to the socket handle of the client, and the other two parameters are used to address the remote socket, that is, the server listening socket.

5. send and receive data

  For stream socketsSend and recv FunctionsTo send and receive data.

1 int send (2 SOCKET s, // SOCKET handle 3 const char FAR * buf, // buffer address of the data to be sent 4 int len, // The buffer length is 5 int flags // specify the call method, which is usually set to 06 );
1 int    recv(SOCKET s,char FAR* buf,int len,int flags);

Finally, I will explain what is socket and its function:

A socket is a basic operation unit supporting TCP/IP network communication. It can be seen as an endpoint for two-way communication between processes of different hosts. Simply put, it is an agreement between the communication parties that the communication process can be completed using functions in the socket.

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.