PageRenderTime 63ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 1ms

/old_projects_reference_material/usb_eth/uip-1.0/doc/html/a00170.html

https://github.com/EFraim/trans-vpn
HTML | 125 lines | 124 code | 0 blank | 1 comment | 0 complexity | 4fee69854f32b7310c41498c31fbcb00 MD5 | raw file
  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: apps/hello-world/hello-world.c Source File</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 id="current"><a href="files.html"><span>Files</span></a></li>
  14. <li><a href="examples.html"><span>Examples</span></a></li>
  15. </ul></div>
  16. <div class="tabs">
  17. <ul>
  18. <li><a href="files.html"><span>File&nbsp;List</span></a></li>
  19. <li><a href="globals.html"><span>Globals</span></a></li>
  20. </ul></div>
  21. <h1>apps/hello-world/hello-world.c</h1><a href="a00100.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/**</span>
  22. <a name="l00002"></a>00002 <span class="comment"> * \addtogroup helloworld</span>
  23. <a name="l00003"></a>00003 <span class="comment"> * @{</span>
  24. <a name="l00004"></a>00004 <span class="comment"> */</span>
  25. <a name="l00005"></a>00005 <span class="comment"></span>
  26. <a name="l00006"></a>00006 <span class="comment">/**</span>
  27. <a name="l00007"></a>00007 <span class="comment"> * \file</span>
  28. <a name="l00008"></a>00008 <span class="comment"> * An example of how to write uIP applications</span>
  29. <a name="l00009"></a>00009 <span class="comment"> * with protosockets.</span>
  30. <a name="l00010"></a>00010 <span class="comment"> * \author</span>
  31. <a name="l00011"></a>00011 <span class="comment"> * Adam Dunkels &lt;adam@sics.se&gt;</span>
  32. <a name="l00012"></a>00012 <span class="comment"> */</span>
  33. <a name="l00013"></a>00013
  34. <a name="l00014"></a>00014 <span class="comment">/*</span>
  35. <a name="l00015"></a>00015 <span class="comment"> * This is a short example of how to write uIP applications using</span>
  36. <a name="l00016"></a>00016 <span class="comment"> * protosockets.</span>
  37. <a name="l00017"></a>00017 <span class="comment"> */</span>
  38. <a name="l00018"></a>00018
  39. <a name="l00019"></a>00019 <span class="comment">/*</span>
  40. <a name="l00020"></a>00020 <span class="comment"> * We define the application state (struct hello_world_state) in the</span>
  41. <a name="l00021"></a>00021 <span class="comment"> * hello-world.h file, so we need to include it here. We also include</span>
  42. <a name="l00022"></a>00022 <span class="comment"> * uip.h (since this cannot be included in hello-world.h) and</span>
  43. <a name="l00023"></a>00023 <span class="comment"> * &lt;string.h&gt;, since we use the memcpy() function in the code.</span>
  44. <a name="l00024"></a>00024 <span class="comment"> */</span>
  45. <a name="l00025"></a>00025 <span class="preprocessor">#include "<a class="code" href="a00101.html">hello-world.h</a>"</span>
  46. <a name="l00026"></a>00026 <span class="preprocessor">#include "<a class="code" href="a00136.html">uip.h</a>"</span>
  47. <a name="l00027"></a>00027 <span class="preprocessor">#include &lt;string.h&gt;</span>
  48. <a name="l00028"></a>00028
  49. <a name="l00029"></a>00029 <span class="comment">/*</span>
  50. <a name="l00030"></a>00030 <span class="comment"> * Declaration of the protosocket function that handles the connection</span>
  51. <a name="l00031"></a>00031 <span class="comment"> * (defined at the end of the code).</span>
  52. <a name="l00032"></a>00032 <span class="comment"> */</span>
  53. <a name="l00033"></a>00033 <span class="keyword">static</span> <span class="keywordtype">int</span> handle_connection(<span class="keyword">struct</span> <a class="code" href="a00078.html">hello_world_state</a> *s);
  54. <a name="l00034"></a>00034 <span class="comment">/*---------------------------------------------------------------------------*/</span>
  55. <a name="l00035"></a>00035 <span class="comment">/*</span>
  56. <a name="l00036"></a>00036 <span class="comment"> * The initialization function. We must explicitly call this function</span>
  57. <a name="l00037"></a>00037 <span class="comment"> * from the system initialization code, some time after uip_init() is</span>
  58. <a name="l00038"></a>00038 <span class="comment"> * called.</span>
  59. <a name="l00039"></a>00039 <span class="comment"> */</span>
  60. <a name="l00040"></a>00040 <span class="keywordtype">void</span>
  61. <a name="l00041"></a><a class="code" href="a00162.html#gb97849f0d3ea858eee790b69591e6427">00041</a> <a class="code" href="a00162.html#gb97849f0d3ea858eee790b69591e6427">hello_world_init</a>(<span class="keywordtype">void</span>)
  62. <a name="l00042"></a>00042 {
  63. <a name="l00043"></a>00043 <span class="comment">/* We start to listen for connections on TCP port 1000. */</span>
  64. <a name="l00044"></a>00044 <a class="code" href="a00147.html#gdd1ab3704ecd4900eec61a6897d32dc8">uip_listen</a>(<a class="code" href="a00148.html#g69a7a4951ff21b302267532c21ee78fc">HTONS</a>(1000));
  65. <a name="l00045"></a>00045 }
  66. <a name="l00046"></a>00046 <span class="comment">/*---------------------------------------------------------------------------*/</span>
  67. <a name="l00047"></a>00047 <span class="comment">/*</span>
  68. <a name="l00048"></a>00048 <span class="comment"> * In hello-world.h we have defined the UIP_APPCALL macro to</span>
  69. <a name="l00049"></a>00049 <span class="comment"> * hello_world_appcall so that this funcion is uIP's application</span>
  70. <a name="l00050"></a>00050 <span class="comment"> * function. This function is called whenever an uIP event occurs</span>
  71. <a name="l00051"></a>00051 <span class="comment"> * (e.g. when a new connection is established, new data arrives, sent</span>
  72. <a name="l00052"></a>00052 <span class="comment"> * data is acknowledged, data needs to be retransmitted, etc.).</span>
  73. <a name="l00053"></a>00053 <span class="comment"> */</span>
  74. <a name="l00054"></a>00054 <span class="keywordtype">void</span>
  75. <a name="l00055"></a><a class="code" href="a00162.html#g03070adbf8faab0f34f87c1270964306">00055</a> <a class="code" href="a00162.html#g03070adbf8faab0f34f87c1270964306">hello_world_appcall</a>(<span class="keywordtype">void</span>)
  76. <a name="l00056"></a>00056 {
  77. <a name="l00057"></a>00057 <span class="comment">/*</span>
  78. <a name="l00058"></a>00058 <span class="comment"> * The uip_conn structure has a field called "appstate" that holds</span>
  79. <a name="l00059"></a>00059 <span class="comment"> * the application state of the connection. We make a pointer to</span>
  80. <a name="l00060"></a>00060 <span class="comment"> * this to access it easier.</span>
  81. <a name="l00061"></a>00061 <span class="comment"> */</span>
  82. <a name="l00062"></a>00062 <span class="keyword">struct </span><a class="code" href="a00078.html">hello_world_state</a> *s = &amp;(<a class="code" href="a00088.html">uip_conn</a>-&gt;appstate);
  83. <a name="l00063"></a>00063
  84. <a name="l00064"></a>00064 <span class="comment">/*</span>
  85. <a name="l00065"></a>00065 <span class="comment"> * If a new connection was just established, we should initialize</span>
  86. <a name="l00066"></a>00066 <span class="comment"> * the protosocket in our applications' state structure.</span>
  87. <a name="l00067"></a>00067 <span class="comment"> */</span>
  88. <a name="l00068"></a>00068 <span class="keywordflow">if</span>(<a class="code" href="a00147.html#gdb971fb1525d0c5002f52125b05f3218">uip_connected</a>()) {
  89. <a name="l00069"></a>00069 <a class="code" href="a00158.html#g26ae707402e494f3895a9f012a93ea29">PSOCK_INIT</a>(&amp;s-&gt;<a class="code" href="a00078.html#a9825b5977b2d4dec66e6bd09e6ae6ea">p</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#4b1b1436b50ed7638aece35f41421196">inputbuffer</a>));
  90. <a name="l00070"></a>00070 }
  91. <a name="l00071"></a>00071
  92. <a name="l00072"></a>00072 <span class="comment">/*</span>
  93. <a name="l00073"></a>00073 <span class="comment"> * Finally, we run the protosocket function that actually handles</span>
  94. <a name="l00074"></a>00074 <span class="comment"> * the communication. We pass it a pointer to the application state</span>
  95. <a name="l00075"></a>00075 <span class="comment"> * of the current connection.</span>
  96. <a name="l00076"></a>00076 <span class="comment"> */</span>
  97. <a name="l00077"></a>00077 handle_connection(s);
  98. <a name="l00078"></a>00078 }
  99. <a name="l00079"></a>00079 <span class="comment">/*---------------------------------------------------------------------------*/</span>
  100. <a name="l00080"></a>00080 <span class="comment">/*</span>
  101. <a name="l00081"></a>00081 <span class="comment"> * This is the protosocket function that handles the communication. A</span>
  102. <a name="l00082"></a>00082 <span class="comment"> * protosocket function must always return an int, but must never</span>
  103. <a name="l00083"></a>00083 <span class="comment"> * explicitly return - all return statements are hidden in the PSOCK</span>
  104. <a name="l00084"></a>00084 <span class="comment"> * macros.</span>
  105. <a name="l00085"></a>00085 <span class="comment"> */</span>
  106. <a name="l00086"></a>00086 <span class="keyword">static</span> <span class="keywordtype">int</span>
  107. <a name="l00087"></a>00087 handle_connection(<span class="keyword">struct</span> <a class="code" href="a00078.html">hello_world_state</a> *s)
  108. <a name="l00088"></a>00088 {
  109. <a name="l00089"></a>00089 <a class="code" href="a00158.html#g84901a5aa60040e96d272a69977edd22">PSOCK_BEGIN</a>(&amp;s-&gt;<a class="code" href="a00078.html#a9825b5977b2d4dec66e6bd09e6ae6ea">p</a>);
  110. <a name="l00090"></a>00090
  111. <a name="l00091"></a>00091 <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>);
  112. <a name="l00092"></a>00092 <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>);
  113. <a name="l00093"></a>00093 strncpy(s-&gt;<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>));
  114. <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>);
  115. <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>);
  116. <a name="l00096"></a>00096 <a class="code" href="a00158.html#g5d56800f82bfc7bbf53bb4a659589812">PSOCK_CLOSE</a>(&amp;s-&gt;<a class="code" href="a00078.html#a9825b5977b2d4dec66e6bd09e6ae6ea">p</a>);
  117. <a name="l00097"></a>00097
  118. <a name="l00098"></a>00098 <a class="code" href="a00158.html#g4a264bb64ae706d53f572b1d9e4037a2">PSOCK_END</a>(&amp;s-&gt;<a class="code" href="a00078.html#a9825b5977b2d4dec66e6bd09e6ae6ea">p</a>);
  119. <a name="l00099"></a>00099 }
  120. <a name="l00100"></a>00100 <span class="comment">/*---------------------------------------------------------------------------*/</span>
  121. </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;
  122. <a href="http://www.doxygen.org/index.html">
  123. <img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.6 </small></address>
  124. </body>
  125. </html>