?
Solved

Passing files through sockets...

Posted on 2002-06-28
3
Medium Priority
?
259 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 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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 about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
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 Month14 days, 5 hours left to enroll

800 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