⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tcpip_8cc-source.html

📁 C++ TCPIP and UDPIP library of classes
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"><title>tcpip.cc Source File</title><link href="doxygen.css" rel="stylesheet" type="text/css"></head><body bgcolor="#ffffff"><!-- Generated by Doxygen 1.2.13.1 --><h1>tcpip.cc</h1><a href="tcpip_8cc.html">Go to the documentation of this file.</a><div class="fragment"><pre>00001 00034 <font class="preprocessor">#include &lt;sys/socket.h&gt;</font>00035 <font class="preprocessor">#include &lt;arpa/inet.h&gt;</font>00036 <font class="preprocessor">#include &lt;netinet/in.h&gt;</font>00037 <font class="preprocessor">#include &lt;netdb.h&gt;</font>00038 <font class="preprocessor">#include &lt;iostream.h&gt;</font>00039 <font class="preprocessor">#include &lt;string.h&gt;</font>00040 <font class="preprocessor">#include &lt;strings.h&gt;</font>00041 <font class="preprocessor">#include &lt;stdlib.h&gt;</font>00042 <font class="preprocessor">#include &lt;assert.h&gt;</font>00043 <font class="preprocessor">#include "<a class="code" href="const_8h.html">const.h</a>"</font>00044 <font class="preprocessor">#include "<a class="code" href="tcpip_8h.html">tcpip.h</a>"</font>00045 00046 <a name="l00047"></a><a class="code" href="tcpip_8cc.html#a0">00047</a> <font class="keyword">extern</font> <font class="keywordtype">int</font> <a class="code" href="tcpip_8cc.html#a0">errno</a>;<a name="l00048"></a><a class="code" href="tcpip_8cc.html#a1">00048</a> <font class="keyword">extern</font> <font class="keywordtype">char</font>* <a class="code" href="tcpip_8cc.html#a1">sys_errlist</a>[];00049 00050 <a name="l00051"></a><a class="code" href="classtcpip.html#a0">00051</a> <a class="code" href="classtcpip.html#a0">tcpip :: tcpip</a> ()00052 {00053   <a class="code" href="classtcpip.html#o0">portbase</a> = 0;00054 }00055 <a name="l00056"></a><a class="code" href="classtcpip.html#a1">00056</a> <a class="code" href="classtcpip.html#a1">tcpip :: ~tcpip</a> ()00057 {00058 }00059 00060 <a name="l00061"></a><a class="code" href="classtcpip.html#b0">00061</a> <font class="keywordtype">int</font> <a class="code" href="classtcpip.html#b0">tcpip :: connectTCP</a> (<font class="keywordtype">char</font> *host, <font class="keywordtype">char</font> *service)00062 {00063   <font class="keywordflow">return</font> <a class="code" href="classtcpip.html#c0">connectsock</a> (host, service, <font class="stringliteral">"tcp"</font>);00064 }00065 <a name="l00066"></a><a class="code" href="classtcpip.html#b1">00066</a> <font class="keywordtype">int</font> <a class="code" href="classtcpip.html#b1">tcpip :: connectUDP</a> (<font class="keywordtype">char</font> *host, <font class="keywordtype">char</font> *service)00067 {00068   <font class="keywordflow">return</font> <a class="code" href="classtcpip.html#c0">connectsock</a> (host, service, <font class="stringliteral">"udp"</font>);00069 }00070 <a name="l00071"></a><a class="code" href="classtcpip.html#b2">00071</a> <font class="keywordtype">int</font> <a class="code" href="classtcpip.html#b2">tcpip :: passiveTCP</a> (<font class="keywordtype">char</font> *service, <font class="keywordtype">int</font> qlen)00072 {00073   <font class="keywordflow">return</font> <a class="code" href="classtcpip.html#c1">passivesock</a> (service, <font class="stringliteral">"tcp"</font>, qlen);00074 }00075 <a name="l00076"></a><a class="code" href="classtcpip.html#b3">00076</a> <font class="keywordtype">int</font> <a class="code" href="classtcpip.html#b3">tcpip :: passiveUDP</a> (<font class="keywordtype">char</font> *service)00077 {00078   <font class="keywordflow">return</font> <a class="code" href="classtcpip.html#c1">passivesock</a> (service, <font class="stringliteral">"udp"</font>, 0);00079 }00080 <a name="l00081"></a><a class="code" href="classtcpip.html#c0">00081</a> <font class="keywordtype">int</font> <a class="code" href="classtcpip.html#c0">tcpip :: connectsock</a> (<font class="keywordtype">char</font> *host, <font class="keywordtype">char</font> *service, <font class="keywordtype">char</font>* protocol)00082 {00083   <font class="keyword">struct </font>hostent *phe;00084   <font class="keyword">struct </font>servent *pse;00085   <font class="keyword">struct </font>protoent *ppe;00086   <font class="keyword">struct </font>sockaddr_in sin;00087   <font class="keywordtype">int</font> s;00088   <font class="keywordtype">int</font> type;00089   bzero ((<font class="keywordtype">char</font>*) &amp;sin, <font class="keyword">sizeof</font> (sin));00090   sin.sin_family = AF_INET;00091   <font class="keywordflow">if</font> ((pse = getservbyname (service, protocol)))00092     sin.sin_port = pse -&gt; s_port;00093   <font class="keywordflow">else</font> <font class="keywordflow">if</font> ((sin.sin_port = htons ((ushort) atoi (service))) == 0)00094     {00095       cerr &lt;&lt; <a class="code" href="const_8h.html#a0">_NET_ERR_</a> &lt;&lt; <font class="stringliteral">"can't get \""</font> &lt;&lt; service &lt;&lt; <font class="stringliteral">"\" service entry.\n"</font>;00096       exit (1);00097     }00098   <font class="keywordflow">if</font> ((phe = gethostbyname (host)))00099     bcopy (phe -&gt; h_addr, (<font class="keywordtype">char</font>*) &amp;sin.sin_addr, phe -&gt; h_length);00100   <font class="keywordflow">else</font> <font class="keywordflow">if</font> ((sin.sin_addr.s_addr = inet_addr (host)) == INADDR_NONE)00101     {00102       cerr &lt;&lt; <a class="code" href="const_8h.html#a0">_NET_ERR_</a> &lt;&lt; <font class="stringliteral">"can't get \""</font> &lt;&lt; host &lt;&lt; <font class="stringliteral">"\" host entry.\n"</font>;00103       exit (1);00104     }00105   <font class="keywordflow">if</font> ((ppe = getprotobyname (protocol)) == 0)00106     {00107       cerr &lt;&lt; <a class="code" href="const_8h.html#a0">_NET_ERR_</a> &lt;&lt; <font class="stringliteral">"can't get \""</font> &lt;&lt; protocol &lt;&lt; <font class="stringliteral">"\" protocol entry.\n"</font>;00108       exit (1);00109     }00110   <font class="keywordflow">if</font> (strcmp (protocol, <font class="stringliteral">"udp"</font>) == 0)00111     type = SOCK_DGRAM;00112   <font class="keywordflow">else</font>00113     type = SOCK_STREAM;00114   s = socket (PF_INET, type, ppe -&gt; p_proto);00115   <font class="keywordflow">if</font> (s&lt;0)00116     {00117       cerr &lt;&lt; <a class="code" href="const_8h.html#a0">_NET_ERR_</a> &lt;&lt; <font class="stringliteral">"can't create socket: "</font> &lt;&lt; <a class="code" href="tcpip_8cc.html#a1">sys_errlist</a> [<a class="code" href="tcpip_8cc.html#a0">errno</a>] &lt;&lt; <font class="stringliteral">"\n"</font>;00118       exit (1);00119     }00120   <font class="keywordflow">if</font> (connect (s, (<font class="keyword">struct</font> sockaddr *) &amp;sin, <font class="keyword">sizeof</font> (sin)) &lt; 0)00121     {00122       cerr &lt;&lt; <a class="code" href="const_8h.html#a0">_NET_ERR_</a> &lt;&lt; <font class="stringliteral">"can't connect to "</font> &lt;&lt; host &lt;&lt; <font class="stringliteral">" "</font> &lt;&lt; service &lt;&lt; <font class="stringliteral">": "</font> &lt;&lt; <a class="code" href="tcpip_8cc.html#a1">sys_errlist</a> [<a class="code" href="tcpip_8cc.html#a0">errno</a>] &lt;&lt; <font class="stringliteral">"\n"</font>;00123       exit (1);00124     }00125   <font class="keywordflow">return</font> s;00126 }00127 <a name="l00128"></a><a class="code" href="classtcpip.html#c1">00128</a> <font class="keywordtype">int</font> <a class="code" href="classtcpip.html#c1">tcpip :: passivesock</a> (<font class="keywordtype">char</font> *service, <font class="keywordtype">char</font> *protocol, <font class="keywordtype">int</font> qlen)00129 {00130   <font class="keyword">struct </font>servent *pse;00131   <font class="keyword">struct </font>protoent *ppe;00132   <font class="keyword">struct </font>sockaddr_in sin;00133   <font class="keywordtype">int</font> s;00134   <font class="keywordtype">int</font> type;00135   bzero ((<font class="keywordtype">char</font>*) &amp;sin, <font class="keyword">sizeof</font> (sin));00136   sin.sin_family = AF_INET;00137   <font class="keywordflow">if</font> ((pse = getservbyname (service, protocol)))00138     sin.sin_port = htons (ntohs ((u_short) pse -&gt; s_port) + <a class="code" href="classtcpip.html#o0">portbase</a>);00139   <font class="keywordflow">else</font> <font class="keywordflow">if</font> ((sin.sin_port = htons ((ushort) atoi (service))) == 0)00140     {00141       cerr &lt;&lt; <a class="code" href="const_8h.html#a0">_NET_ERR_</a> &lt;&lt; <font class="stringliteral">"can't get \""</font> &lt;&lt; service &lt;&lt; <font class="stringliteral">"\" service entry.\n"</font>;00142       exit (1);00143     }00144   <font class="keywordflow">if</font> ((ppe = getprotobyname (protocol)) == 0)00145     {00146       cerr &lt;&lt; <a class="code" href="const_8h.html#a0">_NET_ERR_</a> &lt;&lt; <font class="stringliteral">"can't get \""</font> &lt;&lt; protocol &lt;&lt; <font class="stringliteral">"\" protocol entry.\n"</font>;00147       exit (1);00148     }00149   <font class="keywordflow">if</font> (strcmp (protocol, <font class="stringliteral">"udp"</font>) == 0)00150     type = SOCK_DGRAM;00151   <font class="keywordflow">else</font>00152     type = SOCK_STREAM;00153   s = socket (PF_INET, type, ppe -&gt; p_proto);00154   <font class="keywordflow">if</font> (s&lt;0)00155     {00156       cerr &lt;&lt; <a class="code" href="const_8h.html#a0">_NET_ERR_</a> &lt;&lt; <font class="stringliteral">"can't create socket: "</font> &lt;&lt; <a class="code" href="tcpip_8cc.html#a1">sys_errlist</a> [<a class="code" href="tcpip_8cc.html#a0">errno</a>] &lt;&lt; <font class="stringliteral">"\n"</font>;00157       exit (1);00158     }00159   <font class="keywordflow">if</font> (bind (s, (<font class="keyword">struct</font> sockaddr *) &amp;sin, <font class="keyword">sizeof</font> (sin)) &lt; 0)00160     {00161       cerr &lt;&lt; <a class="code" href="const_8h.html#a0">_NET_ERR_</a> &lt;&lt; <font class="stringliteral">"can't bind to "</font> &lt;&lt; service &lt;&lt; <font class="stringliteral">"port: "</font> &lt;&lt; <a class="code" href="tcpip_8cc.html#a1">sys_errlist</a> [<a class="code" href="tcpip_8cc.html#a0">errno</a>] &lt;&lt; <font class="stringliteral">"\n"</font>;00162       exit (1);00163     }00164   <font class="keywordflow">if</font> (type == SOCK_STREAM &amp;&amp; listen (s, qlen) &lt; 0)00165     {00166       cerr &lt;&lt; <a class="code" href="const_8h.html#a0">_NET_ERR_</a> &lt;&lt; <font class="stringliteral">"can't listen on "</font> &lt;&lt; service &lt;&lt; <font class="stringliteral">"port: "</font> &lt;&lt; <a class="code" href="tcpip_8cc.html#a1">sys_errlist</a> [<a class="code" href="tcpip_8cc.html#a0">errno</a>] &lt;&lt; <font class="stringliteral">"\n"</font>;00167       exit (1);00168     }00169   <font class="keywordflow">return</font> s;00170 }00171 </pre></div><hr><address><small>&copy; 2002 &nbsp; Marcin Caban 'Cabko' and Borys Wisniewski 'Boria'</small></address></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -