xref: /nrf52832-nimble/rt-thread/components/net/uip/doc/html/a00036.html (revision 042d53a763ad75cb1465103098bb88c245d95138)
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
3<title>uIP 1.0: hello-world.c</title>
4<link href="doxygen.css" rel="stylesheet" type="text/css">
5<link href="tabs.css" rel="stylesheet" type="text/css">
6</head><body>
7<!-- Generated by Doxygen 1.4.6 -->
8<div class="tabs">
9  <ul>
10    <li><a href="main.html"><span>Main&nbsp;Page</span></a></li>
11    <li><a href="modules.html"><span>Modules</span></a></li>
12    <li><a href="classes.html"><span>Data&nbsp;Structures</span></a></li>
13    <li><a href="files.html"><span>Files</span></a></li>
14    <li><a href="examples.html"><span>Examples</span></a></li>
15  </ul></div>
16<h1>hello-world.c</h1><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/**</span>
17<a name="l00002"></a>00002 <span class="comment"> * \addtogroup helloworld</span>
18<a name="l00003"></a>00003 <span class="comment"> * @{</span>
19<a name="l00004"></a>00004 <span class="comment"> */</span>
20<a name="l00005"></a>00005 <span class="comment"></span>
21<a name="l00006"></a>00006 <span class="comment">/**</span>
22<a name="l00007"></a>00007 <span class="comment"> * \file</span>
23<a name="l00008"></a>00008 <span class="comment"> *         An example of how to write uIP applications</span>
24<a name="l00009"></a>00009 <span class="comment"> *         with protosockets.</span>
25<a name="l00010"></a>00010 <span class="comment"> * \author</span>
26<a name="l00011"></a>00011 <span class="comment"> *         Adam Dunkels &lt;[email protected]&gt;</span>
27<a name="l00012"></a>00012 <span class="comment"> */</span>
28<a name="l00013"></a>00013
29<a name="l00014"></a>00014 <span class="comment">/*</span>
30<a name="l00015"></a>00015 <span class="comment"> * This is a short example of how to write uIP applications using</span>
31<a name="l00016"></a>00016 <span class="comment"> * protosockets.</span>
32<a name="l00017"></a>00017 <span class="comment"> */</span>
33<a name="l00018"></a>00018
34<a name="l00019"></a>00019 <span class="comment">/*</span>
35<a name="l00020"></a>00020 <span class="comment"> * We define the application state (struct hello_world_state) in the</span>
36<a name="l00021"></a>00021 <span class="comment"> * hello-world.h file, so we need to include it here. We also include</span>
37<a name="l00022"></a>00022 <span class="comment"> * uip.h (since this cannot be included in hello-world.h) and</span>
38<a name="l00023"></a>00023 <span class="comment"> * &lt;string.h&gt;, since we use the memcpy() function in the code.</span>
39<a name="l00024"></a>00024 <span class="comment"> */</span>
40<a name="l00025"></a>00025 <span class="preprocessor">#include "<a class="code" href="a00101.html">hello-world.h</a>"</span>
41<a name="l00026"></a>00026 <span class="preprocessor">#include "<a class="code" href="a00136.html">uip.h</a>"</span>
42<a name="l00027"></a>00027 <span class="preprocessor">#include &lt;string.h&gt;</span>
43<a name="l00028"></a>00028
44<a name="l00029"></a>00029 <span class="comment">/*</span>
45<a name="l00030"></a>00030 <span class="comment"> * Declaration of the protosocket function that handles the connection</span>
46<a name="l00031"></a>00031 <span class="comment"> * (defined at the end of the code).</span>
47<a name="l00032"></a>00032 <span class="comment"> */</span>
48<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);
49<a name="l00034"></a>00034 <span class="comment">/*---------------------------------------------------------------------------*/</span>
50<a name="l00035"></a>00035 <span class="comment">/*</span>
51<a name="l00036"></a>00036 <span class="comment"> * The initialization function. We must explicitly call this function</span>
52<a name="l00037"></a>00037 <span class="comment"> * from the system initialization code, some time after uip_init() is</span>
53<a name="l00038"></a>00038 <span class="comment"> * called.</span>
54<a name="l00039"></a>00039 <span class="comment"> */</span>
55<a name="l00040"></a>00040 <span class="keywordtype">void</span>
56<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>)
57<a name="l00042"></a>00042 {
58<a name="l00043"></a>00043   <span class="comment">/* We start to listen for connections on TCP port 1000. */</span>
59<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));
60<a name="l00045"></a>00045 }
61<a name="l00046"></a>00046 <span class="comment">/*---------------------------------------------------------------------------*/</span>
62<a name="l00047"></a>00047 <span class="comment">/*</span>
63<a name="l00048"></a>00048 <span class="comment"> * In hello-world.h we have defined the UIP_APPCALL macro to</span>
64<a name="l00049"></a>00049 <span class="comment"> * hello_world_appcall so that this funcion is uIP's application</span>
65<a name="l00050"></a>00050 <span class="comment"> * function. This function is called whenever an uIP event occurs</span>
66<a name="l00051"></a>00051 <span class="comment"> * (e.g. when a new connection is established, new data arrives, sent</span>
67<a name="l00052"></a>00052 <span class="comment"> * data is acknowledged, data needs to be retransmitted, etc.).</span>
68<a name="l00053"></a>00053 <span class="comment"> */</span>
69<a name="l00054"></a>00054 <span class="keywordtype">void</span>
70<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>)
71<a name="l00056"></a>00056 {
72<a name="l00057"></a>00057   <span class="comment">/*</span>
73<a name="l00058"></a>00058 <span class="comment">   * The uip_conn structure has a field called "appstate" that holds</span>
74<a name="l00059"></a>00059 <span class="comment">   * the application state of the connection. We make a pointer to</span>
75<a name="l00060"></a>00060 <span class="comment">   * this to access it easier.</span>
76<a name="l00061"></a>00061 <span class="comment">   */</span>
77<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>);
78<a name="l00063"></a>00063
79<a name="l00064"></a>00064   <span class="comment">/*</span>
80<a name="l00065"></a>00065 <span class="comment">   * If a new connection was just established, we should initialize</span>
81<a name="l00066"></a>00066 <span class="comment">   * the protosocket in our applications' state structure.</span>
82<a name="l00067"></a>00067 <span class="comment">   */</span>
83<a name="l00068"></a>00068   <span class="keywordflow">if</span>(<a name="a107"></a><a class="code" href="a00147.html#gdb971fb1525d0c5002f52125b05f3218">uip_connected</a>()) {
84<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>));
85<a name="l00070"></a>00070   }
86<a name="l00071"></a>00071
87<a name="l00072"></a>00072   <span class="comment">/*</span>
88<a name="l00073"></a>00073 <span class="comment">   * Finally, we run the protosocket function that actually handles</span>
89<a name="l00074"></a>00074 <span class="comment">   * the communication. We pass it a pointer to the application state</span>
90<a name="l00075"></a>00075 <span class="comment">   * of the current connection.</span>
91<a name="l00076"></a>00076 <span class="comment">   */</span>
92<a name="l00077"></a>00077   handle_connection(s);
93<a name="l00078"></a>00078 }
94<a name="l00079"></a>00079 <span class="comment">/*---------------------------------------------------------------------------*/</span>
95<a name="l00080"></a>00080 <span class="comment">/*</span>
96<a name="l00081"></a>00081 <span class="comment"> * This is the protosocket function that handles the communication. A</span>
97<a name="l00082"></a>00082 <span class="comment"> * protosocket function must always return an int, but must never</span>
98<a name="l00083"></a>00083 <span class="comment"> * explicitly return - all return statements are hidden in the PSOCK</span>
99<a name="l00084"></a>00084 <span class="comment"> * macros.</span>
100<a name="l00085"></a>00085 <span class="comment"> */</span>
101<a name="l00086"></a>00086 <span class="keyword">static</span> <span class="keywordtype">int</span>
102<a name="l00087"></a>00087 handle_connection(<span class="keyword">struct</span> <a class="code" href="a00078.html">hello_world_state</a> *s)
103<a name="l00088"></a>00088 {
104<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>);
105<a name="l00090"></a>00090
106<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>);
107<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>);
108<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>));
109<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>);
110<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>);
111<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>);
112<a name="l00097"></a>00097
113<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>);
114<a name="l00099"></a>00099 }
115<a name="l00100"></a>00100 <span class="comment">/*---------------------------------------------------------------------------*/</span>
116</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;
117<a href="http://www.doxygen.org/index.html">
118<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.6 </small></address>
119</body>
120</html>
121