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 )".