简体   繁体   中英

java socket connection blocking process

I'm working with two java processes that communicates using sockets.

From the server side, I use this to send info :

public void send(Serializable order)
{
    try
    {
        if (this.clientSocket != null && this.clientSocket.isBound() && this.clientSocket.isConnected())
        {
            String json = "";
            json = mapper.writeValueAsString(order);
            log.info("sending to client : " + order.getClass());

            json = convertToUTF8(json);

            this.output.write(json + "\n");
            this.output.flush();
            log.info("Message sent to client");
        }
        else
        {
            log.info("no client connected");
        }
    }
    catch (Exception e)
    {
        log.fatal("Exception while trying to send message to client : " + e.getMessage(), e);
    }
}

output here is :

private BufferedWriter output;

And on the client side, I try to read data like that :

while (this.running)
        {   
            try
            {
                String msg = in.readLine();

                log.debug("MSG VALUE IS : |"+msg+"|\n\n**********\n");

                if ("".equalsIgnoreCase(msg) || msg == null)
                {
                    break;
                }

in here is a :

private BufferedReader in;

The problem here is after some time, the server process is blocked, ans if I run the netstat command, I can see that the recv-Q and send-Q values are not at 0.

But I can't reproduce myself the situation, so I wonder what is producing this situation, is ther a way to handle that or do I have to change the way to read data ?

Thanks in advance.

Generally if you are using blocking IO operations from server side code, each client needs to have it's own worker thread that is responsible for sending to that client.

The other option is to use NIO (or some framework that uses NIO) to allow your server to multiplex things.

To avoid blocking you should use thead-per-request.

Very brief example :

public class App {


    public static void main(String[] args) throws IOException {

        ServerSocket serverSocket = null; // consider it is properly initialized

        while (true /* stub health condition */) {

            Socket clientSocket = serverSocket.accept(); // the blocking call
            final Worker worker = new Worker(clientSocket);

            Thread workerThread = new Thread() {

                @Override
                public void run() {
                    // handle request here
                    worker.send(new Serializable(){} /* stub */);
                }

            };

            workerThread.start();
        }

    }


}

class Worker {

    private Socket clientSocket;

    Worker (Socket clientSocket) {
        this.clientSocket = clientSocket;
    }

    public void send(Serializable order) {
        // logic
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM