List of usage examples for java.net Socket getInputStream
public InputStream getInputStream() throws IOException
From source file:com.myJava.file.driver.remote.ftp.SecuredSocketFactory.java
private void readReply(Socket socket) throws IOException { // Read response BufferedReader in = new BufferedReader( new InputStreamReader(socket.getInputStream(), client.getControlEncoding())); String line = in.readLine();//from w ww.j a v a 2 s . c om StringBuffer sb = new StringBuffer(line); int length = line.length(); if (length > 3 && line.charAt(3) == '-') { do { line = in.readLine(); sb.append(" / ").append(line); if (line == null) { throw new FTPConnectionClosedException("Connection closed without indication."); } } while (!(line.length() >= 4 && line.charAt(3) != '-' && Character.isDigit(line.charAt(0)))); } Logger.defaultLogger().info("Received FTP server response : " + sb.toString()); }
From source file:org.apache.isis.objectstore.nosql.db.file.FileServerDb.java
private ClientConnection getConnection() { try {/*www. j a v a 2s .c o m*/ final Socket socket; socket = new Socket(host, port); socket.setSoTimeout(timeout); return new ClientConnection(socket.getInputStream(), socket.getOutputStream()); } catch (final UnknownHostException e) { throw new NoSqlStoreException("Unknow host " + host, e); } catch (final IOException e) { throw new NoSqlStoreException("Failed to connect to " + host + ":" + port, e); } }
From source file:haxball.networking.ConnectionHandler.java
public ConnectionHandler(@NonNull Socket socket, @NonNull Dimension fieldSize, @NonNull Goal goals[], byte id) { this.socket = socket; this.fieldSize = fieldSize; this.id = id; try {//from w w w . ja va 2 s . c o m in = socket.getInputStream(); out = socket.getOutputStream(); // send dimension size out.write(serializeDimension(getFieldSize())); out.flush(); out.write(id); out.write(goals.length); for (Goal g : goals) { out.write(serializeGoal(g)); } // read name from client byte b[] = new byte[in.read()]; if (in.read(b) != b.length) { throw new IOException(); } name = new String(b, StandardCharsets.UTF_8); System.out.println("Connected client " + getSocket() + " as name " + name); // create player player = new Player(getId(), getName(), fieldSize); player.position = new Vector2D(100, 100); } catch (Exception ioe) { ioe.printStackTrace(); try { socket.close(); } catch (Exception e) { } } }
From source file:org.ardverk.daap.bio.DaapConnectionBIO.java
public DaapConnectionBIO(DaapServerBIO server, Socket socket) throws IOException { super(server); this.socket = socket; in = new BufferedInputStream(socket.getInputStream()); out = socket.getOutputStream();//from www .j ava 2 s . com running = true; }
From source file:com.fuzhepan.arpc.client.ProxyHandler.java
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { log.debug("invoke was called!"); if (method.getName().equals("toString")) { return "toString method was called"; }//from ww w. j a v a 2 s . c o m RpcContext rpcContext = new RpcContext(interfaceName, method.getName(), method.getParameterTypes(), args); //get service info and load balance List<HostPortPair> serviceList = RpcConfig.getServiceList(serviceName); if (serviceList == null || serviceList.size() == 0) throw new ClassNotFoundException("not find service : " + serviceName); int index = requestCount.get() % serviceList.size(); if (requestCount.get() > 100) requestCount.set(0); else requestCount.getAndIncrement(); HostPortPair hostPort = serviceList.get(index); Socket socket = new Socket(hostPort.host, hostPort.port); ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream()); objectOutputStream.writeObject(rpcContext); objectOutputStream.flush(); ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream()); Object response = objectInputStream.readObject(); objectInputStream.close(); objectOutputStream.close(); socket.close(); Class methodReturnType = method.getReturnType(); return methodReturnType.cast(response); }
From source file:org.apache.flink.streaming.api.functions.sink.SocketClientSinkTest.java
@Test public void testSocketSink() throws Exception { final ServerSocket server = new ServerSocket(0); final int port = server.getLocalPort(); final AtomicReference<Throwable> error = new AtomicReference<Throwable>(); Thread sinkRunner = new Thread("Test sink runner") { @Override/* w w w . j a v a 2s. c o m*/ public void run() { try { SocketClientSink<String> simpleSink = new SocketClientSink<>(host, port, simpleSchema, 0); simpleSink.open(new Configuration()); simpleSink.invoke(TEST_MESSAGE + '\n'); simpleSink.close(); } catch (Throwable t) { error.set(t); } } }; sinkRunner.start(); Socket sk = server.accept(); BufferedReader rdr = new BufferedReader(new InputStreamReader(sk.getInputStream())); String value = rdr.readLine(); sinkRunner.join(); server.close(); if (error.get() != null) { Throwable t = error.get(); t.printStackTrace(); fail("Error in spawned thread: " + t.getMessage()); } assertEquals(TEST_MESSAGE, value); }
From source file:org.apache.flink.streaming.api.functions.sink.SocketClientSinkTest.java
@Test public void testSinkAutoFlush() throws Exception { final ServerSocket server = new ServerSocket(0); final int port = server.getLocalPort(); final SocketClientSink<String> simpleSink = new SocketClientSink<>(host, port, simpleSchema, 0, true); simpleSink.open(new Configuration()); final AtomicReference<Throwable> error = new AtomicReference<Throwable>(); Thread sinkRunner = new Thread("Test sink runner") { @Override//from w w w .ja v a2 s. c o m public void run() { try { // need two messages here: send a fin to cancel the client state:FIN_WAIT_2 while the server is CLOSE_WAIT simpleSink.invoke(TEST_MESSAGE + '\n'); } catch (Throwable t) { error.set(t); } } }; sinkRunner.start(); Socket sk = server.accept(); BufferedReader rdr = new BufferedReader(new InputStreamReader(sk.getInputStream())); String value = rdr.readLine(); sinkRunner.join(); simpleSink.close(); server.close(); if (error.get() != null) { Throwable t = error.get(); t.printStackTrace(); fail("Error in spawned thread: " + t.getMessage()); } assertEquals(TEST_MESSAGE, value); }
From source file:client.communication.SocketClient.java
/** * Envia a mensagem para o servidor e retorna a resposta * // ww w . j a v a 2s . com * @param message * @return */ public String sendMessage(String message) { Socket socket = null; PrintStream stream = null; try { socket = new Socket(serverAddress, serverPort); stream = new PrintStream(socket.getOutputStream()); // Envia requisiao stream.println(message); // L resposta socket.getInputStream().read(response); } catch (IOException e) { System.out.println("Problem connecting server!"); } finally { try { // Fecha stream if (stream != null) stream.close(); if (socket != null) socket.close(); } catch (IOException e) { System.err.println("Problem closing socket: " + e.getMessage()); } } // Decodifica resposta em base 64 String _reply = new String(Base64.decodeBase64(response)); // Remove o espao nas extremidades da string de resposta return _reply.trim(); }
From source file:com.yahoo.pulsar.testclient.LoadSimulationClient.java
private void handle(final Socket socket) throws Exception { final DataInputStream inputStream = new DataInputStream(socket.getInputStream()); int command;//from w w w .j a v a 2 s. c om while ((command = inputStream.read()) != -1) { handle((byte) command, inputStream, new DataOutputStream(socket.getOutputStream())); } }
From source file:com.byteatebit.nbserver.simple.TestSimpleNbServer.java
protected String sendMessage(String message) throws IOException { LOG.info("sending message '" + message + "'"); 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((message + '\n').getBytes(StandardCharsets.UTF_8)); String response = reader.readLine(); LOG.info("recieved response '" + response + "'"); socket.close();/*w w w. j a v a2 s. co m*/ return response; }