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.supernovapps.audio.jstreamsourcer.ShoutcastV1Test.java

@Test
public void testOnSourcerListenerFails() throws IOException {
    Socket sockMock = EasyMock.createNiceMock(Socket.class);

    OnSourcerListener listener = new OnSourcerListener() {

        @Override//  w w  w. ja  v  a 2 s.c  om
        public void onError(String string) {
            onConnected = true;
        }

        @Override
        public void onDisconnected(boolean connected) {
        }

        @Override
        public void onConnected() {
        }
    };

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ByteArrayInputStream in = new ByteArrayInputStream(new String("KO").getBytes());

    EasyMock.expect(sockMock.getOutputStream()).andReturn(out);
    EasyMock.expect(sockMock.getInputStream()).andReturn(in);
    EasyMock.replay(sockMock);

    shoutcast.setOnSourcerListener(listener);
    shoutcast.start(sockMock);

    Assert.assertTrue(onConnected);
}

From source file:com.chigix.bio.proxy.buffer.FixedBufferTest.java

License:asdf

@Test
public void testHTTPS() {
    System.out.println("BANKAI");
    Socket tmp_socket = null;// www  .  j  av a2  s. c  o m
    try {
        tmp_socket = new Socket("jp.jfwq.net", 6002);
    } catch (IOException ex) {
        Logger.getLogger(FixedBufferTest.class.getName()).log(Level.SEVERE, null, ex);
    }
    final Socket socket = tmp_socket;
    new Thread() {

        @Override
        public void run() {
            while (true) {
                try {
                    int read;
                    System.out.print(read = socket.getInputStream().read());
                    System.out.print((char) read);
                    System.out.print(",");
                } catch (IOException ex) {
                    Logger.getLogger(FixedBufferTest.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

    }.start();
    try {
        socket.getOutputStream()
                .write("CONNECT www.dnspod.cn:443 HTTP/1.1\nHost: www.dnspod.cn:443\n\n".getBytes());
    } catch (IOException ex) {
        Logger.getLogger(FixedBufferTest.class.getName()).log(Level.SEVERE, null, ex);
    }
    try {
        Thread.sleep(50000);
    } catch (InterruptedException ex) {
        Logger.getLogger(FixedBufferTest.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.supernovapps.audio.jstreamsourcer.ShoutcastV1Test.java

@Test
public void testOnSourcerListenerSuccess() throws IOException {
    Socket sockMock = EasyMock.createNiceMock(Socket.class);

    OnSourcerListener listener = new OnSourcerListener() {

        @Override/*from   w w w . j a  va  2s  .  com*/
        public void onError(String string) {
        }

        @Override
        public void onDisconnected(boolean connected) {
        }

        @Override
        public void onConnected() {
            onConnected = true;
        }
    };

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ByteArrayInputStream in = new ByteArrayInputStream(new String("HTTP OK").getBytes());

    EasyMock.expect(sockMock.getOutputStream()).andReturn(out);
    EasyMock.expect(sockMock.getInputStream()).andReturn(in);
    EasyMock.replay(sockMock);

    shoutcast.setOnSourcerListener(listener);
    shoutcast.start(sockMock);

    Assert.assertTrue(onConnected);
}

From source file:com.supernovapps.audio.jstreamsourcer.IcecastTest.java

@Test
public void testOnSourcerListenerFails() throws IOException {
    Socket sockMock = EasyMock.createNiceMock(Socket.class);

    OnSourcerListener listener = new OnSourcerListener() {

        @Override//w w w  .  j  av  a  2 s.c  o m
        public void onError(String string) {
            onConnected = true;
        }

        @Override
        public void onDisconnected(boolean connected) {
        }

        @Override
        public void onConnected() {
        }
    };

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ByteArrayInputStream in = new ByteArrayInputStream(new String("KO").getBytes());

    EasyMock.expect(sockMock.getOutputStream()).andReturn(out);
    EasyMock.expect(sockMock.getInputStream()).andReturn(in);
    EasyMock.replay(sockMock);

    icecast.setOnSourcerListener(listener);
    icecast.start(sockMock);

    Assert.assertTrue(onConnected);
}

From source file:com.gargoylesoftware.htmlunit.NoHttpResponseTest.java

@Override
public void run() {
    try {//from   ww  w . j  a v a  2  s .  co m
        serverSocket_ = new ServerSocket(port_);
        started_.set(true);
        LOG.info("Starting listening on port " + port_);
        while (!shutdown_) {
            final Socket s = serverSocket_.accept();
            final BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));

            final CharBuffer cb = CharBuffer.allocate(5000);
            br.read(cb);
            cb.flip();
            final String in = cb.toString();
            cb.rewind();

            final RawResponseData responseData = getResponseData(in);

            if (responseData == null || responseData.getStringContent() == DROP_CONNECTION) {
                LOG.info("Closing impolitely in & output streams");
                s.getOutputStream().close();
            } else {
                final PrintWriter pw = new PrintWriter(s.getOutputStream());
                pw.println("HTTP/1.0 " + responseData.getStatusCode() + " " + responseData.getStatusMessage());
                for (final NameValuePair header : responseData.getHeaders()) {
                    pw.println(header.getName() + ": " + header.getValue());
                }
                pw.println();
                pw.println(responseData.getStringContent());
                pw.println();
                pw.flush();
                pw.close();
            }
            br.close();
            s.close();
        }
    } catch (final SocketException e) {
        if (!shutdown_) {
            LOG.error(e);
        }
    } catch (final IOException e) {
        LOG.error(e);
    } finally {
        LOG.info("Finished listening on port " + port_);
    }
}

From source file:com.supernovapps.audio.jstreamsourcer.ShoutcastV1Test.java

@Test
public void testOnSourcerListenerDisconnect() throws IOException {
    Socket sockMock = EasyMock.createNiceMock(Socket.class);

    OnSourcerListener listener = new OnSourcerListener() {

        @Override/*from ww  w  . j  a  va  2 s  .  co m*/
        public void onError(String string) {
        }

        @Override
        public void onDisconnected(boolean connected) {
            onConnected = true;
        }

        @Override
        public void onConnected() {
        }
    };

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ByteArrayInputStream in = new ByteArrayInputStream(new String("HTTP OK").getBytes());

    EasyMock.expect(sockMock.getOutputStream()).andReturn(out);
    EasyMock.expect(sockMock.getInputStream()).andReturn(in);
    EasyMock.replay(sockMock);

    shoutcast.setOnSourcerListener(listener);

    boolean started = shoutcast.start(sockMock);
    Assert.assertTrue(started);

    boolean stopped = shoutcast.stop();
    Assert.assertTrue(stopped);

    Assert.assertTrue(onConnected);
}

From source file:com.supernovapps.audio.jstreamsourcer.IcecastTest.java

@Test
public void testOnSourcerListenerSuccess() throws IOException {
    Socket sockMock = EasyMock.createNiceMock(Socket.class);

    OnSourcerListener listener = new OnSourcerListener() {

        @Override//from  w  w w  .j a v a2 s . c  o  m
        public void onError(String string) {
        }

        @Override
        public void onDisconnected(boolean connected) {
        }

        @Override
        public void onConnected() {
            onConnected = true;
        }
    };

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ByteArrayInputStream in = new ByteArrayInputStream(new String("HTTP OK").getBytes());

    EasyMock.expect(sockMock.getOutputStream()).andReturn(out);
    EasyMock.expect(sockMock.getInputStream()).andReturn(in);
    EasyMock.replay(sockMock);

    icecast.setOnSourcerListener(listener);
    icecast.start(sockMock);

    Assert.assertTrue(onConnected);
}

From source file:co.cask.cdap.gateway.router.NettyRouterTestBase.java

@Test(timeout = 10000)
public void testConnectionIdleTimeout() throws Exception {
    defaultServer2.cancelRegistration();

    String path = "/v2/ping";
    URI uri = new URI(resolveURI(Constants.Router.GATEWAY_DISCOVERY_NAME, path));
    Socket socket = getSocketFactory().createSocket(uri.getHost(), uri.getPort());
    PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
    InputStream inputStream = socket.getInputStream();

    // make a request
    String firstLine = makeRequest(uri, out, inputStream);
    Assert.assertEquals("HTTP/1.1 200 OK\r", firstLine);

    // sleep for 500 ms below the configured idle timeout; the connection on server side should not get closed by then
    TimeUnit.MILLISECONDS.sleep(TimeUnit.SECONDS.toMillis(CONNECTION_IDLE_TIMEOUT_SECS) - 500);
    firstLine = makeRequest(uri, out, inputStream);
    Assert.assertEquals("HTTP/1.1 200 OK\r", firstLine);

    // sleep for 500 ms over the configured idle timeout; the connection on server side should get closed by then
    TimeUnit.MILLISECONDS.sleep(TimeUnit.SECONDS.toMillis(CONNECTION_IDLE_TIMEOUT_SECS) + 500);
    // Due to timeout the client connection will be closed, and hence this request should not go to the server
    makeRequest(uri, out, inputStream);//w  w w .j  av a 2  s .c  o  m

    // assert that the connection is closed on the server side
    Assert.assertEquals(2, defaultServer1.getNumRequests() + defaultServer2.getNumRequests());
    Assert.assertEquals(1, defaultServer1.getNumConnectionsOpened() + defaultServer2.getNumConnectionsOpened());
    Assert.assertEquals(1, defaultServer1.getNumConnectionsClosed() + defaultServer2.getNumConnectionsClosed());
}

From source file:com.supernovapps.audio.jstreamsourcer.IcecastTest.java

@Test
public void testOnSourcerListenerDisconnect() throws IOException {
    Socket sockMock = EasyMock.createNiceMock(Socket.class);

    OnSourcerListener listener = new OnSourcerListener() {

        @Override/* w  w w  .j  ava  2 s.c  om*/
        public void onError(String string) {
        }

        @Override
        public void onDisconnected(boolean connected) {
            onConnected = true;
        }

        @Override
        public void onConnected() {
        }
    };

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ByteArrayInputStream in = new ByteArrayInputStream(new String("HTTP OK").getBytes());

    EasyMock.expect(sockMock.getOutputStream()).andReturn(out);
    EasyMock.expect(sockMock.getInputStream()).andReturn(in);
    EasyMock.replay(sockMock);

    icecast.setOnSourcerListener(listener);

    boolean started = icecast.start(sockMock);
    Assert.assertTrue(started);

    boolean stopped = icecast.stop();
    Assert.assertTrue(stopped);

    Assert.assertTrue(onConnected);
}

From source file:com.example.gemswin.screancasttest.SenderAsyncTask.java

@Override
protected Void doInBackground(Void... params) {

    Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
    //String portget = MainActivity.portString;

    List<NameValuePair> params1 = new ArrayList<NameValuePair>();
    params1.add(new BasicNameValuePair("class", pref.getclass()));

    String url = "http://176.32.230.250/anshuli.com/ScreenCast/getIPs.php";

    json = jparser.makeHttpRequest(url, "POST", params1);

    try {//from w  w  w  .j  av a 2s .  c o m
        // Checking for SUCCESS TAG

        //forjson.clear();

        String heading = "";
        String message = "";
        //Toast.makeText(MainActivity.this, (CharSequence) json, 1).show();
        ipArray = new ArrayList<String>();

        JSONArray account = json.getJSONArray("IPs");
        for (int i = 0; i < account.length(); i++) {
            json = account.getJSONObject(i);

            String IpString = json.getString("IP");

            if (!IpString.equals("0.0.0.0"))
                ipArray.add(IpString);
            // forjson.add(Roll+"-"+ NAME);
            //categories_description.add(description);

        }

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

    int port = Integer.parseInt(pref.getSerialNo());

    java.net.Socket socket = null;

    //   ip.add("192.168.15.105");
    //   ip.add("192.168.15.103");
    int len = ipArray.size();
    /*   try {
    for(int i=0;i<ip.size();i++)
    socket[i] = new java.net.Socket(ip.get(i),port);
       //   socket1 = new java.net.Socket("192.168.15.105", port);
     // connect to server
       } catch (IOException e) {
    e.printStackTrace();
       }*/
    dataOut = new DataOutputStream[len];
    try {
        for (int i = 0; i < len; i++) {
            socket = new java.net.Socket(ipArray.get(i), port);
            dataOut[i] = new DataOutputStream(socket.getOutputStream());
        }

        //   dataOut1 = new DataOutputStream(socket1.getOutputStream());
    } catch (IOException e) {
        e.printStackTrace();
    }

    while (!isCancelled()) {

        VideoChunk chunk = null;
        try {
            //Log.d(LOG_TAG, "waiting for data to send");
            chunk = mVideoChunks.takeLast();
            //Log.d(LOG_TAG,"got data. writing to socket");
            int length = chunk.getData().length;
            for (int i = 0; i < ipArray.size(); i++) {

                dataOut[i].writeInt(length);
                dataOut[i].writeInt(chunk.getFlags());
                dataOut[i].writeLong(chunk.getTimeUs());
                dataOut[i].write(chunk.getData());
                dataOut[i].flush();

            }
        } catch (InterruptedException e) {
            e.printStackTrace();
            continue;
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    return null;
}