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 onclick="action('ul');" id="udp">
UDP
<input type="button" value="Change Color" onclick="changeColor(event);" />
</div>
<ol id="ul" style="display: none">
<li>
<div onclick="action('u11');" id="server">
UDPServer
<input type="button" value="Change Color"
onclick="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 onclick="action('u12');" id="client">
UDPClient
<input type="button" value="Change Color"
onclick="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();
}
}
UDP协议的网络编程的基本步骤
最新推荐文章于 2022-08-31 11:31:40 发布