为啥从键盘录入数据后,没有反应,请大侠帮忙看看,郁闷半天了
import java.net.*;
import java.io.*;
class Send implements Runnable
{
private DatagramSocket ds;
public Send(DatagramSocket ds)
{
this.ds = ds;
}
public void run()
{
try
{
while(true)
{
BufferedReader bufr =
new BufferedReader(new InputStreamReader(System.in));
String line = null;
while ((line = bufr.readLine())!= null)
{
byte[] buf = new byte[1024];
DatagramPacket dp = new
DatagramPacket(buf,buf.length,InetAddress.getByName("172.0.0.1"),10003);
ds.send(dp);
}
}
}
catch (Exception e)
{
throw new RuntimeException("发送数据失败");
}
}
}
class Rece implements Runnable
{
private DatagramSocket ds;
public Rece(DatagramSocket ds)
{
this.ds = ds;
}
public void run()
{
try
{
while (true)
{
byte [] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf,buf.length);
ds.receive(dp);
String ip = dp.getAddress().getHostName();
String data = new String(dp.getData(),0,dp.getLength());
System.out.println(ip+"::"+data);
}
}
catch (Exception e)
{
throw new RuntimeException("接收失败");
}
}
}
class ChatDemo
{
public static void main(String[] args)throws Exception
{
DatagramSocket sendSocket = new DatagramSocket();
DatagramSocket receSocket = new DatagramSocket();
new Thread(new Send(sendSocket)).start();
new Thread(new Rece(receSocket)).start();
}
}
|
|