/*
报的这错误
Exception in thread "Thread-0" java.lang.RuntimeException: 发送端异常
at Send.run(ChatDemo.java:50)
at java.lang.Thread.run(Thread.java:745)
*/
import java.io.*;
import java.net.*;
class ChatDemo
{
public static void main(String[] args) throws Exception
{
DatagramSocket sendSocket = new DatagramSocket();
DatagramSocket receiveSocket = new DatagramSocket(10001);
new Thread(new Send(sendSocket)).start();
new Thread(new Receive(receiveSocket)).start();
}
}
class Send implements Runnable
{
private DatagramSocket ds;
public Send(DatagramSocket ds)
{
this.ds = ds;
}
public void run()
{
try
{
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
String line;
while((line= bufr.readLine())!=null)
{
if("886".equals(line))
break;
byte[] buf = line.getBytes();
DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.105)"),10001);
ds.send(dp);
}
ds.close();
}
catch (Exception e)
{
throw new RuntimeException("发送端异常");
}
}
}
class Receive implements Runnable
{
private DatagramSocket ds;
public Receive(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 data = new String(dp.getData(),0,dp.getLength());
String ip = dp.getAddress().getHostAddress();
System.out.println(ip+"::"+data);
}
}
catch (Exception e)
{
throw new RuntimeException("接收端异常");
}
}
}
|
|