What is Jupyter notebook?
We have already checked out Jupyter couple of days ago. It is a browser based IDE and markdown editor for around 40 languages including of course JavaScript or node.js.
Node.js is just JavaScript on Linux. It works outside of the browser VM. It is really cool way to import the power of the wonderful language JavaScript and its event loop to the Linux console.
Jupyter notebook just makes any software dev fun since you can not only develop code using the browser with syntax highlighting, built in debugger, execution environment and more.
Why use Jupyter?
I want to henceforth us Jupyter for all our future coding sessions as it is so much more fun. We can also learn without it, but Jupyter just makes things a lot more convenient for everyone concerned.
You are welcome however to use Linux text console if that is what you prefer. By the way Jupyter also has a terminal available.
Moreover Jupyter also allows us to access Linux shell commands with ! operator just like Vim colon command.
How do sockets work?
Berkeley sockets are a big topic and I wish to say only some things about it here.
Every server running TCP must do a listen and accept
Only accepted sockets can communicate with clients
Each client is a different socket instance, in JavaScript a new sock obj
Client code does not have that complexity. It connects to one server and it is the same socket
We shall explore UDP sockets in a separate newsletter.
Sockets programming is fun and definitely with the JavaScript event loop it is even more so.
Sample client code
Client code. The code is explained in the Jupyter notebook but here I will tell you some things. The net object is the networking subsystem. Think of it like a C include file. Don’t worry, Jupyter ships with it.
var net = require('net');
var host = 'localhost';
var port = 8000;
var socket = new net.Socket();
socket.connect(port, host, () => {
socket.write("Hello");
});
socket.on('data', (data) => {
console.log(`${data}`);
socket.destroy();
});
Sample server code
Sample echo server code. Typical sockets programming beginner code. They also echo back what they get from the client.
Once you get a hang of this, then you can easily develop real protocols. You are welcome to send JSON back and forth and mimic a real protocol.
JSON needs to be serialized when sending thro’ sockets.
var net = require('net');
var host = '0.0.0.0';
var port = 8000;
net.createServer(sock => {
console.log(`Connection from: ${sock.remoteAddress}:${sock.remotePort}`);
sock.on('data', (data) => {
console.log(`${sock.remoteAddress}: ${data}`);
sock.write(`${data}`);
});
sock.on('close', (data) => {
console.log(`connection closed: ${sock.remoteAddress}:${sock.remotePort}`);
});
}).listen(port, host);
console.log(`Server listening on ${host}:${port}`);
How to run?
If you use node.js on Linux then it is like this.
$ node server.js
$ node client.js
If you use Jupyter notebook then simply typing CTRL-ENTER inside the code cell(when it is highlighted green) will do.
Conclusion
The code for this article as well as explanation if available as a Jupyter notebook here.
Answer
How does grep and cut work together?
$ cat mango.txt
Girl-in-the-bronx
Useless line
$ grep bronx mango.txt|cut -d- -f2
in
The f switch to cut stands for field and it is 1 based. You can give ranges too.
Question
What is top ?
Feedback
Should you wish to learn a technology area that is hard for you to grasp you are free to email me and I shall incorporate that in a future newsletter edition.