/**
* Script: daytimesrv.js
* Purpose: Create a daytime server - using the WinSock control
* available with MS Office Dev, VB, VBCC, etc.. You
* can create a simple server.
* Author: Daren Thiel
* Date: 31 Mar 2001
* Web: http://www.winscripter.com
*
* Usage: cscript daytimesrv.js
*
* Note: Script will run from either cscript or wscript.
* To kill: Cmd Line -> CTRL-C
* GUI -> Task Manager and End task.
**/
// Define Winsock contstants - given for reference
var sckClosed = 0;// Default. Closed
var sckOpen = 1;// Open
var sckListening = 2;// Listening
var sckConnectionPending = 3;// Connection pending
var sckResolvingHost = 4;// Resolving host
var sckHostResolved = 5;// Host resolved
var sckConnecting = 6;// Connecting
var sckConnected = 7;// Connected
var sckClosing = 8;// Peer is closing the connection
var sckError = 9;// Error
// Create WinSock - Remember this Winsock is a wrapper around
// the WinSock2 API and not full featured. Bind events to
// functions which start with "wsock_"
var socket = WScript.CreateObject("MSWinsock.Winsock", "wsock_");
// Bind the socket to the standard daytime server port.
// Ahhh Windows... Lets you bind to any port you want ;)
socket.Bind( 13 );
// Begin listening for connections
socket.Listen();
// Set up infinite loop. Continue until error
while( socket.State != sckError )
{
// Give the system a chance...
WScript.Sleep( 200 );
}
// Handle a connection.
function wsock_ConnectionRequest( reqid )
{
// Check the status of our socket
if( socket.State != sckClosed )
socket.Close();
// Accept the connection
socket.Accept( reqid );
// Build and send message to client
var datetime = new String( new Date().toLocaleString() );
socket.SendData( datetime );
// Hang onto the connection for 1 second. With no delay
// the client will never get the data because the socket will
// close too soon.
WScript.Sleep( 1000 );
// Close and keep listening
socket.Close();
socket.Listen();
}