- 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
推薦閱讀
- Java逍遙游記
- Django+Vue.js商城項目實戰
- Apache ZooKeeper Essentials
- C語言程序設計習題解析與上機指導(第4版)
- JavaScript:Functional Programming for JavaScript Developers
- Python計算機視覺編程
- INSTANT Sencha Touch
- HDInsight Essentials(Second Edition)
- PySpark Cookbook
- Simulation for Data Science with R
- 跟戴銘學iOS編程:理順核心知識點
- Tableau Desktop可視化高級應用
- Arduino機器人系統設計及開發
- 精通Spring:Java Web開發與Spring Boot高級功能
- WordPress Search Engine Optimization(Second Edition)