跟着视频教程自己写了几个程序,在处理异常的时候,抛出RuntimeException??就是编译不通过,注释掉就可以了
//同一窗口聊天
import java.io.*;
import java.net.*;
class Send implements Runnable
{
private DatagramSocket ds;
Send(DatagramSocket ds)
{
this.ds = ds;
}
public void run()
{
try
{
BufferedReader bufr =
new BufferedReader(new InputStreamReader(System.in));
String line = null;
while((line=bufr.readLine())!=null)
{
if("over".equals(line))
break;
byte[] buf = line.getBytes();
DatagramPacket dp =
new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.103"),10002);
ds.send(dp);
}
}
catch (Exception e)
{
//throw new RuntimeException("上传失败");
}
}
}
class Rece implements Runnable
{
private DatagramSocket ds;
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().getHostAddress();
String data = new String(dp.getData(),0,dp.getLength());
System.out.println(ip+":"+data);
}
}
catch (Exception e)
{
//throw new RuntimeException("读取失败");
}
}
}
class SocketDemo
{
public static void main(String[] args) throws Exception
{
DatagramSocket SendSocket = new DatagramSocket();
DatagramSocket ReceSocket = new DatagramSocket(10002);
new Thread(new Send(SendSocket)).start();
new Thread(new Rece(ReceSocket)).start();
}
}
|
|