与 TikZ 的连接握手图

与 TikZ 的连接握手图

我试图在 TikZ 上获取以下图表。但是,我没有找到任何能够绘制类似内容的包。我找到的最相似的是使用 的序列图pgf-umlsd

握手

下面的代码是获取图表的最佳尝试。问题是我需要线条对角线显示连接延迟。

我如何使用 TikZ 实现这一点?

\documentclass[tikz, border=2mm]{standalone}

\usetikzlibrary{arrows,shadows}
\usepackage{pgf-umlsd}

\begin{document}
    \begin{sequencediagram}
        \newthread[white]{c}{Initiator}
        \newinst[4]{s}{Receiver}

        \mess{c}{FIN}{s}
        \mess{s}{ACK}{c}
        \mess{s}{FIN}{c}
        \mess{c}{ACK}{s}

    \end{sequencediagram}

\end{document}

在此处输入图片描述

类似问题

网络/消息序列图提出了同样的问题,但解决办法是使用msc。但是,我想要一个 TikZ 解决方案(我认为它看起来更好)。

协议消息图-箭头颜色有一个类似的解决方案pgf-umlsd,但它有仅使用水平线的相同限制。

答案1

\mess命令有一个可选参数,表示delay传输和接收之间的(垂直分离)。你只需要使用它:

\mess[1]{c}{FIN}{s}

使用它的示例以及有关插入注释和其他一些样式更改的说明,请参见使用 pgf-umlsd 时添加条形图和注释

编辑

虽然 John Kormylo 比我快。但我也认为有时我更容易画它用手而不是使用pgf-umlsd

\documentclass[tikz, border=5mm]{standalone}

\usetikzlibrary{arrows,shadows,positioning}

\begin{document}

\begin{tikzpicture}[font=\sffamily,>=stealth',thick,
commentl/.style={text width=3cm, align=right},
commentr/.style={commentl, align=left},]
\node[] (init) {\LARGE Initiator};
\node[right=1cm of init] (recv) {\LARGE Receiver};


\draw[->] ([yshift=-1.7cm]init.south) coordinate (fin1o) -- ([yshift=-.7cm]fin1o-|recv) coordinate (fin1e) node[pos=.3, above, sloped] {FIN};

\draw[->] ([yshift=-.3cm]fin1e) coordinate (ack1o) -- ([yshift=-.7cm]ack1o-|init) coordinate (ack1e) node[pos=.3, above, sloped] {ACK};

\draw[->] (ack1e-|recv) coordinate (fin2o) -- ([yshift=-.7cm]fin2o-|init) coordinate (fin2e) node[pos=.3, above, sloped] {FIN};

\draw[->] ([yshift=-.3cm]fin2e) coordinate (ack2o) -- ([yshift=-.7cm]ack2o-|recv) coordinate (ack2e) node[pos=.3, above, sloped] {ACK};

\draw[thick, shorten >=-1cm] (init) -- (init|-ack2e);
\draw[thick, shorten >=-1cm] (recv) -- (recv|-ack2e);

\draw[dotted] (recv.285)--([yshift=2mm]recv.285|-fin1e) coordinate[pos=.5] (aux1);

\draw[dotted] (init.255)--([yshift=2mm]init.255|-fin1o);

\draw[dotted] ([yshift=1mm]init.255|-fin2e) --([yshift=-5mm]init.255|-ack2e) coordinate (aux2);

\node[commentr, right =2mm of ack2e] {\textbf{CLOSED}};
\node[commentr, right =2mm of fin2o] {\textbf{LAST ACK}};
\node[below left = 0mm and 2mm of init.south, commentl]{\textbf{ESTABLISHED}\\[-1.5mm]{\itshape connection}};
\node[left = 2mm of fin1o.west, commentl]{{\itshape active close}\\[-1mm]\textbf{FIN\_WAIT\_1}};
\node[left = 2mm of ack1e.west, commentl]{\textbf{FIN\_WAIT\_2}};
\node[below left = -1mm and 2mm of fin2e.west, commentl]{\textbf{TIME\_WAIT}};
\node[below left = -1mm and 2mm of aux2-|init, commentl]{\textbf{CLOSED}};

\node[right = 2mm of recv|-aux1, commentr]{\textbf{ESTABLISHED}\\[-1.5mm]{\itshape connection}};
\node[right = 2mm of fin1e.west, commentr]{\textbf{CLOSE\_WAIT}\\[-1mm]{\itshape passive close}};
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案2

您总是可以用困难的方式做到这一点。使用命名坐标可以更轻松地进行更改。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc,arrows}

\begin{document}
\begin{center}
\begin{tikzpicture}[>=latex]
\coordinate (A) at (2,5);
\coordinate (B) at (2,0);
\coordinate (C) at (6,5);
\coordinate (D) at (6,0);
\draw[thick] (A)--(B) (C)--(D);
\draw (A) node[above]{\Large Initiator};
\draw (C) node[above]{\Large Receiver};

\coordinate (E) at ($(A)!.1!(B)$);
\draw (E) node[left]{\begin{tabular}{r}
\textit{active close}\\
\verb$FIN_WAIT_1$
\end{tabular}};

\coordinate (F) at ($(C)!.25!(D)$);
\draw (F) node[right]{\begin{tabular}{l}
\verb$CLOSE_WAIT$\\
\textit{passive close}
\end{tabular}};
\draw[->] (E) -- (F) node[midway,sloped,above]{\verb$FIN$};

\coordinate (G) at ($(A)!.4!(B)$);
\draw (G) node[left]{\verb$FIN_WAIT_2$};
\draw[->] (F) -- (G) node[midway,sloped,above]{\verb$ACK$};

\coordinate (H) at ($(C)!.5!(D)$);
\draw (H) node[right]{\verb$LAST_ACK$};

\coordinate (I) at ($(A)!.7!(B)$);
\draw (I) node[left]{\verb$TIME_WAIT_2$};
\draw[->] (H) -- (I) node[midway,sloped,above]{\verb$FIN$};

\coordinate (J) at ($(C)!.9!(D)$);
\draw (J) node[right]{\verb$CLOSED$};
\draw[->] (I) -- (J) node[midway,sloped,above]{\verb$ACK$};
\end{tikzpicture}
\end{center}
\end{document} 

图表

相关内容