linux-notebook% netcat -l 6666In a terminal on another Linux machine, or on your Wi-Fi-connected smartphone, start Netcat (note: Netcat is called “nc” on Android version 4.1.2) with the notebook's IP address and the same port number.
rob-android% nc 192.168.10.102 6666After hitting Return, you can type on the Android's terminal. The text will show up on the Linux notebook terminal. Likewise, typing on the Linux notebook displays in the Android terminal app.
Other ApproachesYou can also use Netcat in a one-sided way. Such was the case of data-flow troubleshooting between my Arduino Yun and my Linux notebook. I had uploaded an Arduino script to the Yun to read in characters and interpret them as commands, thereby controlling two servos and an LED. The program was looking for a number from 0 to 180 followed by a single letter. An “A” activated the pan servo, a “B” activated the tilt servo, and an “l” toggled the LED. I also put together a Processing script on the Linux notebook, displaying two sliders to control the servos and a button to turn the LED on and off. Neither the connection to the Yun nor data flowing from the sliders worked properly. Servo movement was jerky as I moved a slider and it certainly didn't work in real time. As a matter of fact, there was a 10- or 20-second delay. To get a handle on what was happening, I decided to send just individual commands to the Yun and see how the servo performed. To accomplish this, I ran Netcat as a client, connecting to the Arduino servo (server) script running on the Yun.
linux-notebook% netcat 192.168.240.1 6666Upon connecting to the Yun server, I could then send commands like “1l”, “90A”, “180B”, and so on. “1l” turned on the LED, while the other two moved the respective servos to new positions, thus proving that my connection worked properly. After a little while, I noticed that the servos were behind by one command. In other words, the first command, after connecting, would not move the servo. The second command would move the servo to the previously sent command position. Everything was out of step by one command. This led to my re-ordering a case statement in the Arduino script to eliminate the lag. Near real-time control is tough when you’re a step behind on each command. Getting the servos to respond immediately using discrete commands also showed that I was flooding the Yun with unnecessary instructions, leading to the time delays. The Processing slider program was changed to send a command only when a slider was moved or the lamp button was pushed. No movement or button push, no data sent. Another horrendous lag problem solved. After making these changes, the servos moved in sync with the sliders over a W-iFi connection. Pretty cool. Netcat is a quick little program that should be in every admin's or developer's tool kit. While I only covered the basics here, there are a ton of options you’ll want to explore. One that’s particularly interesting is the ability to add network communication capabilities to applications that don't have them natively by simply piping data through Netcat. Give Netcat a try. You'll find it very useful.
- Building a Simple Slider Servo Control
- Processing: A Visual Language, Not Just for Artists
- Intro to Pipes on the Linux Command Line
- 8 Practical Linux Netcat NC Command Examples [Geek Stuff]
- Using BusyBox Version of Netcat for Listening TCP Port [Stack Overflow]
- Netcat for Windows [Jon Craton]