错误说套接字已关闭...我可以很好地发送到服务器,我可以很好地从服务器接收,但是一旦我尝试发送和接收,我总是会收到“套接字已关闭”错误。
服务器:
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String decodedString;
while ((decodedString = in.readLine()) != null) {
System.out.println(decodedString);
}
in.close();
OutputStreamWriter osw = new OutputStreamWriter(connection.getOutputStream());
osw.write("return: "+decodedString);
osw.flush();
osw.close();
客户:
Socket c = new Socket("localhost",4040);
OutputStreamWriter osw = new OutputStreamWriter(c.getOutputStream());
osw.write("Test");
osw.flush();
osw.close();
BufferedReader in = new BufferedReader(new InputStreamReader(c.getInputStream()));
String decodedString;
while ((decodedString = in.readLine()) != null) {
System.out.println(decodedString);
}
in.close();
c.close();