代码
客户端在写入完成需要关闭输出流
ServerSocket localhost = new ServerSocket( 9999);
while (true) {
Socket accept = localhost.accept();
InputStream inputStream = accept.getInputStream();
byte[] bytes = new byte[1024];
int len = 0;
StringBuilder sb = new StringBuilder();
while ((len = inputStream.read(bytes)) != -1) {
sb.append(new String(bytes, 0, len, "GBK"));
}
if (!accept.isInputShutdown()) {
accept.shutdownInput();
}
OutputStream outputStream = accept.getOutputStream();
outputStream.write("成功接受".getBytes("GBK"));
outputStream.flush();
if (!accept.isOutputShutdown()) {
accept.shutdownOutput();
}
accept.close();
}