------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------
写着两个程序是我是卸载两个java文件下的,而且这两个文件分别放在了两个不同的目录下,也就是同时打开两个MyEclipse窗口,这样有利于调试,查看效果更明显。
发送方:
package twenty_three;
import java.net.*;
import java.io.*;
public class Upsent2 {
public static void main(String[] args)throws Exception {
DatagramSocket dss=new DatagramSocket();
BufferedReader bufer=new BufferedReader(new InputStreamReader(System.in));
String line=null;
while((line=bufer.readLine())!=null)
{
if("886".equals(line))
break;
byte[]buf=line.getBytes();
DatagramPacket dpp=new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.106.255"),6253);//255是广播地址
dss.send(dpp);
}
dss.close();
}
}
接收方:
package twenty_three;
import java.net.*;
import java.io.*;
public class UpRece2 {
public static void main(String[] args)throws Exception {
DatagramSocket dss=new DatagramSocket(6253);
while(true)
{
byte[]buf=new byte[1024];
DatagramPacket dpp=new DatagramPacket(buf,buf.length);
dss.receive(dpp);
String ip=dpp.getAddress().getHostAddress();
String data=new String(dpp.getData(),0,dpp.getLength());
System.out.println(ip+":::"+data);
}
}
}
------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------