Solved

Passing files through sockets...

Posted on 2002-06-28
3
255 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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

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 …
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

717 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