List of usage examples for java.net Socket getInputStream
public InputStream getInputStream() throws IOException
From source file:com.l2jfree.network.legacy.NetworkThread.java
protected final void initConnection(Socket con) throws IOException { _connection = con;//from w w w .j a v a2 s . co m _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:hudson.plugins.chainreactorclient.ChainReactorInvalidServerException.java
public boolean connect(ChainReactorServer server) { try {// w ww .j a va2 s. com InetSocketAddress sockaddr = server.getSocketAddress(); Socket sock = new Socket(); sock.setSoTimeout(2000); sock.connect(sockaddr, 2000); BufferedReader rd = new BufferedReader(new InputStreamReader(sock.getInputStream())); BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream())); ensureValidServer(rd); logger.println("Connected to chain reactor server"); sendBuildInfo(wr); rd.close(); wr.close(); sock.close(); } catch (UnknownHostException uhe) { logError("Failed to resolve host: " + server.getUrl()); } catch (SocketTimeoutException ste) { logError("Time out while trying to connect to " + server.toString()); } catch (IOException ioe) { logError(ioe.getMessage()); } catch (ChainReactorInvalidServerException crise) { logError(crise.getMessage()); } return true; }
From source file:com.l2jfree.network.NetworkThread.java
protected final void initConnection(Socket con) throws IOException { _connection = con;//from w ww .j av a2 s.c o m _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:org.apache.ftpserver.socketfactory.FtpSocketFactoryTest.java
private void testCreateServerSocket(final FtpSocketFactory ftpSocketFactory, final int port) throws Exception, IOException { boolean freePort = false; try {// w w w. j a v a2 s . co m final ServerSocket testSocket = new ServerSocket(port, 100); freePort = testSocket.isBound(); testSocket.close(); } catch (Exception exc) { // ok freePort = true; } if (freePort) { new Thread() { public void run() { synchronized (this) { try { this.wait(1000); } catch (InterruptedException e) { e.printStackTrace(); fail(e.toString()); } } try { Socket socket = new Socket(); socket.connect(new InetSocketAddress("localhost", port)); socket.getInputStream(); socket.getOutputStream(); socket.close(); } catch (UnknownHostException e) { e.printStackTrace(); fail(e.toString()); } catch (IOException e) { e.printStackTrace(); fail(e.toString()); } } }.start(); ServerSocket serverSocket = ftpSocketFactory.createServerSocket(); assertNotNull(serverSocket); serverSocket.accept(); } }
From source file:com.googlecode.android_scripting.jsonrpc.JsonRpcServer.java
@Override protected void handleConnection(Socket socket) throws Exception { RpcReceiverManager receiverManager = mRpcReceiverManagerFactory.create(); BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()), 8192); PrintWriter writer = new PrintWriter(socket.getOutputStream(), true); boolean passedAuthentication = false; String data;/*from ww w . j av a2 s . co m*/ while ((data = reader.readLine()) != null) { Log.v("Received: " + data); JSONObject request = new JSONObject(data); int id = request.getInt("id"); String method = request.getString("method"); JSONArray params = request.getJSONArray("params"); // First RPC must be _authenticate if a handshake was specified. if (!passedAuthentication && mHandshake != null) { if (!checkHandshake(method, params)) { SecurityException exception = new SecurityException("Authentication failed!"); send(writer, JsonRpcResult.error(id, exception)); shutdown(); throw exception; } passedAuthentication = true; send(writer, JsonRpcResult.result(id, true)); continue; } MethodDescriptor rpc = receiverManager.getMethodDescriptor(method); if (rpc == null) { send(writer, JsonRpcResult.error(id, new RpcError("Unknown RPC."))); continue; } try { send(writer, JsonRpcResult.result(id, rpc.invoke(receiverManager, params))); } catch (Throwable t) { Log.e("Invocation error.", t); send(writer, JsonRpcResult.error(id, t)); } } }
From source file:gobblin.tunnel.ConnectProxyServer.java
@Override void handleClientSocket(Socket clientSocket) throws IOException { final InputStream clientToProxyIn = clientSocket.getInputStream(); BufferedReader clientToProxyReader = new BufferedReader(new InputStreamReader(clientToProxyIn)); final OutputStream clientToProxyOut = clientSocket.getOutputStream(); String line = clientToProxyReader.readLine(); String connectRequest = ""; while (line != null && isServerRunning()) { connectRequest += line + "\r\n"; if (connectRequest.endsWith("\r\n\r\n")) { break; }/* w w w . j a v a2s .c o m*/ line = clientToProxyReader.readLine(); } // connect to given host:port Matcher matcher = hostPortPattern.matcher(connectRequest); if (!matcher.find()) { try { sendConnectResponse("400 Bad Request", clientToProxyOut, null, 0); } finally { clientSocket.close(); stopServer(); } return; } String host = matcher.group(1); int port = Integer.decode(matcher.group(2)); // connect to server Socket serverSocket = new Socket(); try { serverSocket.connect(new InetSocketAddress(host, port)); addSocket(serverSocket); byte[] initialServerResponse = null; int nbytes = 0; if (mixServerAndProxyResponse) { // we want to mix the initial server response with the 200 OK initialServerResponse = new byte[64]; nbytes = serverSocket.getInputStream().read(initialServerResponse); } sendConnectResponse("200 OK", clientToProxyOut, initialServerResponse, nbytes); } catch (IOException e) { try { sendConnectResponse("404 Not Found", clientToProxyOut, null, 0); } finally { clientSocket.close(); stopServer(); } return; } final InputStream proxyToServerIn = serverSocket.getInputStream(); final OutputStream proxyToServerOut = serverSocket.getOutputStream(); _threads.add(new EasyThread() { @Override void runQuietly() throws Exception { try { IOUtils.copy(clientToProxyIn, proxyToServerOut); } catch (IOException e) { LOG.warn("Exception " + e.getMessage() + " on " + getServerSocketPort()); } } }.startThread()); try { if (nBytesToCloseSocketAfter > 0) { // Simulate proxy abruptly closing connection int leftToRead = nBytesToCloseSocketAfter; byte[] buffer = new byte[leftToRead + 256]; while (true) { int numRead = proxyToServerIn.read(buffer, 0, leftToRead); if (numRead < 0) { break; } clientToProxyOut.write(buffer, 0, numRead); clientToProxyOut.flush(); leftToRead -= numRead; if (leftToRead <= 0) { LOG.warn("Cutting connection after " + nBytesToCloseSocketAfter + " bytes"); break; } } } else { IOUtils.copy(proxyToServerIn, clientToProxyOut); } } catch (IOException e) { LOG.warn("Exception " + e.getMessage() + " on " + getServerSocketPort()); } clientSocket.close(); serverSocket.close(); }
From source file:com.rabbitmq.client.impl.SocketFrameHandler.java
/** * @param socket the socket to use/*from ww w .j a v a2 s. c o m*/ */ public SocketFrameHandler(Socket socket) throws IOException { _socket = socket; _inputStream = new DataInputStream(new BufferedInputStream(socket.getInputStream())); _outputStream = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream())); this.socketFrameHandler = this; }
From source file:disko.flow.analyzers.socket.SocketReceiver.java
private Message readMessage(ServerSocket serverSocket) { while (true) { Socket clientSocket = null; ObjectInputStream in = null; try {// w w w . ja va 2 s . com clientSocket = serverSocket.accept(); in = new ObjectInputStream(clientSocket.getInputStream()); Message message = null; try { message = (Message) in.readObject(); } catch (IOException e) { log.error("Error reading object.", e); continue; } catch (ClassNotFoundException e) { log.error("Class not found.", e); continue; } // while (in.read() != -1); // if (in.available() > 0) // { // System.err.println("OOPS, MORE THAT ON SOCKET HASN'T BEEN // READ: " + in.available()); // while (in.available() > 0) // in.read(); // } return message; } catch (SocketTimeoutException ex) { if (Thread.interrupted()) return null; } catch (InterruptedIOException e) // this doesn't really work, that's why we put an Socket timeout and we check for interruption { return null; } catch (IOException e) { log.error("Could not listen on port: " + port, e); } finally { if (in != null) try { in.close(); } catch (Throwable t) { log.error("While closing receiver input.", t); } if (clientSocket != null) try { clientSocket.close(); } catch (Throwable t) { log.error("While closing receiver socket.", t); } } } }
From source file:net.wimpi.modbus.io.ModbusTCPTransport.java
/** * Prepares the input and output streams of this * <tt>ModbusTCPTransport</tt> instance based on the given * socket./*from ww w. java2 s.c o m*/ * * @param socket the socket used for communications. * @throws IOException if an I/O related error occurs. */ private void prepareStreams(Socket socket) throws IOException { m_Input = new DataInputStream(new BufferedInputStream(socket.getInputStream())); m_Output = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream())); m_ByteIn = new BytesInputStream(Modbus.MAX_MESSAGE_LENGTH); }
From source file:jeeves.utils.EMail.java
/** Sends the message to the mail server *///from w w w. j ava 2s .c om public boolean send() throws IOException { Socket socket = new Socket(sMailServer, iPort); try { in = new BufferedReader(new InputStreamReader(new DataInputStream(socket.getInputStream()), Charset.forName(Jeeves.ENCODING))); out = new OutputStreamWriter(new DataOutputStream(socket.getOutputStream()), "ISO-8859-1"); if (lookMailServer()) if (sendData("2", "HELO " + InetAddress.getLocalHost().getHostName() + "\r\n")) if (sendData("2", "MAIL FROM: <" + sFrom + ">\r\n")) if (sendData("2", "RCPT TO: <" + sTo + ">\r\n")) if (sendData("354", "DATA\r\n")) if (sendData("2", buildContent())) if (sendData("2", "QUIT\r\n")) return true; sendData("2", "QUIT\r\n"); } finally { IOUtils.closeQuietly(socket); } return false; }