版权声明:本博客文章,大多是本人整理编写,或在网络中收集,转载请注明出处! https://blog.csdn.net/tennysonsky/article/details/45047133 </div>
<link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-cd6c485e8b.css">
<link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-cd6c485e8b.css">
<div class="htmledit_views" id="content_views">
<h1><a name="t0"></a><span style="font-family:SimSun;font-size:18px;">概述</span></h1><p><span style="font-family:SimSun;font-size:18px;">UDP 是 User Datagram Protocol 的简称, 中文名是用户数据报协议,是一个简单的面向数据报的运输层协议,在网络中用于处理数据包,是一种<a href="http://blog.csdn.net/tennysonsky/article/details/44455565" rel="nofollow">无连接的协议</a>。UDP 不提供可靠性的传输,它只是把应用程序传给 IP 层的数据报发送出去,但是并不能保证它们能到达目的地。由于 UDP 在传输数据报前不用在客户和服务器之间建立一个连接,且没有超时重发等机制,故而传输速度很快。</span></p><p><span style="font-family:SimSun;font-size:18px;"><br></span></p><p><strong><span style="font-family:SimSun;font-size:18px;">UDP 有如下的特点:</span></strong></p><blockquote style="border:none;"><p><span style="font-family:SimSun;font-size:18px;">1)邮件系统服务模式的抽象(可通过邮件模型来进行对比)</span></p><p><span style="font-family:SimSun;font-size:18px;">2)每个分组都携带完整的目的地址</span></p><p><span style="font-family:SimSun;font-size:18px;">3)发送数据之前<a href="http://blog.csdn.net/tennysonsky/article/details/44455565" rel="nofollow">不需要建立链接</a></span></p><p><span style="font-family:SimSun;font-size:18px;">4)不对数据包的顺序进行检查,不能保证分组的先后顺序</span></p><p><span style="font-family:SimSun;font-size:18px;">5)不进行分组出错的恢复和重传</span></p><p><span style="font-family:SimSun;font-size:18px;">6)不保证数据传输的可靠性</span></p><p><span style="font-size:24px;"><span style="font-family:'KaiTi_GB2312';"><br></span></span></p></blockquote><div style="text-align:center;"><span style="font-family:'KaiTi_GB2312';"><img src="https://img-blog.csdn.net/20150415201806514" alt="" style="font-size:24px;"></span></div><p><span style="font-size:24px;"><span style="font-family:'KaiTi_GB2312';"><br></span></span></p><p><span style="font-family:SimSun;font-size:18px;">在网络质量令人十分不满意的环境下,UDP 协议数据包丢失会比较严重。但是由于 UDP 的特性:<a href="http://blog.csdn.net/tennysonsky/article/details/44455565" rel="nofollow">它不属于连接型协议</a>,因而具有资源消耗小,处理速度快的优点,所以通常音频、视频和普通数据在传送时使用 UDP 较多,因为它们即使偶尔丢失一两个数据包,也不会对接收结果产生太大影响。比如我们聊天用的 ICQ 和 QQ 就是使用的 UDP 协议。</span><br></p><p><span style="font-size:24px;"><span style="font-family:'KaiTi_GB2312';"><br></span></span></p><h1><a name="t1"></a><strong><span style="font-family:SimSun;font-size:18px;">UDP 编程的 C/S 架构</span></strong></h1><div style="text-align:center;"><span style="font-size:24px;"><strong><span style="font-family:'KaiTi_GB2312';"><img src="https://img-blog.csdn.net/20150509101207345" alt=""><br></span></strong></span></div><p><span style="font-size:24px;"><span style="font-family:'KaiTi_GB2312';"><br></span></span></p><h1><a name="t2"></a><span style="font-family:SimSun;font-size:18px;">UDP 客户端程序</span></h1><div><span style="font-family:SimSun;font-size:18px;">对比于写信模型,客户端相当于寄信人,要想成功给人寄信,信封上必须写上对方的地址。</span></div><p><span style="color:rgb(255,0,0);"><span style="font-family:SimSun;font-size:18px;">ssize_t sendto( int sockfd,<br></span></span></p><blockquote style="border:none;"><blockquote style="border:none;"><blockquote style="border:none;"><blockquote style="border:none;"><p><span style="color:rgb(255,0,0);"><span style="font-family:SimSun;font-size:18px;">const void *buf,</span></span></p></blockquote></blockquote></blockquote><blockquote style="border:none;"><blockquote style="border:none;"><blockquote style="border:none;"><p><span style="color:rgb(255,0,0);"><span style="font-family:SimSun;font-size:18px;">size_t nbytes,</span></span></p></blockquote></blockquote></blockquote><blockquote style="border:none;"><blockquote style="border:none;"><blockquote style="border:none;"><p><span style="color:rgb(255,0,0);"><span style="font-family:SimSun;font-size:18px;">int flags,</span></span></p></blockquote></blockquote></blockquote><blockquote style="border:none;"><blockquote style="border:none;"><blockquote style="border:none;"><p><span style="color:rgb(255,0,0);"><span style="font-family:SimSun;font-size:18px;">const struct sockaddr *to, </span></span></p></blockquote></blockquote></blockquote><blockquote style="border:none;"><blockquote style="border:none;"><blockquote style="border:none;"><p><span style="color:rgb(255,0,0);"><span style="font-family:SimSun;font-size:18px;">socklen_t addrlen );</span></span></p></blockquote></blockquote></blockquote></blockquote><p><span style="font-family:SimSun;font-size:18px;"><span>功能</span>:</span></p><blockquote style="border:none;"><p><span style="font-family:SimSun;font-size:18px;">向 to 结构体指针中指定的 ip,发送 UDP 数据,可以发送 0 长度的 UDP 数据包</span></p></blockquote><p><span style="font-family:SimSun;font-size:18px;"><span>参数</span>:</span></p><blockquote style="border:none;"><p><span style="font-family:SimSun;font-size:18px;"><span style="color:#ff0000;">sockfd</span>:<a href="http://blog.csdn.net/tennysonsky/article/details/45047209" rel="nofollow">套接字</a></span></p><p><span style="font-family:SimSun;font-size:18px;"><span style="color:#ff0000;">buf</span>:发送数据缓冲区</span></p><p><span style="font-family:SimSun;font-size:18px;"><span style="color:#ff0000;">nbytes</span>:发送数据缓冲区的大小</span></p><p><span style="font-family:SimSun;font-size:18px;"><span style="color:#ff0000;">flags</span>:一般为 0</span></p><p><span style="font-family:SimSun;font-size:18px;"><span style="color:#ff0000;">to</span>:指向目的主机地址结构体的指针</span></p><p><span style="font-family:SimSun;font-size:18px;"><span style="color:#ff0000;">addrlen</span>:to 所指向内容的长度</span></p></blockquote><p><span style="font-family:SimSun;font-size:18px;"><span>返回值</span>:</span></p><blockquote style="border:none;"><p><span style="font-family:SimSun;font-size:18px;">成功:发送数据的长度</span></p><p><span style="font-family:SimSun;font-size:18px;">失败: -1</span></p></blockquote><p><span style="font-size:24px;"><span style="font-family:'KaiTi_GB2312';"><br></span></span></p><p><span style="font-family:SimSun;font-size:18px;">这里通过 Windows 的网络调试助手和虚拟机中的 ubuntu 客户端程序进行通信,<a href="http://download.csdn.net/detail/tennysonsky/8599411" rel="nofollow">网络调试助手下载请点此处</a>。</span></p><p><span style="font-family:SimSun;font-size:18px;"><br></span></p><p><span style="font-family:SimSun;font-size:18px;">Windows 的网络调试助手作为服务器,接收客户端的请求,调试助手配置如下:</span><br></p><p style="text-align:center;"><span style="font-size:24px;"><span style="font-family:'KaiTi_GB2312';"><img src="https://img-blog.csdn.net/20150416110802073" alt=""><br></span></span></p><p><span style="font-size:24px;"><span style="font-family:'KaiTi_GB2312';"><br></span></span></p><p><span style="font-family:SimSun;font-size:18px;"><strong><span style="color:rgb(255,0,0);">对于 UDP</span><span style="color:rgb(255,0,0);line-height:25.98958396911621px;">客户端编程流程,</span><span style="color:rgb(255,0,0);"> 有点类似于</span><span>写信</span><span style="color:rgb(255,0,0);">过程</span></strong><span style="color:rgb(255,0,0);">:</span>找个邮政工作人员( <span style="line-height:25.98958396911621px;"><span style="color:#ff0000;">socket()</span> </span>)<strong><span style="color:#ff0000;">-></span></strong>信封上写上地址同时里面装上信件内容并且投递(<span style="color:rgb(255,0,0);">sendto()</span> )<span style="color:rgb(255,0,0);"><strong>-></strong></span> ……还可以继续写信,或者,接收对方的回信(<span style="color:rgb(255,0,0);">recvfrom()</span> )……<span style="color:rgb(255,0,0);"><strong>-></strong></span> 打完收工(<span style="color:rgb(255,0,0);">close()</span> )。</span></p><p><span style="font-family:SimSun;font-size:18px;"><br></span></p><p><span style="font-family:SimSun;font-size:18px;">虚拟机中 ubuntu 的 UDP 客户端程序:</span></p><p><span style="font-size:24px;"><span style="font-size:24px;"><span style="font-family:'KaiTi_GB2312';"></span></span></span></p><pre><code class="language-cpp hljs"><ol class="hljs-ln" style="1094px"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><stdio.h></span></span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><stdlib.h></span></span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><string.h></span></span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><unistd.h></span></span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><sys/socket.h></span></span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><netinet/in.h></span></span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="7"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><arpa/inet.h></span></span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="8"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> </div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="9"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">int</span> argc, <span class="hljs-keyword">char</span> *argv[])</span></span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="10"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">{</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="11"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">short</span> port = <span class="hljs-number">8080</span>; <span class="hljs-comment">//服务器端口</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="12"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-keyword">char</span> *server_ip = <span class="hljs-string">"10.221.20.10"</span>; <span class="hljs-comment">//服务器ip地址</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="13"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> </div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="14"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-keyword">if</span>( argc > <span class="hljs-number">1</span> ) <span class="hljs-comment">// main函数传参,服务器ip地址</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="15"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> { </div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="16"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> server_ip = argv[<span class="hljs-number">1</span>];</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="17"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> }</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="18"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> </div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="19"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-keyword">if</span>( argc > <span class="hljs-number">2</span> ) <span class="hljs-comment">// main函数传参,服务器端口</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="20"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> {</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="21"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> port = atoi(argv[<span class="hljs-number">2</span>]);</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="22"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> }</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="23"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> </div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="24"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-keyword">int</span> sockfd;</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="25"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> sockfd = socket(AF_INET, SOCK_DGRAM, <span class="hljs-number">0</span>); <span class="hljs-comment">//创建UDP套接字</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="26"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-keyword">if</span>(sockfd < <span class="hljs-number">0</span>)</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="27"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> {</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="28"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> perror(<span class="hljs-string">"socket"</span>);</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="29"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-built_in">exit</span>(<span class="hljs-number">-1</span>);</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="30"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> }</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="31"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> </div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="32"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-comment">// 套接字地址</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="33"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">sockaddr_in</span> <span class="hljs-title">dest_addr</span>;</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="34"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> bzero(&dest_addr, <span class="hljs-keyword">sizeof</span>(dest_addr)); <span class="hljs-comment">// 清空内容</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="35"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> dest_addr.sin_family = AF_INET; <span class="hljs-comment">// ipv4</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="36"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> dest_addr.sin_port = htons(port); <span class="hljs-comment">// 端口转换</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="37"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> inet_pton(AF_INET, server_ip, &dest_addr.sin_addr); <span class="hljs-comment">// ip地址转换</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="38"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> </div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="39"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-built_in">printf</span>(<span class="hljs-string">"send data to UDP server %s:%d!
"</span>, server_ip, port);</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="40"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> </div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="41"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-keyword">while</span>(<span class="hljs-number">1</span>)</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="42"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> {</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="43"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-keyword">char</span> send_buf[<span class="hljs-number">512</span>] = <span class="hljs-string">""</span>;</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="44"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> fgets(send_buf, <span class="hljs-keyword">sizeof</span>(send_buf), <span class="hljs-built_in">stdin</span>);<span class="hljs-comment">//获取输入</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="45"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> send_buf[<span class="hljs-built_in">strlen</span>(send_buf)<span class="hljs-number">-1</span>] = <span class="hljs-string">' '</span>;</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="46"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-comment">//发送数据</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="47"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-keyword">int</span> len = sendto(sockfd, send_buf, <span class="hljs-built_in">strlen</span>(send_buf), <span class="hljs-number">0</span>, (struct sockaddr*)&dest_addr, <span class="hljs-keyword">sizeof</span>(dest_addr));</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="48"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-built_in">printf</span>(<span class="hljs-string">"len = %d
"</span>, len);</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="49"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> }</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="50"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> </div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="51"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> close(sockfd);</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="52"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="53"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">}</div></div></li></ol></code><div class="hljs-button {2}" data-title="复制" onclick="hljs.copyCode(event)"></div></pre><br><p></p><span style="font-family:SimSun;font-size:18px;">运行结果如下:</span><p><span style="font-size:24px;"><span style="font-size:24px;"><img src="https://img-blog.csdn.net/20150416122007901" alt=""><br></span></span></p><p><span style="font-family:SimSun;font-size:18px;">UDP 客户端<strong><span style="color:#ff0000;">注意点</span></strong>:<br></span></p><blockquote style="border:none;"><p><span style="font-family:SimSun;font-size:18px;">1)本地IP、本地端口(我是谁)</span></p><p><span style="font-family:SimSun;font-size:18px;">2)目的IP、目的端口(发给谁)</span></p><p><span style="font-family:SimSun;font-size:18px;">3)在客户端的代码中,我们只设置了目的IP、目的端口</span></p><p><span style="font-family:SimSun;font-size:18px;">4)客户端的本地 ip、本地 port 是我们调用 sendto 的时候 linux 系统底层自动给客户端分配的;分配端口的方式为随机分配,即每次运行系统给的 port 不一样。</span></p><p><span style="font-size:24px;"><span style="font-family:'KaiTi_GB2312';"><br></span></span></p></blockquote><h1><a name="t3"></a><span style="font-family:SimSun;font-size:18px;">UDP 服务器程序</span></h1><p><span style="font-family:SimSun;font-size:18px;">UDP网络程序想要收取数据需什么条件?<br></span></p><blockquote style="border:none;"><p><span style="font-family:SimSun;font-size:18px;">1)确定的 ip 地址</span></p><p><span style="font-family:SimSun;font-size:18px;">2)确定的端口(port)</span></p></blockquote><p><span style="font-family:SimSun;font-size:18px;">这正如,我要收到别人寄过来的信,我必须告诉别人我的地址(ip),同时告诉别人我我的公寓信箱号(端口)。</span></p><p><span style="font-family:SimSun;font-size:18px;"><br></span></p><p><span style="font-family:SimSun;font-size:18px;"><span>接收端</span>使用 bind() 函数,来完成地址结构与 socket 套接字的绑定,这样 ip、port 就固定了,<span>发送端</span>在 sendto 函数中指定接收端的 ip、port,就可以发送数据了。</span></p><p><span style="font-family:SimSun;font-size:18px;"><br></span></p><p><span style="font-family:SimSun;font-size:18px;"><span>需要头文件</span>:#include <sys/socket.h></span></p><p><span style="color:#ff0000;"><span style="font-family:SimSun;font-size:18px;">int bind( int sockfd,</span></span></p><blockquote style="border:none;"><blockquote style="border:none;"><blockquote style="border:none;"><p><span style="color:#ff0000;"><span style="font-family:SimSun;font-size:18px;">const struct sockaddr *myaddr,</span></span></p></blockquote></blockquote><blockquote style="border:none;"><blockquote style="border:none;"><p><span style="color:#ff0000;"><span style="font-family:SimSun;font-size:18px;">socklen_t addrlen );</span></span></p></blockquote></blockquote></blockquote><p><span style="font-family:SimSun;font-size:18px;"><span>功能</span>:</span></p><blockquote style="border:none;"><p><span style="font-family:SimSun;font-size:18px;">将本地协议地址与 sockfd 绑定,这样 ip、port 就固定了</span></p></blockquote><p><span style="font-family:SimSun;font-size:18px;"><span>参数</span>:</span></p><blockquote style="border:none;"><p><span style="font-family:SimSun;font-size:18px;"><span style="color:#ff0000;">sockfd</span>:<a href="http://blog.csdn.net/tennysonsky/article/details/45047209" rel="nofollow">socket 套接字</a></span></p><p><span style="font-family:SimSun;font-size:18px;"><span style="color:#ff0000;">myaddr</span>: 指向特定协议的地址结构指针</span></p><p><span style="font-family:SimSun;font-size:18px;"><span style="color:#ff0000;">addrlen</span>:该地址结构的长度</span></p></blockquote><p><span style="font-family:SimSun;font-size:18px;"><span>返回值</span>:</span></p><blockquote style="border:none;"><p><span style="font-family:SimSun;font-size:18px;">成功:返回 0</span></p><p><span style="font-family:SimSun;font-size:18px;">失败:-1</span></p></blockquote><p><span style="font-family:SimSun;font-size:18px;"><br></span></p><p><span style="font-family:SimSun;font-size:18px;">使用实例如下:</span></p><p><span style="font-size:24px;"><span style="font-family:'KaiTi_GB2312';"></span></span></p><pre><code class="language-cpp hljs"><ol class="hljs-ln" style="826px"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-comment">// 本地网络地址</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">sockaddr_in</span> <span class="hljs-title">my_addr</span>;</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">bzero(&my_addr, <span class="hljs-keyword">sizeof</span>(my_addr)); <span class="hljs-comment">// 清空结构体内容</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">my_addr.sin_family = AF_INET; <span class="hljs-comment">// ipv4</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">my_addr.sin_port = htons(port); <span class="hljs-comment">// 端口转换</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">my_addr.sin_addr.s_addr = htonl(INADDR_ANY); <span class="hljs-comment">// 绑定网卡所有ip地址,INADDR_ANY为通配地址,值为0</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="7"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> </div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="8"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-built_in">printf</span>(<span class="hljs-string">"Binding server to port %d
"</span>, port);</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="9"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-keyword">int</span> err_log;</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="10"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">err_log = bind(sockfd, (struct sockaddr*)&my_addr, <span class="hljs-keyword">sizeof</span>(my_addr)); <span class="hljs-comment">// 绑定</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="11"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-keyword">if</span>(err_log != <span class="hljs-number">0</span>)</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="12"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">{</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="13"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> perror(<span class="hljs-string">"bind"</span>);</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="14"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> close(sockfd); </div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="15"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-built_in">exit</span>(<span class="hljs-number">-1</span>);</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="16"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">}</div></div></li></ol></code><div class="hljs-button {2}" data-title="复制" onclick="hljs.copyCode(event)"></div></pre><p><br></p><p><span style="font-family:SimSun;font-size:18px;">绑定端口有些需要注意的问题,<a href="http://blog.csdn.net/tennysonsky/article/details/44057585" rel="nofollow">请看《绑定( bind )端口需要注意的问题》</a>。</span></p><p><span style="font-family:SimSun;font-size:18px;"><br></span></p><p><span style="color:#ff0000;"><span style="font-family:SimSun;font-size:18px;">ssize_t recvfrom( int sockfd, </span></span></p><blockquote style="border:none;"><blockquote style="border:none;"><blockquote style="border:none;"><blockquote style="border:none;"><p><span style="color:#ff0000;"><span style="font-family:SimSun;font-size:18px;">void *buf,<span></span> </span></span></p><p><span style="font-family:SimSun;font-size:18px;color:rgb(255,0,0);">size_t nbytes,</span></p><p><span style="font-family:SimSun;font-size:18px;color:rgb(255,0,0);">int flags,</span></p><p><span style="font-family:SimSun;font-size:18px;color:rgb(255,0,0);">struct sockaddr *from, </span></p><p><span style="font-family:SimSun;font-size:18px;color:rgb(255,0,0);">socklen_t *addrlen );</span></p></blockquote></blockquote></blockquote></blockquote><p><span style="font-family:SimSun;font-size:18px;"><span>功能</span>:</span></p><blockquote style="border:none;"><p><span style="font-family:SimSun;font-size:18px;">接收 UDP 数据,并将源地址信息保存在 from 指向的结构中,默认的情况下,如果没有接收到数据,这个函数会阻塞,直到有数据到来。</span></p></blockquote><p><span style="font-family:SimSun;font-size:18px;"><span>参数</span>:</span></p><blockquote style="border:none;"><p><span style="font-family:SimSun;font-size:18px;"><span style="color:#ff0000;">sockfd</span>:<span></span>套接字</span></p><p><span style="font-family:SimSun;font-size:18px;"><span style="color:#ff0000;">buf</span>:<span></span>接收数据缓冲区</span></p><p><span style="font-family:SimSun;font-size:18px;"><span style="color:#ff0000;">nbytes</span>:<span></span>接收数据缓冲区的大小</span></p><p><span style="font-family:SimSun;font-size:18px;"><span style="color:#ff0000;">flags</span>:<span></span>套接字标志<span>(常为 0)</span></span></p><p><span style="font-family:SimSun;font-size:18px;"><span style="color:#ff0000;">from</span>:<span></span>源地址结构体指针,用来保存数据的来源</span></p><p><span style="font-family:SimSun;font-size:18px;"><span style="color:#ff0000;">addrlen</span>:from 所指内容的长度</span></p></blockquote><p><span style="font-family:SimSun;font-size:18px;"><span>返回值</span>:</span></p><blockquote style="border:none;"><p><span style="font-family:SimSun;font-size:18px;">成功:接收到的长度</span></p><p><span style="font-family:SimSun;font-size:18px;">失败: -1</span></p><p><span style="font-family:SimSun;font-size:18px;"><br></span></p></blockquote><p><span style="font-family:SimSun;font-size:18px;"><strong><span style="color:#ff0000;">对于 UDP 服务器编程流程, 有点类似于</span><span>收信</span><span style="color:#ff0000;">过程</span></strong>:找个邮政工作人员( <span style="line-height:25.98958396911621px;"><span style="color:#ff0000;">socket()</span> </span>) <span style="color:#ff0000;">-> </span>确定信箱的位置:地址+信箱号(<span style="color:#ff0000;">bind</span><span style="color:#ff0000;">()</span> )<span style="color:rgb(255,0,0);"><strong>-></strong></span> 等待对方的来信( <span style="color:rgb(255,0,0);">recvfrom()</span> )<span style="color:rgb(255,0,0);"><strong>-></strong></span> ……还可以回信(<span style="color:#ff0000;">write()</span> ),或者,继续等待对方的来信……</span></p><p><span style="font-family:SimSun;font-size:18px;"><br></span></p><p><span style="font-family:SimSun;font-size:18px;">ubuntu 中的服务器程序如下:</span></p><p><span style="font-size:24px;"><span style="font-size:24px;"><span style="font-family:'KaiTi_GB2312';"></span></span></span></p><pre><code class="language-cpp hljs"><ol class="hljs-ln" style="1094px"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><stdio.h></span></span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><stdlib.h></span></span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><string.h></span></span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><unistd.h></span></span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><sys/socket.h></span></span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><netinet/in.h></span></span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="7"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><arpa/inet.h></span></span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="8"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">int</span> argc, <span class="hljs-keyword">char</span> *argv[])</span></span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="9"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">{</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="10"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">short</span> port = <span class="hljs-number">8000</span>; <span class="hljs-comment">// 本地端口</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="11"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-keyword">if</span>(argc > <span class="hljs-number">1</span>)</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="12"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> {</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="13"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> port = atoi(argv[<span class="hljs-number">1</span>]);</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="14"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> }</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="15"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> </div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="16"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-keyword">int</span> sockfd;</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="17"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> sockfd = socket(AF_INET, SOCK_DGRAM, <span class="hljs-number">0</span>); <span class="hljs-comment">// 创建套接字</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="18"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-keyword">if</span>(sockfd < <span class="hljs-number">0</span>)</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="19"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> {</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="20"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> perror(<span class="hljs-string">"socket"</span>);</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="21"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-built_in">exit</span>(<span class="hljs-number">-1</span>);</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="22"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> }</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="23"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> </div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="24"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-comment">// 本地网络地址</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="25"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">sockaddr_in</span> <span class="hljs-title">my_addr</span>;</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="26"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> bzero(&my_addr, <span class="hljs-keyword">sizeof</span>(my_addr)); <span class="hljs-comment">// 清空结构体内容</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="27"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> my_addr.sin_family = AF_INET; <span class="hljs-comment">// ipv4</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="28"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> my_addr.sin_port = htons(port); <span class="hljs-comment">// 端口转换</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="29"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> my_addr.sin_addr.s_addr = htonl(INADDR_ANY); <span class="hljs-comment">// 绑定网卡所有ip地址,INADDR_ANY为通配地址,值为0</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="30"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> </div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="31"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Binding server to port %d
"</span>, port);</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="32"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-keyword">int</span> err_log;</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="33"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> err_log = bind(sockfd, (struct sockaddr*)&my_addr, <span class="hljs-keyword">sizeof</span>(my_addr)); <span class="hljs-comment">// 绑定</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="34"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-keyword">if</span>(err_log != <span class="hljs-number">0</span>)</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="35"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> {</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="36"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> perror(<span class="hljs-string">"bind"</span>);</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="37"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> close(sockfd); </div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="38"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-built_in">exit</span>(<span class="hljs-number">-1</span>);</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="39"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> }</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="40"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> </div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="41"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-built_in">printf</span>(<span class="hljs-string">"receive data...
"</span>);</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="42"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-keyword">while</span>(<span class="hljs-number">1</span>)</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="43"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> {</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="44"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-keyword">int</span> recv_len;</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="45"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-keyword">char</span> recv_buf[<span class="hljs-number">512</span>] = <span class="hljs-string">""</span>;</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="46"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">sockaddr_in</span> <span class="hljs-title">client_addr</span>;</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="47"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-keyword">char</span> cli_ip[INET_ADDRSTRLEN] = <span class="hljs-string">""</span>;<span class="hljs-comment">//INET_ADDRSTRLEN=16</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="48"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-keyword">socklen_t</span> cliaddr_len = <span class="hljs-keyword">sizeof</span>(client_addr); </div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="49"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> </div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="50"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-comment">// 接受数据</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="51"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> recv_len = recvfrom(sockfd, recv_buf, <span class="hljs-keyword">sizeof</span>(recv_buf), <span class="hljs-number">0</span>, (struct sockaddr*)&client_addr, &cliaddr_len);</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="52"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> inet_ntop(AF_INET, &client_addr.sin_addr, cli_ip, INET_ADDRSTRLEN);</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="53"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-built_in">printf</span>(<span class="hljs-string">"
ip:%s ,port:%d
"</span>,cli_ip, ntohs(client_addr.sin_port));</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="54"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-built_in">printf</span>(<span class="hljs-string">"data(%d):%s
"</span>,recv_len,recv_buf);</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="55"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> }</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="56"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> </div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="57"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> close(sockfd);</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="58"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="59"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">}</div></div></li></ol></code><div class="hljs-button {2}" data-title="复制" onclick="hljs.copyCode(event)"></div></pre><p><br></p><p><span style="font-family:SimSun;font-size:18px;">Windows 的网络调试助手作为客户端,给 ubuntu 中的服务器发送数据,调试助手配置如下:<br></span></p><p style="text-align:center;"><span style="font-size:24px;"><img src="https://img-blog.csdn.net/20150416201243415" alt=""></span></p><p style="text-align:center;"><span style="font-size:24px;"><br></span></p><p><span style="font-family:SimSun;font-size:18px;">运行结果如下:</span></p><p><span style="font-size:24px;"><img src="https://img-blog.csdn.net/20150416201900605" alt=""><br></span></p><p><span style="font-size:24px;"><br></span></p><p><strong><span style="font-family:SimSun;font-size:18px;"><a href="http://download.csdn.net/detail/tennysonsky/8601915" rel="nofollow">示例代码下载请点此处。</a></span></strong></p><p><strong><img src="https://img-blog.csdn.net/20180210174507305" alt=""><br></strong></p><p><strong><br></strong></p><span style="font-size:24px;"></span> </div>
</div>
</article>