java - 找不到符号 ois

标签 java serialization objectoutputstream objectinputstream

我正在编写简单的客户端-服务器应用程序,但我有一个愚蠢的问题(它简化了示例(当我不使用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

有人可以帮忙吗?

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

Serwer.java:

公共(public)类服务器{

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

}

} 

}

最佳答案

当您到达第 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 block 中关闭套接字。

关于java - 找不到符号 ois,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14215089/

相关文章:

java - 基于选择的 AuthProvider Spring Security

java - 我可以将接口(interface)传递给 Fragment 包吗?

python - Celery 拒绝反序列化我的自定义序列化内容并抛出 ContentDisallowed Exception

java - (objectinputstream) ObjectInput.readObject() 给我一个空对象

java - 百里香与瓷砖

java - 在同一个 Spring 应用程序中使用两个日志记录框架

java.lang.NoClassDefFoundError : javax/servlet/http/HttpServletRequest

c# - 在 C# 中处理序列化异常

java - 追加到 ObjectOutputStream(写入多个对象而不关闭流)

java - 为什么ObjectOutputStream写入14个字节而不是DataOutputStream写入8个字节?