UDP协议的网络编程的基本步骤

function action(m) { //js中读取的样式必须是内嵌样式 $(m).style.display = $(m).style.display == 'none' ? '' : 'none'; } function changeColor(e) { $('udp').style.color =$('udp').style.color == '' ? '#ff0000' : ''; //事件冒泡的阻止!!! e.cancelBubble = true; } function colorServer(e) { $('server').style.color=$('server').style.color == '' ? '#00ff00' : ''; //事件冒泡的阻止!!! e.cancelBubble = true; } function colorClient(e) { $('client').style.color = $('client').style.color == '' ? '#0000ff' : ''; //事件冒泡的阻止!!! e.cancelBubble = true; }

<script type="text/javascript" src="js/prototype-1.6.0.3.js"> </script> <script type="text/javascript" src="js/list.js"> </script> <div οnclick="action('ul');" id="udp"> UDP <input type="button" value="Change Color" οnclick="changeColor(event);" /> </div> <ol id="ul" style="display: none"> <li> <div οnclick="action('u11');" id="server"> UDPServer <input type="button" value="Change Color" οnclick="colorServer(event);" /> </div> <ol id="u11" style="display: none"> <li> 建立信箱 </li> <li> 准备收件信封 </li> <li> 收信 </li> <li> 通过信封得到发信人的地址和端口号 </li> <li> 准备发信封 </li> <li> 发信(回信) </li> <li> 关闭信箱 </li> </ol> </li> <li> <div οnclick="action('u12');" id="client"> UDPClient <input type="button" value="Change Color" οnclick="colorClient(event);" /> </div> <ol id="u12" style="display: none"> <li> 准备信箱 </li> <li> 准备发件信封 </li> <li> 发信 </li> <li> 准备收件信封 </li> <li> 收信 </li> <li> 关闭信箱 </li> </ol> </li> </ol> import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketException; public class UDPServer { public UDPServer() throws IOException { byte[] buf = new byte[26]; DatagramSocket socket = new DatagramSocket(80); DatagramPacket packet = new DatagramPacket(buf, buf.length); socket.receive(packet); packet = new DatagramPacket(buf, buf.length, packet.getAddress(), packet.getPort()); socket.send(packet); socket.close(); } public static void main(String[] args) throws IOException { new UDPServer(); } }

import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketException; import java.net.UnknownHostException; public class UDPClient { public UDPClient() throws IOException { byte[] buf = new byte[26]; for (int i = 0; i < buf.length; i++) { buf[i] = (byte) ('a' + i); } DatagramSocket socket = new DatagramSocket(8000); DatagramPacket packet = new DatagramPacket(buf, buf.length, InetAddress .getByName("127.0.0.1"), 80); socket.send(packet); packet = new DatagramPacket(buf, buf.length); socket.receive(packet); System.out.println(new String(packet.getData())); System.out.println(new String(buf)); socket.close(); } public static void main(String[] args) throws IOException { new UDPClient(); } }




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值