Java 在GoBackN客户端/服务器程序中获取消息“已弃用的API”
我对GoBack N协议的S/W模拟给出警告-[deprecation]在运行客户端和服务器程序时从流中读取数据时,DataInputStream中的readLine已被弃用。请告诉我解决方案。readLine是否有其他替代方案?这是我的客户机/服务器程序Java 在GoBackN客户端/服务器程序中获取消息“已弃用的API”,java,Java,我对GoBack N协议的S/W模拟给出警告-[deprecation]在运行客户端和服务器程序时从流中读取数据时,DataInputStream中的readLine已被弃用。请告诉我解决方案。readLine是否有其他替代方案?这是我的客户机/服务器程序 //Server Program import java.io.*; import java.net.*; import java.lang.*; public class gobacksender
//Server Program
import java.io.*;
import java.net.*;
import java.lang.*;
public class gobacksender
{
public static void main(String[] args)throws Exception
{
//Establishing connection
ServerSocket ss=new ServerSocket(4444);
System.out.println("Waiting for connection........");
Socket s=ss.accept();
System.out.println("Connected to client at:"+s);
DataInputStream in=new DataInputStream(System.in);
DataInputStream in1=new DataInputStream(s.getInputStream());
PrintStream p=new PrintStream(s.getOutputStream());
int sptr=0,sws=8,i,ano,nf,n;
String sbuf[]=new String[8];
String ch,ch1;
System.out.println("Enter the no.of frames");
nf=Integer.parseInt(in.readLine());
p.println(nf);
if(nf<=sws-1)
{
System.out.println("Enter the "+nf+" messages to be send");
for(i=1;i<=nf;i++)
{
sbuf[sptr]=in.readLine();
p.println(sbuf[sptr]);
sptr=++sptr%8;
}
sws-=nf;
do
{
ano=Integer.parseInt(in.readLine());
System.out.println("Received acknowledgement for:"+ano);
System.out.println("Retransmitting...............");
for(i=ano-1;i<nf;i++)
p.println(i);
ch1=in1.readLine();
}while(ch1.equals("yes"));
sws+=nf;
}
else
{
System.out.println("the no.of frames exceed window size");
}
ano=Integer.parseInt(in1.readLine());
System.out.println("Acknowledgement received for"+ano+"frames");
s.close();
}
}
//Client Program
import java.io.*;
import java.net.*;
import java.lang.*;
public class gobackreceiver
{
public static void main(String[] args)throws Exception
{
//Connection establishment
Socket s=new Socket("localhost",4444);
System.out.println("Connected to server at:"+s);
DataInputStream in=new DataInputStream(System.in);
DataInputStream in1=new DataInputStream(s.getInputStream());
PrintStream p=new PrintStream(s.getOutputStream());
String rbuf[]=new String[8];
int i,rptr=0,rws=8,nf,n;
String ch,ch1;
do
{
nf=Integer.parseInt(in1.readLine());
System.out.println("No.of messages",nf);
if(nf<rws)
{
for(i=1;i<=nf;i++)
{
rbuf[rptr]=in1.readLine();
rptr=++rptr%8;
System.out.println("The received frame"+(rptr+1)+"is:"+rbuf[rptr]);
}
rws-=nf;
do
{
System.out.println("Enter the nak number");
n=Integer.parseInt(in.readLine());
if((1<=n)&&(n<=nf))
{
p.println(n);
for(i=n;i<nf;i++)
{
rbuf[i]=in1.readLine();
System.out.println("the received frame"+i+"is;"+rbuf[i]);
}
}
else
{
System.out.println("Invalid nak");
}
System.out.println("Anymore frames lost?");
ch1=in.readLine();
p.println(ch1);
}while(ch1.equals("yes"));
System.out.println("Ack sent");
p.println(rptr+1);
rws+=nf;
}
else
break;
ch=in.readLine();
}while(ch.equals("yes"));
s.close();
}
}
复制自:
不赞成。
此方法无法将字节正确转换为字符。从JDK1.1开始,读取文本行的首选方法是通过BufferedReader.readLine方法。使用DataInputStream类读取行的程序可以通过替换以下形式的代码转换为使用BufferedReader类:
DataInputStream d = new DataInputStream(in);
与:
如果你收到这样的警告,你的第一眼应该是在API上,或者至少在谷歌上 这些词是“弃用”和“弃用”。错误信息必须准确引用。Javadoc回答了您的问题-1.
BufferedReader d
= new BufferedReader(new InputStreamReader(in));