\chapter{Socket Communication with Python}
Let's start to make our first communication application with Python.

\section{A simple program}

\subsection{server.py}
\lstinputlisting[language=Python]{data/codes/3/1server.py}

\subsection{client.py}
\lstinputlisting[language=Python]{data/codes/3/1client.py}

From the above code, we could know that if we want to create communication, we need to create a server that listens to an IP address with port first, then create a client connected to the exact IP address and port.


\section{A problem about process blocking}

The problem we encounter here is that the server will try to run forever, it won't stop by itself. It'll keep accept connections, and send a "Thank you for connection" string to any client that wants to connect it.

In that case, you can only pre-define that communication processing. Say if the client sends you a "How are you?", you reply "I'm fine."

It's not cool, especially when you want to chat with someone in real-time. Because when it comes to chatting, there must have a two-way flow of information, one man needs to not only send a message, he has also had to receive a message from others.

\subsection{Thread}
The definition: In computer science, a thread of execution is the smallest sequence of programmed instructions that can be managed independently by a scheduler, which is typically a part of the operating system.

\subsubsection{Simple Example}
\lstinputlisting[language=Python]{data/codes/3/3thread_example.py}

\noindent 
The output is:

\begin{lstlisting}
22:16:00: Main    : before creating thread
22:16:00: Main    : before running thread
22:16:00: Thread: starting yeah!
22:16:00: Main    : wait for the thread to finish
22:16:00: Main    : all done
22:16:02: Thread: finishing yeah!
\end{lstlisting}

The thread function can be running separately from the main process. We can use this feature to create nodes that be able to do both side communication.

\subsubsection{Add it to server.py}
\lstinputlisting[language=Python]{data/codes/3/2server.py}

\subsubsection{Add it to client.py}
\lstinputlisting[language=Python]{data/codes/3/2client.py}

\subsubsection{Conclusion}
In this way, we have achieved two-way communication. The sender can send a message at any time, anywhere, and the receiver can also send a message at any time, anywhere. And at any time if the server or client receives a new message, it will print it on the screen.

\subsection{Multiprocessing}
Threading is unsafe. Any senior programmer would agree with that.

That's why we need a more safe programming technique to achieve the same purpose: multiprocessing.

Multiprocessing is the use of two or more central processing units within a single computer system. 

This term also refers to the ability of a system to support more than one processor or the ability to allocate tasks between multiple-processors.

With the help of multi-process technology, we can also run two independent operations in one program.

\subsubsection{A simple example}
\lstinputlisting[language=Python]{data/codes/3/3multiprocessing_example.py}

\noindent 
The result is:

\begin{lstlisting}
3 to the power of 2 is: 
9
\end{lstlisting}

\subsubsection{Add it to server}
\lstinputlisting[language=Python]{data/codes/3/4server.py}

\subsubsection{Add it to client}
\lstinputlisting[language=Python]{data/codes/3/4client.py}