rt-thread/components/net/uip/doc/html/a00036.html

121 lines
12 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>uIP 1.0: hello-world.c</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
<link href="tabs.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.4.6 -->
<div class="tabs">
<ul>
<li><a href="main.html"><span>Main&nbsp;Page</span></a></li>
<li><a href="modules.html"><span>Modules</span></a></li>
<li><a href="classes.html"><span>Data&nbsp;Structures</span></a></li>
<li><a href="files.html"><span>Files</span></a></li>
<li><a href="examples.html"><span>Examples</span></a></li>
</ul></div>
<h1>hello-world.c</h1><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/**</span>
<a name="l00002"></a>00002 <span class="comment"> * \addtogroup helloworld</span>
<a name="l00003"></a>00003 <span class="comment"> * @{</span>
<a name="l00004"></a>00004 <span class="comment"> */</span>
<a name="l00005"></a>00005 <span class="comment"></span>
<a name="l00006"></a>00006 <span class="comment">/**</span>
<a name="l00007"></a>00007 <span class="comment"> * \file</span>
<a name="l00008"></a>00008 <span class="comment"> * An example of how to write uIP applications</span>
<a name="l00009"></a>00009 <span class="comment"> * with protosockets.</span>
<a name="l00010"></a>00010 <span class="comment"> * \author</span>
<a name="l00011"></a>00011 <span class="comment"> * Adam Dunkels &lt;adam@sics.se&gt;</span>
<a name="l00012"></a>00012 <span class="comment"> */</span>
<a name="l00013"></a>00013
<a name="l00014"></a>00014 <span class="comment">/*</span>
<a name="l00015"></a>00015 <span class="comment"> * This is a short example of how to write uIP applications using</span>
<a name="l00016"></a>00016 <span class="comment"> * protosockets.</span>
<a name="l00017"></a>00017 <span class="comment"> */</span>
<a name="l00018"></a>00018
<a name="l00019"></a>00019 <span class="comment">/*</span>
<a name="l00020"></a>00020 <span class="comment"> * We define the application state (struct hello_world_state) in the</span>
<a name="l00021"></a>00021 <span class="comment"> * hello-world.h file, so we need to include it here. We also include</span>
<a name="l00022"></a>00022 <span class="comment"> * uip.h (since this cannot be included in hello-world.h) and</span>
<a name="l00023"></a>00023 <span class="comment"> * &lt;string.h&gt;, since we use the memcpy() function in the code.</span>
<a name="l00024"></a>00024 <span class="comment"> */</span>
<a name="l00025"></a>00025 <span class="preprocessor">#include "<a class="code" href="a00101.html">hello-world.h</a>"</span>
<a name="l00026"></a>00026 <span class="preprocessor">#include "<a class="code" href="a00136.html">uip.h</a>"</span>
<a name="l00027"></a>00027 <span class="preprocessor">#include &lt;string.h&gt;</span>
<a name="l00028"></a>00028
<a name="l00029"></a>00029 <span class="comment">/*</span>
<a name="l00030"></a>00030 <span class="comment"> * Declaration of the protosocket function that handles the connection</span>
<a name="l00031"></a>00031 <span class="comment"> * (defined at the end of the code).</span>
<a name="l00032"></a>00032 <span class="comment"> */</span>
<a name="l00033"></a>00033 <span class="keyword">static</span> <span class="keywordtype">int</span> handle_connection(<span class="keyword">struct</span> <a name="_a100"></a><a class="code" href="a00078.html">hello_world_state</a> *s);
<a name="l00034"></a>00034 <span class="comment">/*---------------------------------------------------------------------------*/</span>
<a name="l00035"></a>00035 <span class="comment">/*</span>
<a name="l00036"></a>00036 <span class="comment"> * The initialization function. We must explicitly call this function</span>
<a name="l00037"></a>00037 <span class="comment"> * from the system initialization code, some time after uip_init() is</span>
<a name="l00038"></a>00038 <span class="comment"> * called.</span>
<a name="l00039"></a>00039 <span class="comment"> */</span>
<a name="l00040"></a>00040 <span class="keywordtype">void</span>
<a name="l00041"></a>00041 <a name="a101"></a><a class="code" href="a00162.html#gb97849f0d3ea858eee790b69591e6427">hello_world_init</a>(<span class="keywordtype">void</span>)
<a name="l00042"></a>00042 {
<a name="l00043"></a>00043 <span class="comment">/* We start to listen for connections on TCP port 1000. */</span>
<a name="l00044"></a>00044 <a name="a102"></a><a class="code" href="a00147.html#gdd1ab3704ecd4900eec61a6897d32dc8">uip_listen</a>(<a name="a103"></a><a class="code" href="a00148.html#g69a7a4951ff21b302267532c21ee78fc">HTONS</a>(1000));
<a name="l00045"></a>00045 }
<a name="l00046"></a>00046 <span class="comment">/*---------------------------------------------------------------------------*/</span>
<a name="l00047"></a>00047 <span class="comment">/*</span>
<a name="l00048"></a>00048 <span class="comment"> * In hello-world.h we have defined the UIP_APPCALL macro to</span>
<a name="l00049"></a>00049 <span class="comment"> * hello_world_appcall so that this funcion is uIP's application</span>
<a name="l00050"></a>00050 <span class="comment"> * function. This function is called whenever an uIP event occurs</span>
<a name="l00051"></a>00051 <span class="comment"> * (e.g. when a new connection is established, new data arrives, sent</span>
<a name="l00052"></a>00052 <span class="comment"> * data is acknowledged, data needs to be retransmitted, etc.).</span>
<a name="l00053"></a>00053 <span class="comment"> */</span>
<a name="l00054"></a>00054 <span class="keywordtype">void</span>
<a name="l00055"></a>00055 <a name="a104"></a><a class="code" href="a00162.html#g03070adbf8faab0f34f87c1270964306">hello_world_appcall</a>(<span class="keywordtype">void</span>)
<a name="l00056"></a>00056 {
<a name="l00057"></a>00057 <span class="comment">/*</span>
<a name="l00058"></a>00058 <span class="comment"> * The uip_conn structure has a field called "appstate" that holds</span>
<a name="l00059"></a>00059 <span class="comment"> * the application state of the connection. We make a pointer to</span>
<a name="l00060"></a>00060 <span class="comment"> * this to access it easier.</span>
<a name="l00061"></a>00061 <span class="comment"> */</span>
<a name="l00062"></a>00062 <span class="keyword">struct </span><a class="code" href="a00078.html">hello_world_state</a> *s = &amp;(<a name="a105"></a><a class="code" href="a00150.html#g788ffac72342f6172343d7f8099cbe1a">uip_conn</a>-&gt;<a name="a106"></a><a class="code" href="a00088.html#97f9e1fda815bfb8b1f4577c355ade20">appstate</a>);
<a name="l00063"></a>00063
<a name="l00064"></a>00064 <span class="comment">/*</span>
<a name="l00065"></a>00065 <span class="comment"> * If a new connection was just established, we should initialize</span>
<a name="l00066"></a>00066 <span class="comment"> * the protosocket in our applications' state structure.</span>
<a name="l00067"></a>00067 <span class="comment"> */</span>
<a name="l00068"></a>00068 <span class="keywordflow">if</span>(<a name="a107"></a><a class="code" href="a00147.html#gdb971fb1525d0c5002f52125b05f3218">uip_connected</a>()) {
<a name="l00069"></a>00069 <a name="a108"></a><a class="code" href="a00158.html#g26ae707402e494f3895a9f012a93ea29">PSOCK_INIT</a>(&amp;s-&gt;<a name="a109"></a><a class="code" href="a00078.html#a9825b5977b2d4dec66e6bd09e6ae6ea">p</a>, s-&gt;<a name="a110"></a><a class="code" href="a00078.html#4b1b1436b50ed7638aece35f41421196">inputbuffer</a>, <span class="keyword">sizeof</span>(s-&gt;<a class="code" href="a00078.html#4b1b1436b50ed7638aece35f41421196">inputbuffer</a>));
<a name="l00070"></a>00070 }
<a name="l00071"></a>00071
<a name="l00072"></a>00072 <span class="comment">/*</span>
<a name="l00073"></a>00073 <span class="comment"> * Finally, we run the protosocket function that actually handles</span>
<a name="l00074"></a>00074 <span class="comment"> * the communication. We pass it a pointer to the application state</span>
<a name="l00075"></a>00075 <span class="comment"> * of the current connection.</span>
<a name="l00076"></a>00076 <span class="comment"> */</span>
<a name="l00077"></a>00077 handle_connection(s);
<a name="l00078"></a>00078 }
<a name="l00079"></a>00079 <span class="comment">/*---------------------------------------------------------------------------*/</span>
<a name="l00080"></a>00080 <span class="comment">/*</span>
<a name="l00081"></a>00081 <span class="comment"> * This is the protosocket function that handles the communication. A</span>
<a name="l00082"></a>00082 <span class="comment"> * protosocket function must always return an int, but must never</span>
<a name="l00083"></a>00083 <span class="comment"> * explicitly return - all return statements are hidden in the PSOCK</span>
<a name="l00084"></a>00084 <span class="comment"> * macros.</span>
<a name="l00085"></a>00085 <span class="comment"> */</span>
<a name="l00086"></a>00086 <span class="keyword">static</span> <span class="keywordtype">int</span>
<a name="l00087"></a>00087 handle_connection(<span class="keyword">struct</span> <a class="code" href="a00078.html">hello_world_state</a> *s)
<a name="l00088"></a>00088 {
<a name="l00089"></a>00089 <a name="a111"></a><a class="code" href="a00158.html#g84901a5aa60040e96d272a69977edd22">PSOCK_BEGIN</a>(&amp;s-&gt;<a class="code" href="a00078.html#a9825b5977b2d4dec66e6bd09e6ae6ea">p</a>);
<a name="l00090"></a>00090
<a name="l00091"></a>00091 <a name="a112"></a><a class="code" href="a00158.html#gb0ad55aa96dd1d200cd0fc5a99f6a4f7">PSOCK_SEND_STR</a>(&amp;s-&gt;<a class="code" href="a00078.html#a9825b5977b2d4dec66e6bd09e6ae6ea">p</a>, <span class="stringliteral">"Hello. What is your name?\n"</span>);
<a name="l00092"></a>00092 <a name="a113"></a><a class="code" href="a00158.html#gb5d9c0becf7cb32d0aaef466839dd92e">PSOCK_READTO</a>(&amp;s-&gt;<a class="code" href="a00078.html#a9825b5977b2d4dec66e6bd09e6ae6ea">p</a>, <span class="charliteral">'\n'</span>);
<a name="l00093"></a>00093 strncpy(s-&gt;<a name="a114"></a><a class="code" href="a00078.html#4da86e9feb5e5835eb15163c2cb61116">name</a>, s-&gt;<a class="code" href="a00078.html#4b1b1436b50ed7638aece35f41421196">inputbuffer</a>, <span class="keyword">sizeof</span>(s-&gt;<a class="code" href="a00078.html#4da86e9feb5e5835eb15163c2cb61116">name</a>));
<a name="l00094"></a>00094 <a class="code" href="a00158.html#gb0ad55aa96dd1d200cd0fc5a99f6a4f7">PSOCK_SEND_STR</a>(&amp;s-&gt;<a class="code" href="a00078.html#a9825b5977b2d4dec66e6bd09e6ae6ea">p</a>, <span class="stringliteral">"Hello "</span>);
<a name="l00095"></a>00095 <a class="code" href="a00158.html#gb0ad55aa96dd1d200cd0fc5a99f6a4f7">PSOCK_SEND_STR</a>(&amp;s-&gt;<a class="code" href="a00078.html#a9825b5977b2d4dec66e6bd09e6ae6ea">p</a>, s-&gt;<a class="code" href="a00078.html#4da86e9feb5e5835eb15163c2cb61116">name</a>);
<a name="l00096"></a>00096 <a name="a115"></a><a class="code" href="a00158.html#g5d56800f82bfc7bbf53bb4a659589812">PSOCK_CLOSE</a>(&amp;s-&gt;<a class="code" href="a00078.html#a9825b5977b2d4dec66e6bd09e6ae6ea">p</a>);
<a name="l00097"></a>00097
<a name="l00098"></a>00098 <a name="a116"></a><a class="code" href="a00158.html#g4a264bb64ae706d53f572b1d9e4037a2">PSOCK_END</a>(&amp;s-&gt;<a class="code" href="a00078.html#a9825b5977b2d4dec66e6bd09e6ae6ea">p</a>);
<a name="l00099"></a>00099 }
<a name="l00100"></a>00100 <span class="comment">/*---------------------------------------------------------------------------*/</span>
</pre></div> <hr size="1"><address style="align: right;"><small>Generated on Mon Jun 12 10:23:01 2006 for uIP 1.0 by&nbsp;
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.6 </small></address>
</body>
</html>