*
* UDP发送端
* 需求:不断的键盘录入对象(BufferedReader(new InputStreamReader(System.in)))的readLine
* 当前客户输入886,程序结束(客户端不在录入)
import java.io.BufferedReader;
public class SendDemo {
public static void main(String[] args) throws IOException {
// 创建发送端Socket
DatagramSocket ds=new DatagramSocket();
//键盘录入
BufferedReader br=new BufferedReader(
new InputStreamReader(System.in));
//读取一行数据
String s=null;
while((s=br.readLine())!=null) {
if("886".equals(s)) {
break;
}
//发送数据(数据报包)
//创建DatagramPacket
DatagramPacket dp=new DatagramPacket(
s.getBytes(),s.getBytes().length,
InetAddress.getByName("WL"),1000);
//发送数据
ds.send(dp);
}
ds.close();
}
}
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
//接收端: