本帖最后由 凝聚 于 2013-12-1 16:54 编辑
package twenty_three;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.*;
class Send implements Runnable
{
private DatagramSocket dss;
public Send(DatagramSocket dss)
{
this.dss=dss;
}
private void run()/////////////////////////////////////
{
try
{
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"),6252);//255是广播地址
dss.send(dpp);
}
}
catch(Exception e)
{
throw new RuntimeException("发送端失败");
}
}
}
class Rece implements Runnable
{
private DatagramSocket dss;
public Rece(DatagramSocket dss)
{
this.dss=dss;
}
private void run()///////////////////////////////
{
try
{
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);
}
}
catch(Exception e)
{
throw new RuntimeException("接受端失败");
}
}
}
public class Ten {
public static void main(String[] args) throws Exception{
DatagramSocket sendSocket =new DatagramSocket();
DatagramSocket receSocket=new DatagramSocket(6532);
new Thread(new Send(sendSocket)).start();
new Thread(new Rece(receSocket)).start();
}
}这段代码被//标注的Run()方法这总有红线错误“Cannot reduce the visibility of the inherited method from Runnable”请问这是什么原因?
|