lundi 13 février 2017

InputStream blocks on remote host but works well on localhost

Vote count: 0

I have non-blocking socketchannel on server side and socket on client side. The server sends a message like "hello" then some characters like "aaatttaagggh" then length of a file and then the file itself. My client receives all data while both server and client are on localhost. But when my client runs on different computer it just receives "hello", "aaatttaagggh" and length of the file and blocks on receiving the file. What might be the cause of this problem? I turned off firewall and antivirus by the way

server:

Integer myInt = (Integer)handle.attachment();

        if (myInt == null) {

            System.out.println("Step one");
            int myJob = jobFacade.isAnyJob(socketChannel, Long.parseLong(properties.getProperty("deadline")));

            if (myJob > 0) {
                ByteBuffer byteBuffer = ByteBuffer.wrap(("available" + "%").getBytes("UTF-8"));
                socketChannel.write(byteBuffer);
                byteBuffer.clear();
                myInt = myJob;
                socketChannel.register(
                        demultiplexer, SelectionKey.OP_WRITE, myInt);
            }else if (myJob == -2){
                ByteBuffer byteBuffer = ByteBuffer.wrap(("pending" + "%").getBytes("UTF-8"));
                socketChannel.write(byteBuffer);
                byteBuffer.clear();
                myInt = null;
                Thread.sleep(5000);
                socketChannel.register(
                        demultiplexer, SelectionKey.OP_WRITE, myInt);
            }else if (myJob == -1){
                ByteBuffer byteBuffer = ByteBuffer.wrap(("unavailable" + "%").getBytes("UTF-8"));
                socketChannel.write(byteBuffer);
                byteBuffer.clear();
                socketChannel.close();
            }

        }else if (myInt > 0){

            System.out.println("Step two");

            long startR = jobFacade.findByID(myInt);
            byte[] b1 = new byte[Integer.parseInt(properties.getProperty("workUnit"))];

            try {

                RandomAccessFile randomAccessFile = new RandomAccessFile(properties.getProperty("textPath"), "r");
                randomAccessFile.seek(startR);
                randomAccessFile.read(b1);
                String str = new String(b1);
                ByteBuffer inputBuffer = ByteBuffer.wrap((str+"%").getBytes("UTF-8"));
                socketChannel.write(inputBuffer);
                inputBuffer.clear();

            }catch (Exception e){}

            myInt=-3;

            socketChannel.register(
                    demultiplexer, SelectionKey.OP_WRITE, myInt);


        }else if (myInt == -3){

//
            System.out.println("Step three");

            File  algorithmFile = new File(properties.getProperty("algorithmPath"));
            long fileLength = algorithmFile.length();
            String length = Long.toString(fileLength);
            ByteBuffer byteBuffer = ByteBuffer.wrap((length+"%").getBytes("UTF-8"));
            socketChannel.write(byteBuffer);
            byteBuffer.clear();

            myInt=-4;

            socketChannel.register(
                    demultiplexer, SelectionKey.OP_WRITE, myInt);

        }else if (myInt == -4){

            System.out.println("Step four");


            RandomAccessFile rf = null;
            int con = 0;
            try {
                File file = new File("./Action.class");
                rf = new RandomAccessFile(file, "r");
                FileChannel fChannel = rf.getChannel();
                ByteBuffer buffer = ByteBuffer.allocate(50);
                while (fChannel.read(buffer) > 0){
                    buffer.flip();
                    con += socketChannel.write(buffer);
                    buffer.clear();
                }
                System.out.println("End of file reached . . ." + con);
                fChannel.close();

            }catch (FileNotFoundException fe){
                fe.printStackTrace();
            }catch (IOException io){
                io.printStackTrace();
            }

            myInt=-5;
            socketChannel.register(
                    demultiplexer, SelectionKey.OP_WRITE, myInt);


        }else if (myInt == -5){
            System.out.println("Step five");

            ByteBuffer byteBuffer = ByteBuffer.wrap(("Start"+"%").getBytes("UTF-8"));
            socketChannel.write(byteBuffer);
            byteBuffer.clear();

            myInt = jobFacade.changeStartTime(socketChannel.socket().getInetAddress().toString());

            socketChannel.register(
                    demultiplexer, SelectionKey.OP_READ, myInt);
        }

client:

OutputStream os = clientSocket.getOutputStream();
        OutputStreamWriter osw = new OutputStreamWriter(os);
        BufferedWriter outToServer = new BufferedWriter(osw);

        InputStream is = clientSocket.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is);
        InputStreamReader isr = new InputStreamReader(bis);
        BufferedReader inFromServer = new BufferedReader(isr);


        while (true) {

            int ch;
            while ((ch = inFromServer.read()) != 37) {
                availability = availability + (char) ch;
            }
            System.out.println("Response from Server : " + availability);

            if (availability.equals("available")) {
                availability = "";
//      ==================================================================

                while ((ch = inFromServer.read()) != 37) {
                    myJob = myJob + (char) ch;
                }
                System.out.println(myJob);

//      ==================================================================

                while ((ch = inFromServer.read()) != 37) {
                    algorithmSize = algorithmSize + (char) ch;
                }
                int alSize = Integer.parseInt(algorithmSize);
                System.out.println(algorithmSize);
                algorithmSize = "";

                byte[] bytes = new byte[alSize];
                int con = 0;
                int index;
                for (index = 0; (index < alSize) && ((con = bis.read(bytes, index, alSize-index)) >0) ; index += con);
//                int con = bis.read(bytes, 0, alSize);
                System.out.println("my con : " + index);
                MyClassLoader myClassLoader = new MyClassLoader(bytes);
                clazz = myClassLoader.findClass(null);

                while ((ch = inFromServer.read()) != 37) {
                    start = start + (char) ch;
                }
                System.out.println("Can I start? " + start);
                start = "";

Furthermore, I found some links here that blocks on read inputStram in both estate (remote and local). But my case is so strange!!!!

asked 10 secs ago

Let's block ads! (Why?)



InputStream blocks on remote host but works well on localhost

Aucun commentaire:

Enregistrer un commentaire