Skip to main content

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

Hi Bin,

Just play a little with the client “timeout” e.g.:

CoapClient client = new CoapClient(uri);
client.setTimeout(15000);

the proper value depends on the size and transfer speed. Just try it. 
If a timeout occurs, the response is null and therefore

CoapResponse response = client.post(data, MediaTypeRegistry.APPLICATION_OCTET_STREAM);
System.out.println("response: " + response.getResponseText());

The print fails with a java.lang.NullPointerException.

Using an async call doesn't use that timeout for the complete response, so it works also.

Mit freundlichen Grüßen / Best regards

Achim Kraus

Bosch Software Innovations GmbH
Communications (INST/ECS4)
Stuttgarter Straße 130
71332 Waiblingen
GERMANY
www.bosch-si.de
http://www.blog.bosch-si.com/ 

Registered office: Berlin, Register court: Amtsgericht Charlottenburg, HRB 148411 B
Executives: Dr.-Ing. Rainer Kallenbach; Michael Hahn


Von: cf-dev-bounces@xxxxxxxxxxx [mailto:cf-dev-bounces@xxxxxxxxxxx] Im Auftrag von bin ma
Gesendet: Donnerstag, 23. März 2017 22:14
An: Californium (Cf) developer discussions <cf-dev@xxxxxxxxxxx>
Betreff: [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