Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[cf-dev] Post Large File

Hello,


I am doing some experiments on Californium for a large project and I tried to post some files with blocking response. I tried files with 200K, 3M and it works fine. However it always fails on transferring file with 12M. Then I tried using asynchronized post and it works fine.


I was thinking this may be caused by the default blocking transfer timeout value. However the default value is 600000 milliseconds and the transfer only takes around 9000 milliseconds.


Can any one give me some clue? I posted my server and client program below.


Thanks,

Bin




TestServer.java


public class TestServer{


   public static void main(String[] args) {

       CoapServer server = new CoapServer();

       CoapResource res = new CoapResource("testImage") {

       @Override

        public void handlePOST(CoapExchange exchange) {

            try {

               byte[] data = exchange.getRequestPayload();

               exchange.respond("file received");

           }

           catch(Exception exc) {

               exc.printStackTrace();

          }

      }

     };

     server.add(res);

     server.start();

  }  

}




Client.java


public class Client {

     public static byte[] read(File file) {

          byte[] buffer = new byte[(int) file.length()];

          InputStream ios = null;

          try {

              ios = new FileInputStream(file);

              ios.read(buffer);

          }

          catch(Exception exc) {

              exc.printStackTrace();

               return null;

          }

          finally {

              try {

                  if(ios != null) {

                     ios.close();

                  }

            }

            catch(IOException exc) {

            }

         }

         return buffer;

      }

 


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

         try {

              URI uri = new URI("coap://localhost:5683/testImage");

              CoapClient client = new CoapClient(uri);

               File file = new File("c:\\tmp\\test.bin");

               byte[] data = read(file);

               CoapResponse response = client.post(data, MediaTypeRegistry.APPLICATION_OCTET_STREAM);

                System.out.println("response: " + response.getResponseText());

                // client.post(new CoapHandler() {

                // @Override

               // public void onLoad(CoapResponse response) {

               // String content = response.getResponseText();

               // System.out.println("asynchronized response: " + content);

               // }

              //

              // @Override

              // public void onError() {

             // System.err.println("asynchronized response: failed.");

            // }

            // }, data, MediaTypeRegistry.APPLICATION_OCTET_STREAM);

            // Thread.sleep(Integer.MAX_VALUE);

       }

       catch(Exception exc) {

            exc.printStackTrace();

        }

    }

}




Back to the top