Basic knowledge about windows socket server socket

Source: Internet
Author: User
Tags htons

Compiling process of the server of win socket:

1. Define the win Socket Structure and address structure: Socket serversocket, clientsocket; struct sockaddr_in socketaddress, clientaddr;

2. Load the socket Dynamic Link Library: wsastartup (makeword (), & wsadata)

3. Enter the address structure:

Socketaddress. sin_family = af_inet;

Socketaddress. sin_port = htons (servport );

Socketaddress. sin_addr.s_un.s_addr = htonl (inaddr_any );

4. Bind the IP address and port with the defined socket:

BIND (serversocket, (const sockaddr *) & socketaddress, sizeof (socketaddress ))

5. Set the socket to the listening status:

Listen (serversocket, 5)

6. Accept the request and send a response:

Clientsocket = accept (serversocket, (struct sockaddr *) & clientaddr, & Len)

Send (clientsocket, buffer, strlen (buffer), 0)

7. Example:

// Original server code
# Include <Winsock. h>
# Include <stdlib. h>
# Include <iostream>
Using namespace STD;
Int main ()
{
// Define socket;
Socket serversocket, clientsocket;
// Socket address Structure
Struct sockaddr_in socketaddress;
Struct sockaddr_in clientaddress;
Int Len;
Int SERVERPORT = 5555; // define the server port
Char buff [256];
For (INT I = 0; I <256; I ++)
{
Buff [I] = 'a ';
Cout <buff [I];
}
Cout <"/nthese the world in buff" <Endl;

// The following structure stores the socket details of Windows
Wsadata WSD;
// Servport = atoi ("servport"); // custom Port
// Call the wsastartup function to load the socket dynamic link library of the specified version. After loading, some functions in the socket
Wsastartup (makeword (1, 1), & WSD); // initialize
Serversocket = socket (af_inet, sock_stream, 0); // creat socket
// Server address struct
Memset (& socketaddress, 0, sizeof (socketaddress ));
Socketaddress. sin_family = af_inet; // TCP/IP protocol family
Socketaddress. sin_port = htons (SERVERPORT); // converts the subnode sequence of the machine to the subnode sequence of the TCP/IP network.
Socketaddress. sin_addr.s_un.s_addr = htonl (inaddr_any); // convert the IP address. The service programs in any network segment can communicate with the principal machine.
BIND (serversocket, (const sockaddr *) & socketaddress, sizeof (socketaddress); // Bind Address
Listen (serversocket, 5); // listening
// Receive looply
While (1)
{
Cout <"waiting.../N ";
Len = sizeof (clientaddress );
Clientsocket = accept (serversocket, (struct sockaddr *) & clientaddress, & Len );
Cout <"accept client:" <inet_ntoa (clientaddress. sin_addr) <"<ntohs (clientaddress. sin_port) <Endl;
// Send ....
Send (clientsocket, buff, strlen (buff), 0 );
Closesocket (clientsocket );
Cout <"[server OK]" <Endl;
}
Closesocket (serversocket );
Wsacleanup ();
Return 0;
}

 

Socket socket (int af, int type, int Protocol );

AF is used to specify the network type. Generally, af_inet is used to indicate that the socket communicates in the Internet domain. type is used to specify the socket type. If sock_stream is used, a stream socket is created, if sock_stream is used, a datagram socket is created. The Protocol parameter is used to specify the network protocol. Generally, 0 indicates that the default protocol is TCP/IP. The socket handle pointing to the socket is returned.

Int BIND (socket S, const sockaddr * Name, int namelen );

S identifies an unbundled socket handle to wait for the client to connect. Name is the address assigned to the socket. It is represented by the struct sockaddr structure. The format of this structure is as follows:

Struct sockaddr

{

U_short sa_family;

Char sa_data [4];

};

The address of this structure changes with the selected protocol. Therefore, another sockaddr_in structure with the same size as the address structure is more commonly used, the socket_in structure is used to represent the addresses under the TCP/IP protocol. Under the TCP/IP protocol, the sockaddr_in structure can be easily converted to the sockaddr structure by forced type conversion. The format of the sockaddr_in structure is as follows:

Struct sockaddr_in

{

Short sin_family;

Unsigned short sin_port;

Struct in_addr sin_addr;

Char sin_zero [8];

};

The sin_family field must be set to af_inet, indicating that the socket is in the Internet domain, the sin_port field is used to specify the server port, and the sin_addr field is used to save an IP address as a 4-byte number. Sin_zero acts as the fill item. When the IP address is inaddr_any, server applications are allowed to listen to client activity on each network interface on the host computer.

When a bound socket calls bind, A wsaeffault error is returned.

Int listen (socket S, int backlog)

S identifies a bundled unconnected socket descriptor.

 

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.