- Groovy 2 Cookbook
- Andrey Adamovich Luciano Fiandesio
- 220字
- 2021-07-23 15:57:19
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
:
- By using the
-l
option, it is trivial to start a simple socket server in Groovy:groovy -l 4444 -e "println new Date()"
- 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
- 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
推薦閱讀
- Learning Neo4j
- 自己動手寫Java虛擬機
- Visual Basic程序設計教程
- PyTorch自然語言處理入門與實戰
- Java技術手冊(原書第7版)
- 數據結構(Python語言描述)(第2版)
- 基于差分進化的優化方法及應用
- Access 2010數據庫基礎與應用項目式教程(第3版)
- TradeStation交易應用實踐:量化方法構建贏家策略(原書第2版)
- JavaScript入門經典
- 西門子S7-200 SMART PLC編程從入門到實踐
- 深入淺出React和Redux
- OpenCV with Python By Example
- JBoss:Developer's Guide
- Raspberry Pi Robotic Projects(Third Edition)