📄 tcp__server__demo_8c-example.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>Example Documentation</title><link href="doxygen.css" rel="stylesheet" type="text/css"></head><body><!-- Generated by Doxygen 1.2.18 --><center><a class="qindex" href="main.html">Main Page</a> <a class="qindex" href="modules.html">Modules</a> <a class="qindex" href="annotated.html">Data Structures</a> <a class="qindex" href="files.html">File List</a> <a class="qindex" href="functions.html">Data Fields</a> <a class="qindex" href="globals.html">Globals</a> <a class="qindex" href="pages.html">Related Pages</a> <a class="qindex" href="examples.html">Examples</a> </center><hr><h1>tcp_server_demo.c</h1><p>An example TCP application working as a server (waiting for an external host to establish a connection and then exchanging some data with it).<p>Basically all OpenTCP applications are divided in three separate functions:<ul><li>app_init() - initialization part of the application where variables are initialized, system resources reserved, etc.<li>app_run() - application main loop which is invoked from the main main loop. Here, event-based processingis performed where upon certain event (timer expiration, communication event...) data is usually sent from and/or other control functions are invoked.<li>app_event_listener - callback function invoked by the TCP/IP stack on events (data arrival, connection state change...)</ul>More details about this example:<p><div class="fragment"><pre><span class="comment">/*</span><span class="comment"> *Copyright (c) 2000-2002 Viola Systems Ltd.</span><span class="comment"> *All rights reserved.</span><span class="comment"> *</span><span class="comment"> *Redistribution and use in source and binary forms, with or without </span><span class="comment"> *modification, are permitted provided that the following conditions </span><span class="comment"> *are met:</span><span class="comment"> *</span><span class="comment"> *1. Redistributions of source code must retain the above copyright </span><span class="comment"> *notice, this list of conditions and the following disclaimer.</span><span class="comment"> *</span><span class="comment"> *2. Redistributions in binary form must reproduce the above copyright </span><span class="comment"> *notice, this list of conditions and the following disclaimer in the </span><span class="comment"> *documentation and/or other materials provided with the distribution.</span><span class="comment"> *</span><span class="comment"> *3. The end-user documentation included with the redistribution, if </span><span class="comment"> *any, must include the following acknowledgment:</span><span class="comment"> * "This product includes software developed by Viola </span><span class="comment"> * Systems (http://www.violasystems.com/)."</span><span class="comment"> *</span><span class="comment"> *Alternately, this acknowledgment may appear in the software itself, </span><span class="comment"> *if and wherever such third-party acknowledgments normally appear.</span><span class="comment"> *</span><span class="comment"> *4. The names "OpenTCP" and "Viola Systems" must not be used to </span><span class="comment"> *endorse or promote products derived from this software without prior </span><span class="comment"> *written permission. For written permission, please contact </span><span class="comment"> *opentcp@opentcp.org.</span><span class="comment"> *</span><span class="comment"> *5. Products derived from this software may not be called "OpenTCP", </span><span class="comment"> *nor may "OpenTCP" appear in their name, without prior written </span><span class="comment"> *permission of the Viola Systems Ltd.</span><span class="comment"> *</span><span class="comment"> *THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED </span><span class="comment"> *WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF </span><span class="comment"> *MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. </span><span class="comment"> *IN NO EVENT SHALL VIOLA SYSTEMS LTD. OR ITS CONTRIBUTORS BE LIABLE </span><span class="comment"> *FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR </span><span class="comment"> *CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF </span><span class="comment"> *SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR </span><span class="comment"> *BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, </span><span class="comment"> *WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE </span><span class="comment"> *OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, </span><span class="comment"> *EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</span><span class="comment"> *====================================================================</span><span class="comment"> *</span><span class="comment"> *OpenTCP is the unified open source TCP/IP stack available on a series </span><span class="comment"> *of 8/16-bit microcontrollers, please see <http://www.opentcp.org>.</span><span class="comment"> *</span><span class="comment"> *For more information on how to network-enable your devices, or how to </span><span class="comment"> *obtain commercial technical support for OpenTCP, please see </span><span class="comment"> *<http://www.violasystems.com/>.</span><span class="comment"> */</span><span class="preprocessor">#include <<a class="code" href="debug_8h.html">inet/debug.h</a>></span><span class="preprocessor">#include <<a class="code" href="datatypes_8h.html">inet/datatypes.h</a>></span><span class="preprocessor">#include <<a class="code" href="globalvariables_8h.html">inet/globalvariables.h</a>></span><span class="preprocessor">#include <<a class="code" href="system_8h.html">inet/system.h</a>></span><span class="preprocessor">#include <<a class="code" href="tcp__ip_8h.html">inet/tcp_ip.h</a>></span><span class="comment">/* The applications that use TCP must implement following function stubs */</span><span class="comment">/* void application_name_init (void) - call once when processor starts */</span><span class="comment">/* void application_name_run (void) - call periodically on main loop */</span><span class="comment">/* INT32 application_name_eventlistener (INT8, UINT8, UINT32, UINT32) */</span><span class="comment">/* - called by TCP input process to inform arriving data, errors etc */</span><span class="comment">/* These will probably go to some include file */</span><span class="keywordtype">void</span> tcps_demo_init(<span class="keywordtype">void</span>);<span class="keywordtype">void</span> tcps_demo_run(<span class="keywordtype">void</span>);INT32 tcps_demo_eventlistener(INT8 , UINT8 , UINT32 , UINT32 );INT8 <a class="code" href="tcp__server__demo_8c.html#a1">tcps_demo_soch</a>;UINT8 <a class="code" href="tcp__server__demo_8c.html#a2">tcps_demo_senddata</a>; <span class="preprocessor">#define TCPS_DEMO_PORT 5001 </span><span class="preprocessor"></span><span class="comment">/* Internal function used for sending data to a predefined host */</span>INT16 tcps_demo_send(<span class="keywordtype">void</span>);<span class="comment">/* Initialize resources needed for the TCP server application */</span><span class="keywordtype">void</span> tcps_demo_init(<span class="keywordtype">void</span>){ DEBUGOUT(<span class="stringliteral">"Initializing TCP server application. \r\n"</span>); <span class="comment">/* Get socket:</span><span class="comment"> * TCP_TYPE_SERVER - type of TCP socket is server</span><span class="comment"> * TCP_TOS_NORMAL - no other type of service implemented so far</span><span class="comment"> * TCP_DEF_TOUT - timeout value in seconds. If for this many seconds</span><span class="comment"> * no data is exchanged over the TCP connection the socket will be</span><span class="comment"> * closed.</span><span class="comment"> * tcps_demo_eventlistener - pointer to event listener function for</span><span class="comment"> * this socket. </span><span class="comment"> */</span> <a class="code" href="tcp__server__demo_8c.html#a1">tcps_demo_soch</a> = <a name="a0"></a><a class="code" href="group__tcp__app__api.html#a0">tcp_getsocket</a>(<a class="code" href="tcp__ip_8h.html#a39">TCP_TYPE_SERVER</a>, <a class="code" href="tcp__ip_8h.html#a18">TCP_TOS_NORMAL</a>, <a class="code" href="group__opentcp__config.html#a14">TCP_DEF_TOUT</a>, tcps_demo_eventlistener); <span class="keywordflow">if</span>( <a class="code" href="tcp__server__demo_8c.html#a1">tcps_demo_soch</a> < 0 ) { DEBUGOUT(<span class="stringliteral">"TCP server unable to get socket. Resetting!!!\r\n"</span>); <a name="a1"></a><a class="code" href="system_8h.html#a6">RESET_SYSTEM</a>(); } <span class="comment">/* Put it to listen on some port */</span> <a name="a2"></a><a class="code" href="group__tcp__app__api.html#a2">tcp_listen</a>(<a class="code" href="tcp__server__demo_8c.html#a1">tcps_demo_soch</a>,<a class="code" href="tcp__server__demo_8c.html#a0">TCPS_DEMO_PORT</a>); <span class="comment">/* for now no data sending */</span> <a class="code" href="tcp__server__demo_8c.html#a2">tcps_demo_senddata</a>=0;}<span class="keywordtype">void</span> tcps_demo_run(<span class="keywordtype">void</span>){ UINT8 i; <span class="comment">/* do maybe some other TCP server app stuff</span><span class="comment"> * .....</span><span class="comment"> */</span> <span class="keywordflow">if</span>(tcps_demo_senddata){ <span class="keywordflow">if</span>(tcps_demo_send()!=-1) <a class="code" href="tcp__server__demo_8c.html#a2">tcps_demo_senddata</a>=0; }} <span class="comment">/*</span><span class="comment"> * Event listener invoked when TCP/IP stack receives TCP data for</span><span class="comment"> * a given socket. Parameters:</span><span class="comment"> * - cbhandle - handle of the socket this packet is intended for. Check it</span><span class="comment"> * just to be sure, but in general case not needed</span><span class="comment"> * - event - event that is notified. For TCP there are quite a few possible</span>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -