% Options for packages loaded elsewhere
\PassOptionsToPackage{unicode}{hyperref}
\PassOptionsToPackage{hyphens}{url}
%
\documentclass[
]{article}
\usepackage{amsmath,amssymb}
\usepackage{iftex}
\ifPDFTeX
  \usepackage[T1]{fontenc}
  \usepackage[utf8]{inputenc}
  \usepackage{textcomp} % provide euro and other symbols
\else % if luatex or xetex
  \usepackage{unicode-math} % this also loads fontspec
  \defaultfontfeatures{Scale=MatchLowercase}
  \defaultfontfeatures[\rmfamily]{Ligatures=TeX,Scale=1}
\fi
\usepackage{lmodern}
\ifPDFTeX\else
  % xetex/luatex font selection
\fi
% Use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\IfFileExists{microtype.sty}{% use microtype if available
  \usepackage[]{microtype}
  \UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
}{}
\makeatletter
\@ifundefined{KOMAClassName}{% if non-KOMA class
  \IfFileExists{parskip.sty}{%
    \usepackage{parskip}
  }{% else
    \setlength{\parindent}{0pt}
    \setlength{\parskip}{6pt plus 2pt minus 1pt}}
}{% if KOMA class
  \KOMAoptions{parskip=half}}
\makeatother
\usepackage{xcolor}
\setlength{\emergencystretch}{3em} % prevent overfull lines
\providecommand{\tightlist}{%
  \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
\setcounter{secnumdepth}{-\maxdimen} % remove section numbering
\ifLuaTeX
  \usepackage{selnolig}  % disable illegal ligatures
\fi
\IfFileExists{bookmark.sty}{\usepackage{bookmark}}{\usepackage{hyperref}}
\IfFileExists{xurl.sty}{\usepackage{xurl}}{} % add URL line breaks if available
\urlstyle{same}
\hypersetup{
  pdftitle={linux-nc-and-python-sockets},
  hidelinks,
  pdfcreator={LaTeX via pandoc}}

\title{linux-nc-and-python-sockets}
\author{}
\date{}

\begin{document}
\maketitle

\hypertarget{linux-nc-and-python-sockets}{%
\section{linux-nc-and-python-sockets}\label{linux-nc-and-python-sockets}}

\hypertarget{linux-nc-and-python-sockets-1}{%
\section{linux-nc-and-python-sockets}\label{linux-nc-and-python-sockets-1}}

nc or netcat lets you create socket servers or connect to services via
sockets.

The nc man page is excellent. This first thing I tried was creating a
simple-chat like program between two terminals.

\hypertarget{simple-nc-chat-application}{%
\section{Simple nc chat application}\label{simple-nc-chat-application}}

Start listening (nc -l) for a connection on a port (31337).

\textbf{On terminal A}

.. code-block:: bash

nc -l 31337

Connect to (127.0.0.1) on a port (31337).

\textbf{On terminal B}

.. code-block:: bash

nc 127.0.0.1 31337

Like the man page suggests all text submitted on either terminal A or B
will appear on both terminals. Conventionally the server ``listening''
for connections is typically labeled the server. In this case neither
terminal is a server because if either closes, both close\ldots{}

\hypertarget{use-python-to-interact-with-nc}{%
\section{Use python to interact with
nc}\label{use-python-to-interact-with-nc}}

On terminal A

.. code-block:: bash

nc -l 31337

On terminal b

.. code-block:: python

python

\begin{quote}
\begin{quote}
\begin{quote}
import socket s = socket.socket( socket.AF\_INET, socket.SOCK\_STREAM )
s.connect( ( `localhost', 31337 ) ) s.send( `python says hello nc' ) 20
s.recv( 30 ) `nc says hello python\n' \# To learn more about sockets do:
help( s ) \# when you are finished run s.close()
\end{quote}
\end{quote}
\end{quote}

The constant socket.AF\_INET creates a socket which allows us to connect
to an ip/name and port. The constant socket.AF\_UNIX creates a socket
which allows us to connect via files? socket.SOCK\_STREAM is the type of
socket (tcp/ip). socket.SOCK\_DGRAM (data-gram or UDP)

\end{document}
