import java.net.*;
import java.io.*;
public class Test
{
public static void main(String[] args) throws Exception
{
DatagramSocket ds=new DatagramSocket();
BufferedReader br=
new BufferedReader(new InputStreamReader(System.in));
String line=null;
while((line=br.readLine())!=null);
{
if("886".equals(line))
break;//这里为什么报错啊??和毕老师第23天09一样的啊???
byte[] buf=line.getBytes();
DatagramPacket dp=new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.159"),10002);
ds.send(dp);
}
ds.close();
}
}
class UDPRece2
{
public static void main(String[] args) throws Exception
{
DatagramSocket ds=new DatagramSocket(10002);
while(true)
{
byte[] bur =new byte[1024];
DatagramPacket dp=new DatagramPacket(bur,bur.length);
ds.receive(dp);
String ip=dp.getAddress().getHostAddress();
String data=new String(dp.getData(),0,dp.getLength());
System.out.println(ip+"::"+data);
}
}
}
|
|