java socket保持长连接
来源:互联网 发布:linux 安装maven步骤 编辑:程序博客网 时间:2024/05/06 03:06
ServerSocket serverSocket=new ServerSocket(1111);Socket socket=serverSocket.accept();while(true){//保持长连接 try { Thread.sleep(100);//等待时间 } catch (InterruptedException e1) { e1.printStackTrace(); }if (socket !=null){ try { String ip = socket.getInetAddress().toString().replace("/", ""); System.out.println("====socket.getInetAddress()====="+ip); socket.setKeepAlive(true); InputStream is = socket.getInputStream(); OutputStream os = socket.getOutputStream(); System.out.println("服务器端接受请求");String tempdata = StreamEazyUse.getContent(is);system.out.printl(“接收到的数据为:”+tempdata);if(tempdata.contains(“stop”)){is.close();os.close();}os.flush();}catch(Exception e){system.out.println(“出现了错误”);}}}