Example window: Web-Example of the greatest possible path (server side)

Source: Internet
Author: User
Tags socket error

Take a look at the simplest example of Windows server.

// Listen on port 555, accept a listener, and then exit the program.

//////////// ///////////

# Include <windows. h>
# Include <stdio. h>

# Pragma comment (Lib, "ws2_32.lib ")

# Define default_port 555.
# Define default_buffer 8

Int main (void ){
// Initialization
Wsadata WSD;
If (wsastartup (makeword (2, 2), & WSD )! = 0 ){
Printf ("wsastartup error! /N ");
Return false;
}
Printf ("wsastartup initialization successful! /N ");

Socket ssocket = socket (af_inet, sock_stream, ipproto_ip );
If (ssocket = socket_error ){
Printf ("socket error! /N ");
Return false;
}
Printf ("socket initialization successful! /N ");

Struct sockaddr_in local;
Local. sin_addr.s_addr = htonl (inaddr_any );
Local. sin_family = af_inet;
Local. sin_port = htons (default_port );

// Set
If (BIND (ssocket, (struct sockaddr *) & Local, sizeof (local) = socket_error ){
Printf ("fixed failure! BIND error! /N ");
Return false;
}
Printf ("successful! /N ");

// Listen
If (Listen (ssocket, 10) = socket_error ){
Printf ("lost! Listen error! /N ");
Return false;
}
Printf ("successfully listened! /N ");
Printf ("Start listening.../N ");

Int naddrlen = sizeof (sockaddr_in );

// ACCEPT
Socket sclient = accept (ssocket, (struct sockaddr *) & Local, (int *) & naddrlen );
If (sclient = socket_error ){
Printf ("ACCEPT token loss! Accept error! /N ");
// Printf ("error = % d/N", wsagetlasterror ());
Return false;
}
Printf ("ACCEPT successful! /N ");

Char cbuf [default_buffer] = {'/0 '};
If (Recv (sclient, cbuf, default_buffer, 0) = socket_error ){
Printf ("received data loss! Recv error! /N ");
// Printf ("cbuf [] = % s/n", cbuf );
Return false;
}
Printf ("received data successfully! /N ");

Printf ("cbuf [] = % s/n", cbuf );
Printf ("press any key to exit.../N ");
Getchar ();

// End
Closesocket (ssocket );
Wsacleanup ();
Printf ("end! /N ");

Return true;
}
/// // EOF ////////// ////////////////////////////

In the client program, the following code is displayed: webpage-(completion port)-example (customer terminal )".

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.