Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Reading binary data from a socket.

Posted on 2003-02-19
5
Medium Priority
?
224 Views
Last Modified: 2010-03-31
Can someone provide a short code listing of the most efficient way to stream a binary file (say PDF or image file) from a socket?

As simple as it sounds, I have not found any elegant solutions for this simple problem on the web that would be efficient and work well in different environments.

Thanks.
 
0
Comment
Question by:MeSpice107
  • 2
4 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 7982718
This sort of thing:

class FileReceiver implements Runnable {
  /**
   * A self-running class that handles
   * receiving a file transmission
   */
  Socket socket;
  InputStream in;
  OutputStream out;
  long bytesReceived;

  public FileReceiver(Socket socket) {
    this.socket = socket;
    new Thread(this).start();
  }


  public void run() {
    /**
     * Get an input stream on the socket,
     * and write the file to an output
     * stream
     */
    try {
      in = socket.getInputStream();
      int buf;
      out = new FileOutputStream(getSavePath());
      System.out.println("FileReceiver receiving file:");
      while ((buf = in.read()) > -1) {
        doOutput(buf);
      }

    }
    catch (IOException e) {
      e.printStackTrace();
    }
    finally {
      if (in != null) {
        try { in.close(); } catch(IOException e) { e.printStackTrace(); }
      }
      if (out != null) {
        System.out.println("\nFile reception completed (unverified)");
        System.out.println("Listening for next file...");
        try { out.close(); socket.close(); } catch(IOException e) { e.printStackTrace(); }
      }
    }
  }

  public void doOutput(int buf) {
    /**
     * Loose couples the reception from
     * the loop in which it happens
     */
    try {
      out.write(buf);
      bytesReceived++;
      if (bytesReceived % 1024 == 0)
      System.out.print("*");
    }
    catch(Exception e){
      e.printStackTrace();
    }
  }
}
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 200 total points
ID: 7982727
You'd need to put the function getSavePath, returning a String, in there
0
 

Expert Comment

by:CleanupPing
ID: 9059233
MeSpice107:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 7

Expert Comment

by:tomboshell
ID: 9553051
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
ANSWERED by: CEHJ

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Thomas Boshell
EE Cleanup Volunteer
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month10 days, 22 hours left to enroll

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question