?
Solved

Sockets

Posted on 2003-03-02
2
Medium Priority
?
202 Views
Last Modified: 2011-09-20
Hello all,

Can anyone give me the code on how to send a file or just stream 2MB over a socket??

Thanks
0
Comment
Question by:aknight_sa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 200 total points
ID: 8051418
 class FileSender {
    final int BUF_SIZE = 4096;
    Socket socket;
    OutputStream out;

    public FileSender(Socket socket) {
      this.socket = socket;
    }

    public boolean sendFile(String fileName) {
      FileInputStream from = null;
      try {
        int bytesRead = 0;
        from = new FileInputStream(fileName);
        out = new BufferedOutputStream(socket.getOutputStream());
        byte[] buffer = new byte[BUF_SIZE];
        while((bytesRead = from.read(buffer)) != -1) // Read bytes until EOF
          out.write(buffer, 0, bytesRead);            //   write bytes
      }
      // Always close the streams, even if exceptions were thrown
      catch (IOException e) {
        return false;
      }
      finally {
        if (from != null) try { from.close(); } catch (IOException e) { ; }
        if (out != null) try { out.close(); } catch (IOException e) { ; }
        if (socket != null) try { socket.close(); } catch (IOException e) { ; }
      }
      return true;
    }
  }
}
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 8051428
The code below reads data from a file and writes it over a socket:-

InputStream in = new FileInputStream("file");
OutputStream out = socket.getOuputStream();
final int CHUNK_SIZE = 1024;
byte buff[] = new byte[CHUNK_SIZE];
int read = in.read(buff, 0, CHUNK_SIZE);
while(read>0) {
    out.write(buff, 0, read);
    read = in.read(buff, 0, CHUNK_SIZE);
}
in.close();
out.close();
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn different types of Android Layout and some basics of an Android App.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month9 days, 1 hour left to enroll

764 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