Skip to main content
Engineering LibreTexts

3.5: Tips for Networking Experiments

  • Page ID
    43803
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    In sections related to client-side sockets and socket streams, we used interactions with a web server as an example. So, we forged an HTTP Get query and send it to the server. We chose these examples to make experiments straightforward and platform agnostic. In real scale applications, interactions involving HTTP should be coded using a higher level library such as Zinc HTTP Client/Server library that is part of the default Pharo distribution1.

    Network programming can easily scale up in complexity. Using a toolbox outside Pharo is often necessary to identify what the source of an odd behavior is. This section lists a number of Unix utilities to deal with low level network operations. Readers with a Unix machine (Linux, Mac OS X) or with Cygwin (for Windows) can use nc (or netcat), netstat and lsof for their tests.

    nc (netcat)

    nc allows one to set up either a client or a server for both TCP (default protocol) and UDP. It redirects the content of its stdin to the other side. The following snippet shows how to send 'Hello from a client' to a server on the local machine listening on port 9090.

    echo Hello from a client | nc 127.0.0.1 9090
    

    The command line below starts a server listening on port 9090 that sends 'Hi from server' to the first client to connect. It terminates after the interaction.

    echo Hi from server | nc -l 9090
    

    You can keep the server running by means of option -k. But, the string produced by the preceding echo is sent only to the first client to connect. An alternative solution is to make the nc server send text while you type. Simply evaluate the following command line:

    echo nc -lk 9090
    

    Type in some text in the same terminal where you started the server. Then, run a client in another terminal. Your text will be displayed on the client side. You can repeat these two last actions (type text at the server side, then start client) as many times as needed.

    You can even go more interactive by making the connection between a client and a server more persistent. By evaluating the following command line, the client sends every line (ended with "Enter"). It will terminate when sending the EOF signal (ctl-D).

    echo cat | nc -l 9090
    

    netstat

    This command provides information on network interfaces and sockets of your computer. It provides many statistics so one needs to use appropriate options to filter out information. The following command line allows displaying status of tcp sockets and their addresses. Note that the port numbers and addresses are separated by a dot.

    netstat -p tcp -a -n
    

    lsof

    The lsof command lists all files open in your system. This of course includes sockets, since everything is a file in Unix. Why is lsof useful, you would ask, if we already have netstat? The answer is that lsof shows the link between processes and sockets. So you can find sockets related to your program.

    The example provided by following command line lists TCP sockets. The n and P options force lsof to display host addresses and ports as numbers.

    lsof -nP -i tcp
    

    1. http://zn.stfx.eu/zn/index.html

    This page titled 3.5: Tips for Networking Experiments is shared under a CC BY-SA 3.0 license and was authored, remixed, and/or curated by Alexandre Bergel, Damien Cassou, Stéphane Ducasse, Jannik Laval (Square Bracket Associates) via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.