在GoBackN客户端/服务器程序中获取消息“已弃用的API”

时间:2014-11-04 22:42:51

标签: java

我的GoBack N协议的S / W模拟在运行客户端和放大器时发出警告 - [deprecation] readLine() in DataInputStream has been deprecated(在从流中读取时)。服务器程序。请告诉我一个解决方案。readLine()还有其他选择吗?这是我的客户端/服务器程序

    //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();
    }
    }

1 个答案:

答案 0 :(得分:0)

Java API Docs复制: 已过时。 此方法无法将字节正确转换为字符。从JDK 1.1开始,读取文本行的首选方法是通过BufferedReader.readLine()方法。使用DataInputStream类读取行的程序可以通过替换表单的代码转换为使用BufferedReader类:

     DataInputStream d = new DataInputStream(in);

使用:

     BufferedReader d
          = new BufferedReader(new InputStreamReader(in));

如果你收到这样的警告,你的第一眼看上去应该始终在API上或者至少是谷歌搜索..