?
Solved

Passing files through sockets...

Posted on 2002-06-28
3
Medium Priority
?
265 Views
Last Modified: 2010-03-31
Hi,
I want to pass some files, from one machine to another via socket system.
My questions is, how to read and pass the files?
Reading the file and storing it in an array of bytes, and then serializing this array?

There is another way to do that more clean?

Thx
MARC
0
Comment
Question by:shoooot
3 Comments
 
LVL 3

Expert Comment

by:daitt
ID: 7116110
Hello, you can write something like this:

//Sending
byte[] buf = new byte[1024];
int length = fileinputstream.read(buf);
while (length > 0){
   socketoutputstream.write(buf,length);
   length = fileinputstream.read(buf);
}

//Receiving
byte[] buf = new byte[1024];
int length = socketinputstream.read(buf);
while (length > 0){
   fileoutputstream.write(buf,length);
   length = socketinputstream.read(buf);
}
0
 
LVL 9

Expert Comment

by:Venci75
ID: 7116115
try this:
// sending a file:

FileInputStream in = new FileInputStream("myFile");
byte[] buffer = new byte[2048];
Socket s = new Socket("localhost", 12345);
OutputStream out = s.getOutputStream();
int r;
while ((r = in.read(buffer)) != -1)
  out.write(buffer, 0, r);

out.flush();
out.close();


// receiving the file:

FileOutputStream out = new FileOutputStream("myFile");
byte[] buffer = new byte[2048];
Socket s = (new ServerSocket(12345)).accept;
InputStream in = s.getInputStream();
int r;
while ((r = in.read(buffer)) != -1)
  out.write(buffer, 0, r);

out.flush();
out.close();
0
 
LVL 6

Accepted Solution

by:
gadio earned 400 total points
ID: 7116489
I'd like to offer a different approach. If you send the file yourself, most of the chances are that any protocol that you implement yourself will not be good enough (no error correction, communication errors handling, checksum and all the other things that makes a mature protocol). Instead, why dont you use an existing protocol like ftp? You can have an ftp server (both unix and NT has them by default) on one of the machines, and have the other one use an ftp implementation to push or pull the file. You can get many free implementations on ftp client side, or you can use the undocumented built in ftp client:

import sun.net.*;
import sun.net.ftp.*;
import java.io.*;


public class ftp_utility {

  public ftp_utility () {
      FtpClient fcMyFtp = new FtpClient();

      try {
          int ch;
          fcMyFtp.openServer("myftpsite");
          fcMyFtp.login("myname","mypassword");
          TelnetInputStream tisList = fcMyFtp.list();
          while( (ch = tisList.read()) != -1 ) System.out.print((char)ch);
          // ... other stuff.
          fcMyFtp.cd("temp");
          fcMyFtp.binary();

          // writing a file:
          FileInputStream fis = new FileInputStream( "/home/gadio/temp/tempfile
" );
          TelnetOutputStream  tos = fcMyFtp.put("tempfile.new");
          byte buffer[] = new byte[1000];
          int len;
          while( (len = fis.read(buffer)) != -1 ) {
              tos.write(buffer,0,len);
          }
          fis.close();
          tos.close();

          // reading a file:
          FileOutputStream fos = new FileOutputStream( "/home/gadio/temp/tempfile.read" );
          TelnetInputStream   tis = fcMyFtp.get("tempfile.new");
          while( (len = tis.read(buffer)) != -1 ) {
              fos.write(buffer,0,len);
          }
          tis.close();
          fos.close();
      } catch( IOException e ) {
          e.printStackTrace();
      }
  }

  public static void main(String args[]) {
      ftp_utility f = new ftp_utility();
  }
}


You can also find many implementations on the web.
Hope that helps.
gadio

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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 …
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month16 days, 13 hours left to enroll

864 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