Solved

Passing files through sockets...

Posted on 2002-06-28
3
217 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
Comment Utility
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
Comment Utility
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 100 total points
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This video teaches viewers about errors in exception handling.

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now