0

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

}

} 

}

4

1 回答 1

6

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

于 2013-01-08T12:37:08.157 回答