Note that there are some explanatory texts on larger screens.

plurals
  1. POjava socket InputStream hangs/blocks on both client and server
    primarykey
    data
    text
    <p><strong>Hi, all</strong></p> <p>I am recently working on a tiny program aimed to close the browser remotely. The basic procedures are as follow:<br> Server side:<br></p> <ol> <li>Create a SocketServer to listen to some certain port.</li> <li>Accept a connection and create a corresponding socket object</li> <li>Read the InputStream from the created socket(blocked on this operation) <br></li> </ol> <hr> <p>Client side:<br></p> <ol> <li>Create a socket object to establish a connection with the server.</li> <li>Send the command to close the browser on Server side by writing bytes to OutputStream.</li> <li>Read the feedback from the server by using read() on InputStream of the socket(blocked on this operation)</li> </ol> <hr> <p>Code Below:<br><br> <strong>Server.java</strong></p> <pre><code>package socket; import java.io.IOException; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.ServerSocket; import java.net.Socket; import java.util.Enumeration; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class Server { private static ExecutorService es = Executors.newFixedThreadPool(5); public static void main(String[] args) throws IOException { InetAddress targetAddress = null; NetworkInterface ni = NetworkInterface.getByName("eth2"); System.out.println(ni); Enumeration&lt;InetAddress&gt; inetAddresses = ni.getInetAddresses(); while(inetAddresses.hasMoreElements()) { InetAddress inetAddress = inetAddresses.nextElement(); if(inetAddress.toString().startsWith("/10")) { targetAddress = inetAddress; break; } } ServerSocket sSocket = new ServerSocket(11111, 0, targetAddress); while(true) { System.out.println("Server is running..."); Socket client = sSocket.accept(); System.out.println("Client at: " + client.getRemoteSocketAddress()); es.execute(new ClientRequest(client)); } } } </code></pre> <p><br> <strong>ClientRequest.java</strong></p> <pre><code>package socket; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; public class ClientRequest implements Runnable { private Socket client; public ClientRequest(Socket client) { this.client = client; } @Override public void run() { try { System.out.println("Handled by: " + Thread.currentThread().getName()); // get input/output streams for client socket InputStream cis = client.getInputStream(); OutputStream cos = client.getOutputStream(); // buffer size : 1024 ? byte[] buffer = new byte[1024]; int recvSize; int totalRecvSize = 0; while(-1 != (recvSize = cis.read(buffer, totalRecvSize, 1024 - totalRecvSize))) { totalRecvSize += recvSize; } String command = new String(buffer, "utf-8"); System.out.println("Command from client: " + command); String commandNative = CommandMap.getNativeCommand(command.trim()); if(null != commandNative) { Process np = Runtime.getRuntime().exec(commandNative); InputStream is = np.getInputStream(); byte[] bufferProcess = new byte[1024]; int bytesRead; int totalBytesRead = 0; while(-1 != (bytesRead = is.read(bufferProcess, totalBytesRead, 1024 - totalBytesRead))) { totalBytesRead += bytesRead; } // give feed back of process output cos.write(bufferProcess); // close process input stream is.close(); } } catch (IOException e) { e.printStackTrace(); } finally { if(null != client) { try { client.close(); } catch (IOException e) { e.printStackTrace(); } } } } </code></pre> <p>and finally, <strong>Client.java</strong></p> <pre><code>package socket; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; import java.net.UnknownHostException; import java.nio.charset.Charset; public class Client { private static final int BUF_SIZE = 1024; // feedback message size will not exceed 1024 bytes private static final byte[] BUFFER = new byte[BUF_SIZE]; public static void main(String[] args) throws UnknownHostException, IOException, InterruptedException { Socket socket = new Socket("10.117.37.176", 11111); System.out.println("Connected to Server..."); OutputStream os = socket.getOutputStream(); InputStream is = socket.getInputStream(); String command = "kill ie"; byte[] commandBytes = command.getBytes(Charset.forName("utf-8")); System.out.println("Send: " + command); os.write(commandBytes); System.out.println("After send: " + command); int totalRecv = 0; int recvSize; while(-1 != (recvSize = is.read(BUFFER, totalRecv, BUF_SIZE - totalRecv))) { totalRecv += recvSize; } String feedback = new String(BUFFER, "utf-8"); System.out.println("Feedback: " + feedback); socket.close(); } } </code></pre> <p>To reiterate the problems:<br></p> <ul> <li>The Server side can not read the command by calling read(buffer, offset, len) on InputStream object of the socket. It blocks.</li> <li>The Client side can not read the feedback by calling read(buffer, offset, len) on InputStream object of its socket. It blocks.</li> <li>But when I comment out the feedback reading operations in Client.java, both Server and Client work correctly.</li> </ul> <hr> <p>I am wondering what's the hidden causes in those codes ?<br> Hope someone can help me, thanks a lot !<br></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload