首页 > 代码库 > java的Socket通信例子及关于java.net.SocketException: Socket is closed错误
java的Socket通信例子及关于java.net.SocketException: Socket is closed错误
今天写socket数据相互通信的时候,碰到一个及其蛋疼的错误。单向传输数据的时候server与client是没有问题的,但是两个都有输入输出操作的时候就出现了这个问题 java.net.SocketException: Socket is closed:
下面附代码:
Server:
StringBuffer result = new StringBuffer(""); int port = 9090; //定义一个ServerSocket监听在端口9090上 ServerSocket server = null; while(true){//注意while放在哪 try { server = new ServerSocket(port); //server尝试接收其他Socket的连接请求,server的accept方法是阻塞式的 Socket client = server.accept(); server.setSoTimeout(20*1000); System.out.println("client connected!"); //接收消息 Reader read = new InputStreamReader(client.getInputStream()); BufferedReader bufferReader = new BufferedReader(read); String readLine = ""; //bufferReader.readLine()用一次读一行,前面用过再用就是下一行 while((readLine = bufferReader.readLine()) != null){ result.append(readLine); } bufferReader.close; System.out.println("from client: " + result.toString()); //boolean isConnected = socket.isConnected() && !socket.isClosed(); //读完后写一句 OutputStream os = client.getOutputStream();//这一行会报socket关闭的错误 DataOutputStream out = new DataOutputStream(os); out.writeBytes("Hello Client,I‘m Server!"); System.out.println("sent mesg"); out.flush();//清空缓存 out.close();//关闭 client.close(); } catch (IOException e) { e.printStackTrace(); } finally {//这里socket.close最好放在finally里 try { if(server != null){ server.close(); } }catch(IOException e){ e.printStackTrace(); } }
Client:
StringBuffer result = new StringBuffer("") ; String host = "192.168.0.88"; int port = 9090; Socket client = null; try { System.out.println("Connecting to " + host + " on port " + port); client = new Socket(host, port); client.setSoTimeout(20*1000); System.out.println("Just connected to "+ client.getRemoteSocketAddress());
//发送消息 OutputStream os = client.getOutputStream(); DataOutputStream out = new DataOutputStream(os); out.writeBytes("Hello Server!"); System.out.println("sent mesg"); out.flush();//清空缓存 out.close();//关闭//接收消息 Reader read = new InputStreamReader(client.getInputStream());//同样这里会报错 BufferedReader bufferReader = new BufferedReader(read); String readLine = ""; while((readLine = bufferReader.readLine()) != null){ result.append(readLine); } System.out.println("from Server: " + result.toString()); bufferReader.close; }catch(IOException e){ e.printStackTrace(); } finally { try { if(client != null){ client.close(); } }catch(IOException e){ e.printStackTrace(); } }
找了很久终于找到 bufferReader.close()与out.close()关闭会直接导致sockect.close()故只进行输入火鼠初中一个之后就出现java.net.SocketException: Socket is closed:错误,可能是因为用到了socket.getOutputStream(),socket.getInputStream()的缘故。
这里将out.close()与bufferReader.close()兑换成client.shutdownOutput()与client.shutdownInput();之后就不会出现Socket直接被关闭的问题了。
Socket.close()->输入输出流都被关闭->有时候希望仅关闭输入流或输出流之一->Socket半关闭方法->
shutdownInput():仅关闭输入流
shutdownOutput():仅关闭输出流
上述问题中如果把bufferReader.close()与out.close()放在最后,就会出现Server在read的时候因为Client未关闭outputstream一直傻等下去。故此种方法不可取。
java的Socket通信例子及关于java.net.SocketException: Socket is closed错误
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。