- import java.net.*;
- import java.io.*;
- class UdpSend
- {
- public static void main(String[] args)throws Exception
- {
- DatagramSocket ds = new DatagramSocket();
- //键盘录入
- BufferedReader bufr =
- new BufferedReader(new InputStreamReader(System.in));
- String line = null;
- while((line= bufr.readLine())!=null)
- {
- if("889".equals(line))
- break;
- byte[] buf = line.getBytes();
- DatagramPacket dp = new DatagramPacket
- (buf,buf.length,InetAddress.getByName("192.168.1.100"),10001);
- ds.send(dp);
- }
- ds.close();
- }
- }
- class UdpReceive
- {
- 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);
- }
- }
- }
复制代码
这两个类文件都放在day020.java文件中
UdpSend即可编译也可以运行
为什么UdpReceive可以编译,却运行不了!
运行时出现 错误: 仅当显式请求注释处理时才接受类名称 'UdpReceive'
那地方出错了!帮忙看一下! |