List of usage examples for java.net Socket getOutputStream
public OutputStream getOutputStream() throws IOException
From source file:com.googlecode.jmxtrans.model.output.OpenTSDBWriterTests.java
@Test public void socketInvalidatedWhenError() throws Exception { GenericKeyedObjectPool<InetSocketAddress, Socket> pool = Mockito.mock(GenericKeyedObjectPool.class); Socket socket = Mockito.mock(Socket.class); Mockito.when(pool.borrowObject(Matchers.any(InetSocketAddress.class))).thenReturn(socket); UnflushableByteArrayOutputStream out = new UnflushableByteArrayOutputStream(); Mockito.when(socket.getOutputStream()).thenReturn(out); OpenTSDBWriter writer = OpenTSDBWriter.builder().setHost("localhost").setPort(4243).build(); writer.setPool(pool);/*from w w w .j a v a2 s .c o m*/ writer.doWrite(dummyServer(), dummyQuery(), dummyResults()); Mockito.verify(pool).invalidateObject(Matchers.any(InetSocketAddress.class), Matchers.eq(socket)); Mockito.verify(pool, Mockito.never()).returnObject(Matchers.any(InetSocketAddress.class), Matchers.eq(socket)); }
From source file:com.l2jfree.network.legacy.NetworkThread.java
protected final void initConnection(Socket con) throws IOException { _connection = con;/*from w ww. j av a 2s . c om*/ _connectionIp = con.getInetAddress().getHostAddress(); _in = new BufferedInputStream(con.getInputStream()); _out = new BufferedOutputStream(con.getOutputStream()); _blowfish = new NewCipher("_;v.]05-31!|+-%xT!^[$\00"); }
From source file:com.l2jfree.network.NetworkThread.java
protected final void initConnection(Socket con) throws IOException { _connection = con;// w w w . j a v a 2 s . c om _connectionIp = con.getInetAddress().getHostAddress(); _in = new BufferedInputStream(con.getInputStream()); _out = new BufferedOutputStream(con.getOutputStream()); _blowfish = new NewCrypt("_;v.]05-31!|+-%xT!^[$\00"); }
From source file:br.com.i9torpedos.model.service.serverSocket.DSSocketSIM2.java
@Override public void run() { try {/*from w w w. ja va 2s .co m*/ while (true) { Socket socket = server.accept(); log.info("Cliente conectado " + socket.getInetAddress()); try { PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true); printWriter.println(new Date().toString() + "Teste Servidor"); log.info("Sinal de autenticao enviado para o Cliente " + new Date().toString()); //Faz verificao no fluxo de entrada do Objeto ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream()); try { //faz a leitura do Objeto de entrada Object readObject = objectInputStream.readObject(); if (readObject instanceof SendSMSMessage) { try { SendSMSMessage smsMessage = (SendSMSMessage) readObject; // Thread.sleep(random.nextInt(10000)); ponte.set(smsMessage); new Thread(new ConsumerSendSMSMessage(ponte), "PONTE_ASYNC_DSSOCKETSIM2").start(); listaSMS.add(smsMessage); objectInputStream.close(); socket.close(); if (listaSMS.size() > 0 && !listaSMS.isEmpty()) { DServiceModem2 md2 = new DServiceModem2(listaSMS, 10000); new Thread(md2, "MODEM 2").start(); listaSMS.clear(); } } catch (InterruptedException ex) { Logger.getLogger(DSSocketSIM2.class.getName()).log(Level.SEVERE, null, ex); } } } catch (ClassNotFoundException ex) { Logger.getLogger(DSSocketSIM2.class.getName()).log(Level.SEVERE, null, ex); } } catch (IOException ex) { Logger.getLogger(DSSocketSIM2.class.getName()).log(Level.SEVERE, null, ex); } finally { try { socket.close(); } catch (IOException ex) { Logger.getLogger(DSSocketSIM2.class.getName()).log(Level.SEVERE, null, ex); } } } } catch (IOException ex) { Logger.getLogger(DSSocketSIM2.class.getName()).log(Level.SEVERE, null, ex); } finally { try { server.close(); } catch (IOException ex) { Logger.getLogger(DSSocketSIM2.class.getName()).log(Level.SEVERE, null, ex); } } }
From source file:com.byteatebit.nbserver.simple.TestSimpleNbServer.java
@Test public void testMultipleMessages() throws IOException { SimpleNbServer simpleNbServer = SimpleNbServer.Builder.builder() .withConfig(/*from w ww . ja va 2s .com*/ SimpleNbServerConfig.builder().withListenAddress("localhost").withListenPort(1111).build()) .withConnectorFactory(TcpConnectorFactory.Builder.builder() .withConnectedSocketTask(new TcpConnectTestHandler()).build()) .build(); simpleNbServer.start(); String message1 = "this is message1"; String message2 = "this is message2"; try { Socket socket = new Socket("localhost", 1111); OutputStream out = socket.getOutputStream(); InputStream in = socket.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8)); // message1 out.write((message1 + '\n' + message2 + '\n').getBytes(StandardCharsets.UTF_8)); String messageRead1 = reader.readLine(); Assert.assertEquals(message1, messageRead1); String messageRead2 = reader.readLine(); Assert.assertEquals(message2, messageRead2); // quit out.write("quit\n".getBytes(StandardCharsets.UTF_8)); String finalMessage = reader.readLine(); Assert.assertNull(finalMessage); } finally { simpleNbServer.shutdown(); } }
From source file:com.byteatebit.nbserver.simple.TestSimpleNbServer.java
@Test public void testReadTimeout() throws IOException, InterruptedException { SimpleNbServer simpleNbServer = SimpleNbServer.Builder.builder() .withConfig(SimpleNbServerConfig.builder().withListenAddress("localhost").withListenPort(1111) .withNbServiceConfig(NbServiceConfig.Builder.builder().withIoTaskTimeoutMs(10l) .withIoTaskTimeoutCheckPeriodMs(2l).withSelectorTimeoutMs(2l).build()) .build())/* w w w .ja v a 2 s .c o m*/ .withConnectorFactory(TcpConnectorFactory.Builder.builder() .withConnectedSocketTask(new TcpConnectTestHandler()).build()) .build(); simpleNbServer.start(); String message1 = "this is message1"; try { Socket socket = new Socket("localhost", 1111); OutputStream out = socket.getOutputStream(); InputStream in = socket.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8)); out.write((message1 + '\n').getBytes(StandardCharsets.UTF_8)); reader.readLine(); // message1 Thread.sleep(100); String nextLine = null; try { out.write((message1 + '\n').getBytes(StandardCharsets.UTF_8)); nextLine = reader.readLine(); } catch (SocketException e) { } Assert.assertNull(nextLine); } finally { simpleNbServer.shutdown(); } }
From source file:com.talis.platform.sequencing.zookeeper.ZkTestHelper.java
public boolean waitForServerUp(String hp, long timeout) { long start = System.currentTimeMillis(); String split[] = hp.split(":"); String host = split[0];//from ww w. j a v a 2 s .c om int port = Integer.parseInt(split[1]); while (true) { try { Socket sock = new Socket(host, port); BufferedReader reader = null; try { OutputStream outstream = sock.getOutputStream(); outstream.write("stat".getBytes()); outstream.flush(); reader = new BufferedReader(new InputStreamReader(sock.getInputStream())); String line = reader.readLine(); if (line != null && line.startsWith("Zookeeper version:")) { return true; } } finally { sock.close(); if (reader != null) { reader.close(); } } } catch (IOException e) { // ignore as this is expected LOG.info("server " + hp + " not up " + e); } if (System.currentTimeMillis() > start + timeout) { break; } try { Thread.sleep(250); } catch (InterruptedException e) { // ignore } } return false; }
From source file:com.twinflag.coofiletouch.AuthorityChecking.java
private void fetchLicenseFromInternet() { Log.i(TAG, "===fetchLicenseFromInternet !"); new Thread() { public void run() { try { JSONObject json = new JSONObject(); try { int readLength = 0; int sendSize = 0; json.put("command", "checkLicense"); json.put("hardinfo", DeviceUtil.getDeviceInfo()); Socket socket = new Socket("192.168.13.95", 60000); // ? OutputStream os = socket.getOutputStream(); String jsonStr = json.toString(); byte[] buffer = jsonStr.getBytes("UTF-8"); sendSize = buffer.length; byte[] array = new byte[4]; array[3] = (byte) (0xff & sendSize); array[2] = (byte) ((0xff00 & sendSize) >> 8); array[1] = (byte) ((0xff0000 & sendSize) >> 16); array[0] = (byte) (0xff000000 & sendSize >> 24); os.write(array);/*from w w w . ja va 2 s .co m*/ os.flush(); os.write(buffer); os.flush(); // ; InputStream inputStream = socket.getInputStream(); byte[] length = new byte[4]; byte[] temp = new byte[4]; byte oneByte; try { inputStream.read(length); } catch (IOException e1) { e1.printStackTrace(); } for (int i = 0; i < 4; i++) { temp[3 - i] = length[i]; } for (int j = 0; j < 4; j++) { oneByte = temp[j]; readLength += (oneByte & 0xFF) << (8 * j); } System.out.println(readLength + "?"); if (readLength == 0) { System.out.println("readLength == 0"); Message msg = Message.obtain(); msg.what = MSG_GET_LINCENSE_FROM_INTERNET; msg.obj = null; mHandler.sendMessageDelayed(msg, 100); } // ??0 else { System.out.println("readLength == " + readLength); buffer = new byte[readLength]; try { String receivedContent = null; inputStream.read(buffer, 0, readLength); receivedContent = new String(buffer, "UTF-8"); Message msg = Message.obtain(); msg.what = MSG_GET_LINCENSE_FROM_INTERNET; msg.obj = receivedContent; mHandler.sendMessageDelayed(msg, 100); } catch (IOException e) { e.printStackTrace(); } } socket.close(); } catch (JSONException exception) { exception.printStackTrace(); } } catch (IOException e) { e.printStackTrace(); } } }.start(); }
From source file:com.byteatebit.nbserver.simple.TestSimpleNbServer.java
@Test public void testEchoServer() throws IOException { SimpleNbServer simpleNbServer = SimpleNbServer.Builder.builder() .withConfig(/*w w w .j a v a2 s . c o m*/ SimpleNbServerConfig.builder().withListenAddress("localhost").withListenPort(1111).build()) .withConnectorFactory(TcpConnectorFactory.Builder.builder() .withConnectedSocketTask(new TcpConnectTestHandler()).build()) .build(); simpleNbServer.start(); String message1 = "this is message1"; String message2 = "this is message2"; try { Socket socket = new Socket("localhost", 1111); OutputStream out = socket.getOutputStream(); InputStream in = socket.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8)); // message1 out.write((message1 + '\n').getBytes(StandardCharsets.UTF_8)); String messageRead1 = reader.readLine(); Assert.assertEquals(message1, messageRead1); // message2 out.write((message2 + '\n').getBytes(StandardCharsets.UTF_8)); String messageRead2 = reader.readLine(); Assert.assertEquals(message2, messageRead2); // quit out.write("quit\n".getBytes(StandardCharsets.UTF_8)); String finalMessage = reader.readLine(); Assert.assertNull(finalMessage); } finally { simpleNbServer.shutdown(); } }
From source file:com.citruspay.enquiry.gateway.AXISGatewayConnector.java
public String connect(String vpcHost, String queryParameters, EnquiryRequest request) { boolean useSSL = false; String fileName = ""; // determine if SSL encryption is being used if (vpcHost.substring(0, 8).equalsIgnoreCase("HTTPS://")) { useSSL = true;// w w w . j av a 2 s . co m // remove 'HTTPS://' from host URL vpcHost = vpcHost.substring(8); // get the filename from the last section of vpc_URL fileName = vpcHost.substring(vpcHost.lastIndexOf("/")); // get the IP address of the VPC machine vpcHost = vpcHost.substring(0, vpcHost.lastIndexOf("/")); } try { Socket socket = null; if (useSSL) { socket = getSocket(vpcHost, vpc_Port); // use next block of code if NOT using SSL encryption } else { socket = new Socket(vpcHost, vpc_Port); } OutputStream os = socket.getOutputStream(); InputStream is = socket.getInputStream(); os.write(prepareCompleteQueryString(fileName, queryParameters).getBytes()); String res = new String(readAll(is)); // check if a successful connection if (res.indexOf("200") < 0) { throw new IOException("Connection Refused - " + res); } if (res.indexOf("404 Not Found") > 0) { throw new IOException("File Not Found Error - " + res); } int resIndex = res.indexOf("\r\n\r\n"); String body = res.substring(resIndex + 4, res.length()); return body; } catch (IOException ioe) { } return StringUtils.EMPTY; }