UDP消息发送
package com.wuming.lesson03;import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;//還是要等待客戶端的連接
public class UdpServerDemo01 {public static void main(String[] args) throws Exception {//1.開放端口DatagramSocket socket = new DatagramSocket(9090);//接受數據包byte[] buffer = new byte[1024];DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);socket.receive(packet);//阻塞接受System.out.println(packet.getAddress().getHostAddress());System.out.println(new String(packet.getData(),0,packet.getLength()));//關閉連接socket.close();}
}
===========================
package com.wuming.lesson03;import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; //不需要連接服務器 public class UdpClientDemo01 {public static void main(String[] args) throws Exception {//1.建立一個socketDatagramSocket socket = new DatagramSocket();//2.建個包String msg="你好服務器";InetAddress localhost = InetAddress.getByName("localhost");int port= 9090;//數據,數據的長度起始,要發送給誰DatagramPacket packet = new DatagramPacket(msg.getBytes(), 0, msg.getBytes().length, localhost, port);//3.發送包socket.send(packet);//4.關閉流socket.close();} }================================
?
總結
- 上一篇: C语言 va_end 宏 - C语言零基
- 下一篇: PWN题[强网先锋]orw超详细讲解(多