Package: groovy.ui

## [Java] Class GroovySocketServer

- groovy.ui.GroovySocketServer

- All Implemented Interfaces and Traits:
    Runnable

      public class GroovySocketServer
      extends Object
      implements Runnable

  Simple server that executes supplied script against a socket.

  Typically this is used from the groovy command line agent but it can be invoked programmatically. To run this program from the command line please refer to the command line documentation at Running Groovy from the commandline.

  Here is an example of how to use this class to open a listening socket on the server, listen for incoming data, and then echo the data back to the client in reverse order:

       new GroovySocketServer(
               new GroovyShell(),      // evaluator
               false,                  // is not a file
               "println line.reverse()",         // script to evaluate
               true,                   // return result to client
               1960)                   //port

  There are several variables in the script binding:
  - line - The data from the socket
  - out - The output PrintWriter, should you need it for some reason.
  - socket - The socket, should you need it for some reason.

  The constructors that take a port bind the listening socket to the loopback address, so only clients on the same host can connect. To accept connections from other hosts, pass an explicit InetSocketAddress; note that doing so exposes the script to any peer that can reach the port, and this class performs no authentication of its own.

- - ### Constructor Summary

    Constructors
    Constructor and description
    `GroovySocketServer(GroovyShell groovy, boolean isScriptFile, String scriptFilenameOrText, boolean autoOutput, int port)` This creates and starts the socket server on a new Thread.
    `GroovySocketServer(GroovyShell groovy, GroovyCodeSource source, boolean autoOutput, int port)` This creates and starts the socket server on a new Thread.
    `GroovySocketServer(GroovyShell groovy, GroovyCodeSource source, boolean autoOutput, InetSocketAddress bindAddress)` This creates and starts the socket server on a new Thread.

  - ### Methods Summary

    Methods
    Type Params | Return Type | Name and description
    --- | --- | ---
    `` | `public void` | `run()` Runs this server.

  - ### Inherited Methods Summary

    Inherited Methods
    Methods inherited from class | Name
    --- | ---
    `class Object` | `equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### public GroovySocketServer(GroovyShell groovy, boolean isScriptFile, String scriptFilenameOrText, boolean autoOutput, int port)

      This creates and starts the socket server on a new Thread. There is no need to call run or spawn a new thread yourself.

      Parameters:
        `groovy` - The GroovyShell object that evaluates the incoming text. If you need additional classes in the classloader then configure that through this object.
        `isScriptFile` - Whether the incoming socket data String will be a script or a file path.
        `scriptFilenameOrText` - This will be a groovy script or a file location depending on the argument isScriptFile.
        `autoOutput` - whether output should be automatically echoed back to the client
        `port` - the port to listen on, bound to the loopback address

    - #### public GroovySocketServer(GroovyShell groovy, GroovyCodeSource source, boolean autoOutput, int port)

      This creates and starts the socket server on a new Thread. There is no need to call run or spawn a new thread yourself.

      Parameters:
        `groovy` - The GroovyShell object that evaluates the incoming text. If you need additional classes in the classloader then configure that through this object.
        `source` - GroovyCodeSource for the Groovy script
        `autoOutput` - whether output should be automatically echoed back to the client
        `port` - the port to listen on, bound to the loopback address
      Since:
        2.3.0

    - #### public GroovySocketServer(GroovyShell groovy, GroovyCodeSource source, boolean autoOutput, InetSocketAddress bindAddress)

      This creates and starts the socket server on a new Thread. There is no need to call run or spawn a new thread yourself.

      Binding to anything other than a loopback address accepts connections from other hosts. The script is run once per line received, and this class performs no authentication, so the port should be reachable only by peers the script is prepared to serve.

      Parameters:
        `groovy` - The GroovyShell object that evaluates the incoming text. If you need additional classes in the classloader then configure that through this object.
        `source` - GroovyCodeSource for the Groovy script
        `autoOutput` - whether output should be automatically echoed back to the client
        `bindAddress` - the address and port to listen on
      Since:
        6.0.0

  - ### Method Detail

    - #### public void run()

      Runs this server. There is typically no need to call this method, as the object's constructor creates a new thread and runs this object automatically.