黑马程序员技术交流社区
标题:
ChatDemo.java 使用或覆盖了已过时的 API。
[打印本页]
作者:
♂诸侯♂
时间:
2012-2-24 03:02
标题:
ChatDemo.java 使用或覆盖了已过时的 API。
今天看了黑马的视频。有个例子按照视频做的。提示这个错误。谁能帮忙看看。下边是源码。
/*
多线程技术,
一个聊天程序,收,发数据
*/
import java.io.*;
import java.net.*;
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 = null;
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.100"),10000);
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
{
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(),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(10000);
new Thread(new Send(sendSocket)).start();
new Thread(new Send(receSocket)).start();
}
}
作者:
tanlei200620
时间:
2012-2-24 03:32
String data = new String(dp.getData(),dp.getLength()); 方法已过时
作者:
【专】【属】
时间:
2012-2-24 09:52
对于过时对象,是不一推荐使用的,但时对于一些老的程序中可能使用了,所以java会将标为过时。
作者:
♂诸侯♂
时间:
2012-2-24 17:59
已经解决了。当时误以为是没有编译成功。后发现只是个善意的提示而已。呵呵
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2