Solved

Passing files through sockets...

Posted on 2002-06-28
3
248 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
[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
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 100 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

749 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