/*
UDP-键盘录入数据,发送、接收
还是将两个应用程序写在一个文件下。便于观看。
*/
import java.net.*;
import java.io.*;
class UdpSend1
{
public static void main(String[] args) throws Exception
{
DatagramSocket ds = new DatagramSocket(8888);
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
String line=null;
while((line=bufr.readLine())!=null)
{
if("886".equals(line))
break;
DatagramPacket dp =
new DatagramPacket(line.getBytes(),line.getBytes().length,InetAddress.getByName("192.168.1.255"),10001);
ds.send(dp);
}
ds.close();
}
}
class UdpReceive1
{
public static void main(String[] args) throws Exception
{
DatagramSocket ds = new DatagramSocket(10001);
while(true)
{
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf,buf.length);
ds.receive(dp);
String ip = dp.getAddress().getHostAddress();
String data = new String(dp.getData(),0,dp.getLength());
System.out.println(ip+"::"+data);
}
//关闭资源
//ds.close();
}
}
day23/UdpDemo1.java
最新推荐文章于 2024-07-06 00:20:02 发布