Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/share/jdwp/SocketTransport.java
41162 views
/*1* Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223package nsk.share.jdwp;2425import nsk.share.*;2627import java.io.*;28import java.net.*;2930/**31* This class represents a socket transport for JDWP.32*/33public class SocketTransport extends Transport {3435/**36* Socket for established JDWP connection.37*/38private Socket socket = null;3940/**41* ServerSocket for listening JDWP connection.42*/43private ServerSocket serverSocket = null;4445/**46* Input stream of socket.47*/48private InputStream in = null;4950/**51* Output stream of socket.52*/53private OutputStream out = null;5455/**56* Port to listen to.57*/58int listenPort = 0;5960/**61* Make <code>SocketTransport</cose> object with providing specified Log.62*/63public SocketTransport(Log log) {64super(log);65}6667/**68* Bind for connection from target VM to specified port number.69* If given port number is 0 then bind to any free port number.70*71* @return port number which this transport is listening for72*/73public int bind(int port)74throws IOException {75serverSocket = new ServerSocket();76if (port == 0) {77// Only need SO_REUSEADDR if we're using a fixed port. If we78// start seeing EADDRINUSE due to collisions in free ports79// then we should retry the bind() a few times.80display("port == 0, disabling SO_REUSEADDR");81serverSocket.setReuseAddress(false);82}83serverSocket.bind(new InetSocketAddress(port));84listenPort = serverSocket.getLocalPort();85return listenPort;86}8788/**89* Attach to the target VM for specified host name and port number.90*/91public void attach(String ServerName, int PortNumber)92throws UnknownHostException, IOException {93for (int i = 0; i < Binder.CONNECT_TRIES; i++ ) {94try {95socket = new Socket(ServerName, PortNumber);96display("JDWP socket connection established");97// socket.setTcpNoDelay(true);98in = socket.getInputStream();99out = socket.getOutputStream();100return;101} catch (IOException e) {102display("Attempt #" + i + " to establish JDWP socket connection failed:\n\t" + e);103try {104Thread.currentThread().sleep(Binder.CONNECT_TRY_DELAY);105} catch (InterruptedException ie) {106ie.printStackTrace(log.getOutStream());107throw new Failure("Thread interrupted while establishing JDWP connection: \n\t"108+ ie);109}110}111}112throw new IOException("Timeout exceeded while establishing JDWP socket connection to "113+ ServerName + ":" + PortNumber);114}115116/**117* Accept connection from target VM for already boud port.118*/119public void accept()120throws IOException {121122if (serverSocket == null)123throw new Failure("Attempt to accept JDWP socket connection from unbound port");124125socket = serverSocket.accept();126serverSocket.close();127serverSocket = null;128129in = socket.getInputStream();130out = socket.getOutputStream();131}132133/**134* Close socket and streams.135*/136public void close() throws IOException {137138if (socket == null)139return;140141if (out != null) {142flush();143out.close();144}145146if (in != null) {147in.close();148}149150if (socket != null) {151socket.close();152socket = null;153}154155if (serverSocket != null) {156serverSocket.close();157serverSocket = null;158}159}160161/**162* Return number of bytes that can be read.163*/164public int available() throws IOException {165return in.available();166};167168/**169* Flush bytes being buffered for writing if any.170*/171public void flush() throws IOException {172out.flush();173}174175/**176* Set timeout for reading data in milliseconds.177*/178public void setReadTimeout(long millisecs) throws IOException {179socket.setSoTimeout((int)millisecs);180}181182/**183* Read the next byte of data from the socket.184* The value byte is returned as an int in the range 0 to 255.185* If no byte is available, the value -1 is returned.186*/187public byte read() throws IOException {188int b = in.read();189if (b < 0) {190throw new IOException("JDWP socket connection closed by remote host");191}192return (byte) b;193};194195/**196* Write the specified byte to the socket.197*/198public void write(int b) throws IOException {199out.write(b);200};201}202203204