Results 1 to 4 of 4
  1. #1
    sunil6639 is offline Member
    Join Date
    Aug 2014
    Posts
    1
    Rep Power
    0

    Default How to send binary data from file (256KB) through WebSocket

    I am not able to send large binary data from Java WebSocket client to Java WebSocket server. However, 1KB data transfer is working. Below is my code:

    Client:
    RandomAccessFile aFile = new RandomAccessFile
    ("c:\\test.txt", "r");
    FileChannel inChannel = aFile.getChannel();
    ByteBuffer buffer = ByteBuffer.allocate(1024);
    while(inChannel.read(buffer) > 0)
    {
    session.getBasicRemote().sendBinary(buffer, false);
    buffer.clear(); // do something with the data and clear/compact it.
    }

    Server:
    @OnMessage
    public String onMessage(ByteBuffer byteBuffer, boolean EOF) throws IOException {
    File outFile = new File("c:\\Test_output.txt");
    FileChannel channel;
    channel = new FileOutputStream(outFile, true).getChannel();
    channel.write(byteBuffer);


    Give me an example of WebSocket sample for large binary data transfer.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,172
    Rep Power
    20

    Default Re: How to send binary data from file (256KB) through WebSocket

    How is it failing?

    I can hazard a guess, but it would be good to be told what it is about the 256(ish) file that is going wrong.

    Also, what class is "session"?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,260
    Rep Power
    6

    Default Re: How to send binary data from file (256KB) through WebSocket

    Its part of JSR 356 -- Java API for WebSockets ;)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,172
    Rep Power
    20

    Default Re: How to send binary data from file (256KB) through WebSocket

    Yeah, but I really don't like not being given the declaration.
    Don't like making an assumption about what class is actually being used...been bitten by that one before.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 5
    Last Post: 07-14-2014, 08:50 AM
  2. Replies: 3
    Last Post: 02-11-2013, 08:40 AM
  3. Replies: 2
    Last Post: 01-29-2013, 10:34 AM
  4. Adding data to an existing binary file
    By NatProg in forum Advanced Java
    Replies: 5
    Last Post: 03-15-2012, 01:51 AM
  5. Reading Binary File and Changing data
    By janakiram.attuluri in forum Advanced Java
    Replies: 1
    Last Post: 12-21-2007, 11:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •