Example usage for java.net Socket getOutputStream

List of usage examples for java.net Socket getOutputStream

Introduction

In this page you can find the example usage for java.net Socket getOutputStream.

Prototype

public OutputStream getOutputStream() throws IOException 

Source Link

Document

Returns an output stream for this socket.

Usage

From source file:com.zhch.example.commons.http.v4_5.ProxyTunnelDemo.java

/**
 * ?? demo//www. ja  va 2s.c  o  m
 * 
 * @throws Exception
 */
public final static void officalDemo() throws Exception {
    ProxyClient proxyClient = new ProxyClient();
    HttpHost target = new HttpHost("www.yahoo.com", 80);
    HttpHost proxy = new HttpHost("localhost", 8888);
    UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("user", "pwd");
    Socket socket = proxyClient.tunnel(proxy, target, credentials);
    try {
        Writer out = new OutputStreamWriter(socket.getOutputStream(), HTTP.DEF_CONTENT_CHARSET);
        out.write("GET / HTTP/1.1\r\n");
        out.write("Host: " + target.toHostString() + "\r\n");
        out.write("Agent: whatever\r\n");
        out.write("Connection: close\r\n");
        out.write("\r\n");
        out.flush();
        BufferedReader in = new BufferedReader(
                new InputStreamReader(socket.getInputStream(), HTTP.DEF_CONTENT_CHARSET));
        String line = null;
        while ((line = in.readLine()) != null) {
            System.out.println(line);
        }
    } finally {
        socket.close();
    }
}

From source file:Main.java

/**
 * Performs execution of the shell command on remote host.
 * @param host ip address or name of the host.
 * @param port telnet port//www  . j  a  v  a2  s .c  o m
 * @param command shell command to be executed.
 * @return true if success, false on error.
 */
public static final boolean executeRemotely(String host, int port, String command) {
    Socket socket = null;
    OutputStream os = null;

    try {
        socket = new Socket(host, port);

        os = socket.getOutputStream();

        os.write(command.getBytes());
        os.flush();

        return true;
    } catch (UnknownHostException e) {
        e.printStackTrace();
        return false;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    } finally {

        if (os != null) {
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        if (socket != null && !socket.isClosed()) {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:fitnesse.http.ResponseParser.java

public static ResponseParser performHttpRequest(String hostname, int hostPort, RequestBuilder builder)
        throws IOException {
    Socket socket = new Socket(hostname, hostPort);
    OutputStream socketOut = socket.getOutputStream();
    InputStream socketIn = socket.getInputStream();
    builder.send(socketOut);/*from ww  w .j av a2  s  .  com*/
    socketOut.flush();
    try {
        return new ResponseParser(socketIn);
    } finally {
        socketOut.close();
        socket.close();
    }
}

From source file:Main.java

/**
 *
 * Resolve DNS name into IP address/*w  w  w  . ja v  a 2 s  .c o  m*/
 *
 * @param host DNS name
 * @return IP address in integer format
 * @throws IOException
 */
public static int resolve(String host) throws IOException {
    Socket localSocket = new Socket(ADB_HOST, ADB_PORT);
    DataInputStream dis = new DataInputStream(localSocket.getInputStream());
    OutputStream os = localSocket.getOutputStream();
    int count_read = 0;

    if (localSocket == null || dis == null || os == null)
        return -1;
    String cmd = "dns:" + host;

    if (!sendAdbCmd(dis, os, cmd))
        return -1;

    count_read = dis.readInt();
    localSocket.close();
    return count_read;
}

From source file:com.zhch.example.commons.http.v4_5.ProxyTunnelDemo.java

/**
 *  demo  /*from  w ww.j  a  v a  2 s.co m*/
 * 
 * @throws Exception
 */
public final static void zhchModifyDemo() throws Exception {

    ProxyClient proxyClient = new ProxyClient();
    HttpHost target = new HttpHost("pan.baidu.com", 80);
    //      HttpHost proxy = new HttpHost("120.24.0.162", 80);
    HttpHost proxy = new HttpHost("127.0.0.1", 80);
    UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("abc", "def");
    System.out.println("before tunnel.");
    Socket socket = proxyClient.tunnel(proxy, target, credentials);
    System.out.println("after tunnel.");

    try {
        Writer out = new OutputStreamWriter(socket.getOutputStream(), HTTP.DEF_CONTENT_CHARSET);
        out.write("GET / HTTP/1.1\r\n");
        out.write("Host: " + target.toHostString() + "\r\n");
        out.write("Agent: whatever\r\n");
        out.write("Connection: close\r\n");
        out.write("\r\n");
        out.flush();
        BufferedReader in = new BufferedReader(
                new InputStreamReader(socket.getInputStream(), HTTP.DEF_CONTENT_CHARSET));
        String line = null;
        while ((line = in.readLine()) != null) {
            System.out.println(line);
        }
    } finally {
        socket.close();
    }
}

From source file:Main.java

/**
 *
 * Get a tcp socket connection to specified IP address and port proxied by adb
 *
 * The proxying is transparent, e.g. if a socket is returned, then it can be written to and
 * read from as if it is directly connected to the target
 *
 * @param remoteAddress IP address of the host to connect to
 * @param remotePort port of the host to connect to
 * @return a valid Socket instance if successful, null otherwise
 *//*from www  .jav  a2  s  . c  om*/
public static Socket getForwardedSocket(int remoteAddress, int remotePort) {
    try {
        Socket socket = new Socket(ADB_HOST, ADB_PORT);
        String cmd = "tcp:" + remotePort + ":" + convert(remoteAddress);
        if (!sendAdbCmd(socket.getInputStream(), socket.getOutputStream(), cmd)) {
            socket.close();
            return null;
        }
        return socket;
    } catch (IOException ioe) {
        Log.w(LOGTAG, "error creating adb socket", ioe);
        return null;
    }
}

From source file:Main.java

/**
 * Sends Gps data to emulator, and the start value has an offset.
 * /*from  w w  w  . ja  v a  2 s .co m*/
 * @param number send times
 * @param offset is used to compute the start latitude and longitude
 * @param pause pause interval between each sending
 */
public static void sendGps(int number, int offset, int pause) {
    if (number < 1) {
        return;
    }

    int pauseInterval = TINY_WAIT_TIME;
    if (pause != -1) {
        pauseInterval = pause;
    }

    // If it's a real device, does not send simulated GPS signal.
    if (!isEmulator) {
        return;
    }

    PrintStream out = null;
    Socket socket = null;
    try {
        socket = new Socket(ANDROID_LOCAL_IP, emulatorPort);
        out = new PrintStream(socket.getOutputStream());
        double longitude = START_LONGITUDE + offset * DELTA_LONGITUDE;
        double latitude = START_LATITUDE + offset * DELTA_LADITUDE;
        for (int i = 0; i < number; i++) {
            out.println("geo fix " + longitude + " " + latitude);
            longitude += DELTA_LONGITUDE;
            latitude += DELTA_LADITUDE;
            Thread.sleep(pauseInterval);
        }
        // Wait the GPS signal can be obtained by MyTracks.
        Thread.sleep(SHORT_WAIT_TIME);
    } catch (UnknownHostException e) {
        System.exit(-1);
    } catch (IOException e) {
        System.exit(-1);
    } catch (InterruptedException e) {
        System.exit(-1);
    } finally {
        if (out != null) {
            out.close();
        }
    }
}

From source file:mainserver.Server.java

public static void sendMessage(String text, Socket clientSocket) {
    try {//  w w  w. ja va2s . c o m
        BufferedWriter outputToServer = new BufferedWriter(
                new OutputStreamWriter(clientSocket.getOutputStream()));
        outputToServer.write(text + "\r\n");
        outputToServer.flush();

    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:com.comcast.viper.flume2storm.zookeeper.ZkTestUtils.java

/**
 * Utility function to send a command to the internal server. ZK servers
 * accepts 4 byte command strings to test their liveness.
 *//*www  . j  a va2  s. c  om*/
protected static String send4LetterWord(final String host, final int port, final String cmd) {
    Preconditions.checkArgument(cmd.length() == 4);
    try {
        final Socket sock = new Socket(host, port);
        OutputStream outstream = null;
        BufferedReader reader = null;
        try {
            outstream = sock.getOutputStream();
            outstream.write(cmd.getBytes());
            outstream.flush();

            reader = new BufferedReader(new InputStreamReader(sock.getInputStream()));
            final StringBuilder sb = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            return sb.toString();
        } finally {
            if (outstream != null) {
                outstream.close();
            }
            sock.close();
            if (reader != null) {
                reader.close();
            }
        }
    } catch (final Exception e) {
        System.out.println(e.getMessage());
        return StringUtils.EMPTY;
    }
}

From source file:Main.java

/**
 * Sends Gps data to emulator, and the start value has an offset.
 * //  w ww  .  ja  va2s .c  o m
 * @param number send times
 * @param offset is used to compute the start latitude and longitude 
 * @param pause pause interval between each sending
 */
public static void sendGps(int number, int offset, int pause) {
    if (number < 1) {
        return;
    }

    int pauseInterval = PAUSE_DEFAULT;
    if (pause != -1) {
        pauseInterval = pause;
    }

    // If it's a real device, does not send simulated GPS signal.
    if (!isEmulator) {
        return;
    }

    PrintStream out = null;
    Socket socket = null;
    try {
        socket = new Socket(ANDROID_LOCAL_IP, emulatorPort);
        out = new PrintStream(socket.getOutputStream());
        double longitude = START_LONGITUDE + offset * DELTA_LONGITUDE;
        double latitude = START_LATITUDE + offset * DELTA_LADITUDE;
        for (int i = 0; i < number; i++) {
            out.println("geo fix " + longitude + " " + latitude);
            longitude += DELTA_LONGITUDE;
            latitude += DELTA_LADITUDE;
            Thread.sleep(pauseInterval);
        }
        // Wait the GPS signal can be obtained by MyTracks.  
        Thread.sleep(SHORT_WAIT_TIME);
    } catch (UnknownHostException e) {
        System.exit(-1);
    } catch (IOException e) {
        System.exit(-1);
    } catch (InterruptedException e) {
        System.exit(-1);
    } finally {
        if (out != null) {
            out.close();
        }
    }
}