⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.html

📁 最新的免费嵌入式系统TCP/IP协议栈!
💻 HTML
📖 第 1 页 / 共 5 页
字号:
      <span class="keywordflow">case</span> WELCOME_ACKED:         <a class="code" href="a00147.html#g04b053a623aac7cd4195157d470661b3">uip_send</a>(<span class="stringliteral">"ok\n"</span>, 3);         <span class="keywordflow">break</span>;      }   }}</pre></div><p>The configuration for the application:<p><div class="fragment"><pre class="fragment"><span class="preprocessor">#define UIP_APPCALL       example2_app</span><span class="preprocessor">#define UIP_APPSTATE_SIZE sizeof(struct example2_state)</span></pre></div><h3><a class="anchor" name="example3">Differentiating Between Applications</a></h3>If the system should run multiple applications, one technique to differentiate between them is to use the TCP port number of either the remote end or the local end of the connection. The example below shows how the two examples above can be combined into one application.<p><div class="fragment"><pre class="fragment"><span class="keywordtype">void</span> example3_init(<span class="keywordtype">void</span>) {   example1_init();   example2_init();   }<span class="keywordtype">void</span> example3_app(<span class="keywordtype">void</span>) {   <span class="keywordflow">switch</span>(<a class="code" href="a00088.html">uip_conn</a>-&gt;lport) {   <span class="keywordflow">case</span> <a class="code" href="a00148.html#g69a7a4951ff21b302267532c21ee78fc">HTONS</a>(1234):      example1_app();      <span class="keywordflow">break</span>;   <span class="keywordflow">case</span> <a class="code" href="a00148.html#g69a7a4951ff21b302267532c21ee78fc">HTONS</a>(2345):      example2_app();      <span class="keywordflow">break</span>;   }}</pre></div><h3><a class="anchor" name="example4">Utilizing TCP Flow Control</a></h3>This example shows a simple application that connects to a host, sends an HTTP request for a file and downloads it to a slow device such a disk drive. This shows how to use the flow control functions of uIP.<p><div class="fragment"><pre class="fragment"><span class="keywordtype">void</span> example4_init(<span class="keywordtype">void</span>) {   <a class="code" href="a00153.html#g77570ac4fcab86864fa1916e55676da2">u16_t</a> ipaddr[2];   <a class="code" href="a00148.html#g87f0b54ade0d159fba495089128a4932">uip_ipaddr</a>(ipaddr, 192,168,0,1);   <a class="code" href="a00147.html#g8096b0c4b543dc408f4dd031ddae7240">uip_connect</a>(ipaddr, <a class="code" href="a00148.html#g69a7a4951ff21b302267532c21ee78fc">HTONS</a>(80));}<span class="keywordtype">void</span> example4_app(<span class="keywordtype">void</span>) {   <span class="keywordflow">if</span>(<a class="code" href="a00147.html#gdb971fb1525d0c5002f52125b05f3218">uip_connected</a>() || <a class="code" href="a00147.html#ga8933ad15a2e2947dae4a5cff50e6007">uip_rexmit</a>()) {      <a class="code" href="a00147.html#g04b053a623aac7cd4195157d470661b3">uip_send</a>(<span class="stringliteral">"GET /file HTTP/1.0\r\nServer:192.186.0.1\r\n\r\n"</span>,               48);      <span class="keywordflow">return</span>;   }   <span class="keywordflow">if</span>(<a class="code" href="a00147.html#g26a14b8dae3f861830af9e7cf1e03725">uip_newdata</a>()) {      device_enqueue(<a class="code" href="a00150.html#g561b8eda32e059d4e7397f776268cc63">uip_appdata</a>, <a class="code" href="a00147.html#g1a1bc437c09ddef238abab41d77c3177">uip_datalen</a>());      <span class="keywordflow">if</span>(device_queue_full()) {         <a class="code" href="a00147.html#g0a8bb9d6d0f1f56852ccfccbbad6c5d8">uip_stop</a>();      }   }   <span class="keywordflow">if</span>(<a class="code" href="a00147.html#g58bb90796c1cdad3aac2ecf44d87b20e">uip_poll</a>() &amp;&amp; <a class="code" href="a00147.html#g64a238a5c02640a7a4aef004163aeb47">uip_stopped</a>()) {      <span class="keywordflow">if</span>(!device_queue_full()) {         <a class="code" href="a00147.html#g81ac47cee1c18f6aa479044069db7ca3">uip_restart</a>();      }   }}</pre></div><p>When the connection has been established, an HTTP request is sent to the server. Since this is the only data that is sent, the application knows that if it needs to retransmit any data, it is that request that should be retransmitted. It is therefore possible to combine these two events as is done in the example.<p>When the application receives new data from the remote host, it sends this data to the device by using the function device_enqueue(). It is important to note that this example assumes that this function copies the data into its own buffers. The data in the uip_appdata buffer will be overwritten by the next incoming packet.<p>If the device's queue is full, the application stops the data from the remote host by calling the uIP function <a class="el" href="a00147.html#g0a8bb9d6d0f1f56852ccfccbbad6c5d8">uip_stop()</a>. The application can then be sure that it will not receive any new data until <a class="el" href="a00147.html#g81ac47cee1c18f6aa479044069db7ca3">uip_restart()</a> is called. The application polling event is used to check if the device's queue is no longer full and if so, the data flow is restarted with <a class="el" href="a00147.html#g81ac47cee1c18f6aa479044069db7ca3">uip_restart()</a>.<h3><a class="anchor" name="example5">A Simple Web Server</a></h3>This example shows a very simple file server application that listens to two ports and uses the port number to determine which file to send. If the files are properly formatted, this simple application can be used as a web server with static pages. The implementation follows.<p><div class="fragment"><pre class="fragment"><span class="keyword">struct </span>example5_state {   <span class="keywordtype">char</span> *dataptr;   <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> dataleft;};<span class="keywordtype">void</span> example5_init(<span class="keywordtype">void</span>) {   <a class="code" href="a00147.html#gdd1ab3704ecd4900eec61a6897d32dc8">uip_listen</a>(<a class="code" href="a00148.html#g69a7a4951ff21b302267532c21ee78fc">HTONS</a>(80));   <a class="code" href="a00147.html#gdd1ab3704ecd4900eec61a6897d32dc8">uip_listen</a>(<a class="code" href="a00148.html#g69a7a4951ff21b302267532c21ee78fc">HTONS</a>(81));}<span class="keywordtype">void</span> example5_app(<span class="keywordtype">void</span>) {   <span class="keyword">struct </span>example5_state *s;   s = (<span class="keyword">struct </span>example5_state)<a class="code" href="a00150.html#g788ffac72342f6172343d7f8099cbe1a">uip_conn</a>-&gt;appstate;      <span class="keywordflow">if</span>(<a class="code" href="a00147.html#gdb971fb1525d0c5002f52125b05f3218">uip_connected</a>()) {      <span class="keywordflow">switch</span>(<a class="code" href="a00150.html#g788ffac72342f6172343d7f8099cbe1a">uip_conn</a>-&gt;<a class="code" href="a00088.html#0cd09beee671e7e9efb0b4aced10249e">lport</a>) {      <span class="keywordflow">case</span> <a class="code" href="a00148.html#g69a7a4951ff21b302267532c21ee78fc">HTONS</a>(80):         s-&gt;dataptr = data_port_80;         s-&gt;dataleft = datalen_port_80;         <span class="keywordflow">break</span>;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -