官术网_书友最值得收藏!

Using Groovy to start a server on the command line

In this recipe, we continue to explore the groovy command's features at one's disposal. This time, we show how to create a process capable of serving client requests through TCP/IP directly from the command line and with one line of code.

How to do it...

The command-line option that we are going to use for this purpose is -l:

  1. By using the -l option, it is trivial to start a simple socket server in Groovy:
    groovy -l 4444 -e "println new Date()"
    
  2. The previous line will start a server that listens to port 4444 and returns the date and time string for every line of data it receives from the clients:
    groovy is listening on port 4444
    
  3. In order to test whether the server actually works, you can start any telnet-like program (for example, KiTTY, if you are on Windows) to connect to a localhost on port 4444, and type any string (for example, What time is it?), and press Enter. The server should reply with a date/time string back as shown in the following screenshot:

In this way, you can quite easily organize communication channels for ad hoc notifications on different hosts.

See also

  • Executing Groovy code from the command line
  • Using Groovy as a command-line text file editor
主站蜘蛛池模板: 吉水县| 深泽县| 闽侯县| 东乡县| 四平市| 洞口县| 云龙县| 光山县| 尉氏县| 三河市| 菏泽市| 斗六市| 永定县| 玛沁县| 华容县| 高碑店市| 昌都县| 佳木斯市| 宕昌县| 巴林右旗| 瑞丽市| 武陟县| 南木林县| 油尖旺区| 金坛市| 乌鲁木齐市| 长丰县| 临汾市| 怀柔区| 彰化市| 祁阳县| 虹口区| 隆化县| 兴和县| 抚顺县| 龙游县| 台北市| 南丹县| 汾阳市| 安远县| 淮安市|