gpt4 book ai didi

java - 找不到符号 ois

转载 作者:行者123 更新时间:2023-12-01 08:14:58 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com