connection_8c-source.html
来自「仿真人工智能是指用人工的方法和技术」· HTML 代码 · 共 276 行 · 第 1/2 页
HTML
276 行
<!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>Connection.C Source File</title><link href="doxygen.css" rel="stylesheet" type="text/css"></head><body bgcolor="#ffffff"><!-- Generated by Doxygen 1.2.12 --><center><a class="qindex" href="index.html">Main Page</a> <a class="qindex" href="hierarchy.html">Class Hierarchy</a> <a class="qindex" href="annotated.html">Compound List</a> <a class="qindex" href="files.html">File List</a> <a class="qindex" href="functions.html">Compound Members</a> <a class="qindex" href="globals.html">File Members</a> </center><hr><h1>Connection.C</h1><a href="Connection_8C.html">Go to the documentation of this file.</a><div class="fragment"><pre>00001 <font class="comment">/*</font>00002 <font class="comment">Copyright (c) 2000,2001, Jelle Kok, University of Amsterdam</font>00003 <font class="comment">All rights reserved.</font>00004 <font class="comment"></font>00005 <font class="comment">Redistribution and use in source and binary forms, with or without </font>00006 <font class="comment">modification, are permitted provided that the following conditions are met:</font>00007 <font class="comment"></font>00008 <font class="comment">1. Redistributions of source code must retain the above copyright notice, this </font>00009 <font class="comment">list of conditions and the following disclaimer. </font>00010 <font class="comment"></font>00011 <font class="comment">2. Redistributions in binary form must reproduce the above copyright notice, </font>00012 <font class="comment">this list of conditions and the following disclaimer in the documentation </font>00013 <font class="comment">and/or other materials provided with the distribution. </font>00014 <font class="comment"></font>00015 <font class="comment">3. Neither the name of the University of Amsterdam nor the names of its </font>00016 <font class="comment">contributors may be used to endorse or promote products derived from this </font>00017 <font class="comment">software without specific prior written permission. </font>00018 <font class="comment"></font>00019 <font class="comment">THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" </font>00020 <font class="comment">AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE </font>00021 <font class="comment">IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE </font>00022 <font class="comment">DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE </font>00023 <font class="comment">FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL </font>00024 <font class="comment">DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR </font>00025 <font class="comment">SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER </font>00026 <font class="comment">CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, </font>00027 <font class="comment">OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE </font>00028 <font class="comment">OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</font>00029 <font class="comment">*/</font>00043 <font class="preprocessor">#include <errno.h></font> <font class="comment">// EWOULDBLOCK</font>00044 <font class="preprocessor">#include <unistd.h></font> <font class="comment">// close</font>00045 <font class="preprocessor">#include <string.h></font> <font class="comment">// strlen</font>00046 <font class="preprocessor">#include <sys/types.h></font> <font class="comment">// socket</font>00047 <font class="preprocessor">#include <sys/socket.h></font> <font class="comment">// socket</font>00048 00049 <font class="preprocessor">#ifdef Solaris</font>00050 <font class="preprocessor"></font><font class="preprocessor">#include <sys/socket.h></font> <font class="comment">// AF_INET SOCK_DGRAM</font>00051 <font class="preprocessor">#endif</font>00052 <font class="preprocessor"></font>00053 <font class="preprocessor">#include "<a class="code" href="Connection_8h.html">Connection.h</a>"</font>00054 <font class="preprocessor">#include "<a class="code" href="Logger_8h.html">Logger.h</a>"</font> <font class="comment">// LOG</font>00055 <a name="l00056"></a><a class="code" href="Connection_8C.html#a0">00056</a> <font class="keyword">extern</font> <a class="code" href="classLogger.html">Logger</a> <a class="code" href="Connection_8C.html#a0">Log</a>; 00058 <font class="comment">/******************************************************************************/</font>00059 <font class="comment">/********************** CONNECTION ********************************************/</font>00060 <font class="comment">/******************************************************************************/</font>00061 <a name="l00063"></a><a class="code" href="classConnection.html#a0">00063</a> <a class="code" href="classConnection.html#a0">Connection::Connection</a>( )00064 {00065 <a class="code" href="classConnection.html#o1">m_iMaxMsgSize</a> = 2048;00066 }00067 <a name="l00072"></a><a class="code" href="classConnection.html#a1">00072</a> <a class="code" href="classConnection.html#a0">Connection::Connection</a>(<font class="keyword">const</font> <font class="keywordtype">char</font> *hostname, <font class="keywordtype">int</font> port, <font class="keywordtype">int</font> iMaxSize)00073 {00074 <a class="code" href="classConnection.html#o1">m_iMaxMsgSize</a> = iMaxSize;00075 <font class="keywordflow">if</font>( <a class="code" href="classConnection.html#a3">connect</a>( hostname, port ) )00076 Log.<a class="code" href="classLogger.html#a1">log</a>( 1, <font class="stringliteral">"(Connection:connection) Socket connection made with %s:%d"</font>,00077 hostname, port );00078 <font class="keywordflow">else</font>00079 Log.<a class="code" href="classLogger.html#a1">log</a>( 1, <font class="stringliteral">"(Connection:Connection) Could not create connection with %s:%d"</font>00080 , hostname, port );00081 }00082 <a name="l00084"></a><a class="code" href="classConnection.html#a2">00084</a> <a class="code" href="classConnection.html#a2">Connection::~Connection</a>()00085 {00086 <a class="code" href="classConnection.html#a4">disconnect</a>();00087 }00088 <a name="l00093"></a><a class="code" href="classConnection.html#a3">00093</a> <font class="keywordtype">bool</font> <a class="code" href="classConnection.html#a3">Connection::connect</a>(<font class="keyword">const</font> <font class="keywordtype">char</font> *host, <font class="keywordtype">int</font> port )00094 {00095 <font class="keyword">struct </font>hostent *host_ent;00096 <font class="keyword">struct </font>in_addr *addr_ptr;00097 <font class="keyword">struct </font>sockaddr_in cli_addr ;00098 <font class="keywordtype">int</font> sockfd ;00099 00100 <a class="code" href="classConnection.html#o0">m_sock</a>.<a class="code" href="struct__socket.html#m0">socketfd</a> = -1 ;00101 00102 <font class="keywordflow">if</font>( (host_ent = (<font class="keyword">struct </font>hostent*)gethostbyname(host)) === NULL)00103 {00104 <font class="comment">// if not a string, get information from IP adress.</font>00105 <font class="preprocessor">#ifdef Solaris</font>00106 <font class="preprocessor"></font> <font class="keywordflow">if</font>( inet_addr(host) == ((in_addr_t)-1) )00107 <font class="preprocessor">#else</font>00108 <font class="preprocessor"></font> <font class="keywordflow">if</font>( inet_addr(host) == INADDR_NONE )00109 <font class="preprocessor">#endif</font>00110 <font class="preprocessor"></font> {00111 cerr << <font class="stringliteral">"(Connection::connect) Cannot find host "</font> << host << endl;00112 <font class="keywordflow">return</font> <font class="keyword">false</font> ;00113 }00114 }00115 <font class="keywordflow">else</font> <font class="comment">// get the necessary information from the hostname (string)</font>00116 {00117 addr_ptr = (<font class="keyword">struct </font>in_addr *) *host_ent->h_addr_list;00118 host = inet_ntoa(*addr_ptr);00119 }00120 00121 <font class="comment">// Open UDP socket.</font>00122 <font class="keywordflow">if</font>( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)00123 {00124 cerr << <font class="stringliteral">"(Connection::connect) Cannot create socket "</font> << host << endl;00125 <font class="keywordflow">return</font> <font class="keyword">false</font> ;00126 }00127 00128 <font class="comment">// insert the information of the client</font>00129 cli_addr.sin_family = AF_INET ;00130 cli_addr.sin_addr.s_addr = htonl(INADDR_ANY) ;00131 cli_addr.sin_port = htons(0) ;00132 00133 <font class="comment">// bind the client to the server socket</font>00134 <font class="keywordflow">if</font>(bind(sockfd, (<font class="keyword">struct</font> sockaddr *) &cli_addr, <font class="keyword">sizeof</font>(cli_addr)) < 0)00135 {00136 cerr << <font class="stringliteral">"(Connection::connect) Cannot bind local address "</font> << host << endl;00137 <font class="keywordflow">return</font> <font class="keyword">false</font> ;00138 }00139 00140 <font class="comment">// Fill in the structure with the address of the server.</font>00141 <a class="code" href="classConnection.html#o0">m_sock</a>.<a class="code" href="struct__socket.html#m0">socketfd</a> = sockfd ;00142 00143 <a class="code" href="classConnection.html#o0">m_sock</a>.<a class="code" href="struct__socket.html#m1">serv_addr</a>.sin_family = AF_INET ;00144 <a class="code" href="classConnection.html#o0">m_sock</a>.<a class="code" href="struct__socket.html#m1">serv_addr</a>.sin_addr.s_addr = inet_addr(host);00145 <a class="code" href="classConnection.html#o0">m_sock</a>.<a class="code" href="struct__socket.html#m1">serv_addr</a>.sin_port = htons(port) ;00146 00147 <font class="keywordflow">return</font> <font class="keyword">true</font>;00148 }00149 <a name="l00151"></a><a class="code" href="classConnection.html#a4">00151</a> <font class="keywordtype">void</font> <a class="code" href="classConnection.html#a4">Connection::disconnect</a>( <font class="keywordtype">void</font> )00152 {00153 <font class="keywordflow">if</font> (<a class="code" href="classConnection.html#a5">isConnected</a>() )00154 {00155 close(<a class="code" href="classConnection.html#o0">m_sock</a>.<a class="code" href="struct__socket.html#m0">socketfd</a>) ;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?