Winscripter
  WSH
  Forums
  Downloads
  Books
  Links
  Amazon




Login
Register

© winscripter.com
1998-2004







Script: Create a Daytime Server Using Microsoft's WinSock Control

Posted by on Sunday, January 18, 2004 (PST)

Script: Create a daytime server using Microsoft's WinSock control, available with MS Office Developer, VB, VBCC, etc.

 

/**
 *    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();
}
 
 



WSH and ADSI Administrative Scripting

New Articles
  • List installed COM objects and associated ProgIDs
    Script: Lists all COM Objects and their associated ProgIDs (If available). Win32_ClassicCOMClassSetting

  • Script: File Rotator
    Script: Rotate files where the most current file has the lowest number in the archive. When files exceed the retention period, they are deleted. Typically used for log files, backups, etc..

  • Script: Create IIS Website and DNS record
    Script: Dan Casier sent me this script that will create a website and appropriate DNS record. The script is intended for Windows 2000 Server with local DNS and necessary DNS mof installed.


  • Winscripter   |  WSH   |  Forums   |  Downloads   |  Books   |  Links   |  Amazon