InputStream 之后 OutputStream 不工作
OutputStream not working after InputStream
我对套接字和 J2ME 很陌生,我想创建一个应用程序,它将一些文本发送到服务器(通过套接字)然后接收答复。因此,当我尝试写入输出时,出现异常:
java.net.SocketException: Socket is closed
at java.net.Socket.getOutputStream(Socket.java:943)
at server.run(server.java:55)
at java.lang.Thread.run(Thread.java:748)
这是我的服务器代码(运行() 方法):
public void run() {
try {
serverSocket = new ServerSocket(7997);
while (true) {
try {
Socket socket = serverSocket.accept();
System.out.println("connected");
InputStream in = socket.getInputStream();
String str = readAll(in);
in.close();
System.out.println(str);
JSONParser parser = new JSONParser();
JSONObject json = null;
json = (JSONObject) parser.parse(str);
text = (String) json.get("text");
presalt = (String) json.get("salt");
keytoCipher = (String) json.get("key2");
String presdvig = (String) json.get("key1");
sdvig = Long.parseLong(presdvig);
//some methods that work with input
caesarEncode();
ConKey();
precipher();
base64code();
OutputStream outputStream = socket.getOutputStream();
outputStream.write(ciphered.getBytes());
outputStream.flush();
outputStream.close();
} catch (ParseException | IOException e) {
e.printStackTrace();
}
}
//serverSocket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
这是 J2ME 客户端代码:
try {
System.out.println("connected");
JSONObject json = new JSONObject();
json.put("text", textField.getString());
json.put("salt", "defaultsalt");
json.put("key1", "228");
json.put("key2", "1488");
OutputStream out = sc.openOutputStream();
out.write(json.toString().getBytes());
out.flush();
out.close();
InputStream in = sc.openInputStream();
String str = readAll(in);
System.out.println(str);
fieldzwei.setString(str);
} catch (IOException ex) {
ex.printStackTrace();
} catch (JSONException ex) {
ex.printStackTrace();
}
此外,如果我不向服务器发送任何内容,只是在服务器连接时从中发送一条消息,它就可以正常工作。
有什么我可能错的想法吗?
这里的问题是您关闭了 Socket.getInputStream()
返回的 InputStream
:
InputStream in = socket.getInputStream();
String str = readAll(in);
in.close();
来自Javadoc of Socket.getInputStream()
:
Closing the returned InputStream will close the associated socket.
所以执行上面的代码后,你的套接字将被关闭。
tl;dr:如果您仍然需要 OutputStream,请不要关闭 InputStream。
我对套接字和 J2ME 很陌生,我想创建一个应用程序,它将一些文本发送到服务器(通过套接字)然后接收答复。因此,当我尝试写入输出时,出现异常:
java.net.SocketException: Socket is closed
at java.net.Socket.getOutputStream(Socket.java:943)
at server.run(server.java:55)
at java.lang.Thread.run(Thread.java:748)
这是我的服务器代码(运行() 方法):
public void run() {
try {
serverSocket = new ServerSocket(7997);
while (true) {
try {
Socket socket = serverSocket.accept();
System.out.println("connected");
InputStream in = socket.getInputStream();
String str = readAll(in);
in.close();
System.out.println(str);
JSONParser parser = new JSONParser();
JSONObject json = null;
json = (JSONObject) parser.parse(str);
text = (String) json.get("text");
presalt = (String) json.get("salt");
keytoCipher = (String) json.get("key2");
String presdvig = (String) json.get("key1");
sdvig = Long.parseLong(presdvig);
//some methods that work with input
caesarEncode();
ConKey();
precipher();
base64code();
OutputStream outputStream = socket.getOutputStream();
outputStream.write(ciphered.getBytes());
outputStream.flush();
outputStream.close();
} catch (ParseException | IOException e) {
e.printStackTrace();
}
}
//serverSocket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
这是 J2ME 客户端代码:
try {
System.out.println("connected");
JSONObject json = new JSONObject();
json.put("text", textField.getString());
json.put("salt", "defaultsalt");
json.put("key1", "228");
json.put("key2", "1488");
OutputStream out = sc.openOutputStream();
out.write(json.toString().getBytes());
out.flush();
out.close();
InputStream in = sc.openInputStream();
String str = readAll(in);
System.out.println(str);
fieldzwei.setString(str);
} catch (IOException ex) {
ex.printStackTrace();
} catch (JSONException ex) {
ex.printStackTrace();
}
此外,如果我不向服务器发送任何内容,只是在服务器连接时从中发送一条消息,它就可以正常工作。 有什么我可能错的想法吗?
这里的问题是您关闭了 Socket.getInputStream()
返回的 InputStream
:
InputStream in = socket.getInputStream();
String str = readAll(in);
in.close();
来自Javadoc of Socket.getInputStream()
:
Closing the returned InputStream will close the associated socket.
所以执行上面的代码后,你的套接字将被关闭。
tl;dr:如果您仍然需要 OutputStream,请不要关闭 InputStream。