【问题标题】:cannot find symbol ois [closed]找不到符号ois [关闭]
【发布时间】:2013-01-08 12:35:33
【问题描述】:

我正在编写简单的客户端 - 服务器应用程序,但我有一个愚蠢的问题(它简化了示例(当我不使用 java 序列化时一切正常)):

    ServerSocket serversocket=null;
    Socket socket=null;    
    String slowo=null;

    try {
        serversocket=new ServerSocket(8877);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {
        socket=serversocket.accept();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {
        ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {
        ObjectOutputStream oos=new ObjectOutputStream(socket.getOutputStream());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    slowo=(String)ois.readObject();

我的编译器显示:

Serwer.java:51: cannot find symbol
symbol  : variable ois
location: class Serwer
slowo=(String)ois.readObject();
                      ^
1 error

谁能帮忙?

我还有一个问题。为什么这个程序不发送消息?

服务器.java:

公共类服务器{

public static void main(String[] args) {
    ServerSocket serversocket=null;
    Socket socket=null;
    InputStream we=null;
    OutputStream wy=null;
    BufferedReader odczyt=null;
    BufferedReader odczytWe=null;
    DataOutputStream zapis=null;
    String slowo=null;
    String tekst=null;

    ObjectInputStream ois=null;
    ObjectOutputStream oos=null;

    try {
        serversocket=new ServerSocket(8877);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        socket=serversocket.accept();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        ois = new ObjectInputStream(socket.getInputStream());
        } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        oos=new ObjectOutputStream(socket.getOutputStream());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    //slowo=(String)ois.readObject();

    while(true) {
        try {
            slowo=(String) ois.readObject();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        if(slowo==null || slowo.equals("end")) {
            try {
                socket.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.exit(0);
        }
        else if(slowo!=null) {
            System.out.println(slowo);
        }

            odczyt=new BufferedReader(new InputStreamReader(System.in));
            try {
                tekst=odczyt.readLine();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            try {
                oos.writeObject(tekst);
                oos.flush();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            }

}

}

客户端.java:

public class Klient {
public static void main(String[] args) {

Socket socket=null;
InputStream we=null;
OutputStream wy=null;
BufferedReader odczyt=null;
BufferedReader odczytWe=null;
DataOutputStream zapis=null;
String slowo=null;
String tekst=null;
ObjectInputStream ois=null;
ObjectOutputStream  oos=null;

try {
    socket=new Socket("localhost", 8877);
} catch (UnknownHostException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
try {
    ois=new ObjectInputStream(socket.getInputStream());
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
try {
    oos=new ObjectOutputStream(socket.getOutputStream());
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

while(true) {
    try {
        slowo=(String) ois.readObject();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


    if(slowo==null || slowo.equals("end")) {
        try {
            socket.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.exit(0);
    }
    else if(slowo!=null) {
        System.out.println(slowo);
    }

        odczyt=new BufferedReader(new InputStreamReader(System.in));
        try {
            tekst=odczyt.readLine();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        try {
            oos.writeObject(tekst);
            oos.flush();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

}

} 

}

【问题讨论】:

    标签: java serialization objectoutputstream objectinputstream


    【解决方案1】:

    当你到达第 51 行时,它已经超出了范围,因为你在之前的尝试中声明了它。

    将声明移到两者之外,或以不同的方式编写代码。

    我认为这种风格杂乱无章,难以阅读。我会这样写:

    ServerSocket serversocket=null;
    String slowo="";
    try {
        serversocket=new ServerSocket(8877);
        Socket socket = serversocket.accept();
        ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
        ObjectOutputStream oos=new ObjectOutputStream(socket.getOutputStream());
        slowo=(String)ois.readObject();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        close(serversocket);
    }
    

    不要让糟糕的 IDE 为您编写糟糕的代码。

    您应该在 finally 块中关闭您的套接字。

    【讨论】:

    • +1 仅用于 IDE 代码编写注释
    猜你喜欢
    • 2016-05-08
    • 1970-01-01
    • 1970-01-01
    • 2019-11-23
    • 2021-07-26
    • 1970-01-01
    • 1970-01-01
    • 2013-02-02
    • 2014-04-14
    相关资源
    最近更新 更多