idspsrvr.jse

来自「javascript source code part2」· JSE 代码 · 共 82 行

JSE
82
字号
//~    title: Generic iDSP Server

   /*
      Copyright 1999 by Ronald Terry Constant
      http://www.rtconstant.com
   */

   /*.
idspsrvr.jse
    syntax: sewin32.exe idspsrvr.jse [port]
     where: port - Port to which to listen for Internet connection.
            Default is DEFAULT_IDSP_PORT (defined as 1066 in idsp.jsh).
      desc: This script will start up an iDSP server on the specified port
            and service any incoming connections until they are closed.
            This script will allow any commands to be executed, so it should
            not be used in security-sensitive situations.  The script will
            run until the user cancels the script via Ctrl-C.
   example:    // Start idsp server on default port 1066
            sewin32.exe idspsrvr.jse
               // Start idsp server, specifying the default port
            secon32.exe idspsrvr.jse 1066
               // Start idsp server, specifying another port. If the default
               // is not used, then the client script must specify the
               // same port.
            secon32.exe idspsrvr.jse 1011
   .*/

#include "idsp.jsh"

function main(argc, argv)
{
      // Get port to which to listen or use default port
   var port = (argv[1]) ? Number(argv[1]) : DEFAULT_IDSP_PORT;

      // Establish connection and get new DSP object
   var serverConnection = new iDSPServer(port);

   if(serverConnection == null)
   {
         // Error message if problem connecting to port
      Screen.writeln("Unable to bind to port " + port);
   }
   else
   {
      Screen.writeln("Generic IDSP server running at " + Socket.hostName() +
                     ":" + port);
      while(true)
      {
            // idsp.jsh: Like Socket.ready() in the Socket library.
            // Like iDSPServer.accept() except that a timeout is allowed.
            // Allows user interaction. But basically checks connection.
         while(!serverConnection.ready(250)) ;

            // idsp.jsh: Like Socket.accept() in the Socket library.
            // After the connection is verified, create an idsp
            // object for the verified connection. This object is used
            // for now.
         var curConnection = serverConnection.accept();

         if(curConnection != null)
         {
            Screen.writeln("Incoming DSP request from " +
                           curConnection.dspConnection.remoteHost());

            try
            {
               while( curConnection.dspService() ) ;
               Screen.writeln("Connection closed");
            }
            catch(error)
            {
               Screen.writeln("Connection lost: " + error);
            }
         }
      }

         // Technically, it can never get here, so this line is redundant
      serverConnection.close();
   }
} //main

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?