Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

File from ObjectInputStream through Socket  RSS feed

 
Adam Sailer
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I could really use some help on this one. I have a Server app on machine "A" that sends a File (an mp3 file for test purposes) through a socket to a Client app on machine "B".

The Client app recieves the Object through the ObjectInputStream over the socket, and casts it to a File object. I can then print out filename.getName(), filename.getPath(), etc. the only quirk is that filename.length() returns a length of 0 bytes.

How can I take this File sent through the Socket and reconstitute it on the hard drive of the Client machine, as an exact copy of the File on the hard drive of the Server machine?

A code sample for the Client process would be helpful. Am I close? What am I missing?

Thank you,
Adam
 
Rawat (Zeon)
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Adam,

What I think that you need to send the byte array also to the server from client. I did like to look at your client code before I could say anything else on it.

-Zeon
 
Adam Sailer
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what does this mean? Am I only sending a "reference" to the file throught the socket?

Server Code:

// Server.java
// Created on February 12, 2005, 3:45 PM
// @author Adam Sailer
import java.net.*;
import java.io.*;

public class Server
{

private int port = 10000;
private InetAddress serverAddress = null;
private ServerSocket serverSocket = null;
private Socket clientSocket = null;
private ObjectInputStream input = null;
private ObjectOutputStream output = null;
private Object clientObject = null;
private File path = new File("D:\\Java\\Love Potion #9.mp3");


public Server()
{ }

public static void main(String[] args)
{
Server server = new Server();
server.process();
}

private void process()
{
try
{
serverAddress = InetAddress.getLocalHost();
serverSocket = new ServerSocket(port, 10, serverAddress);

System.out.println("\n\n" + serverAddress);
System.out.println(serverSocket);

clientSocket = serverSocket.accept();

////output = new ObjectOutputStream(new BufferedOutputStream(clientSocket.getOutputStream()));
////input = new ObjectInputStream(new BufferedInputStream(clientSocket.getInputStream()));

output = new ObjectOutputStream(clientSocket.getOutputStream());
input = new ObjectInputStream(clientSocket.getInputStream());

while((clientObject = input.readObject()) != null)
{
System.out.println(clientObject);
//output.writeObject(clientObject);
System.out.println("\n" + path.getName() + "\t" + path.length() + "\n");
output.writeObject(path);
}

}
catch (Exception e)
{
System.out.println(e);
////System.exit(1);
}
}


private void cleanUp()
{
System.out.println("Closing " + serverSocket);
System.out.println("Closing " + clientSocket);

try
{
output.close();
input.close();
clientSocket.close();
serverSocket.close();
}
catch (Exception e)
{
System.out.println(e + "\n\nPlease Restart System.");
System.exit(1);
}
}


}


Client Code:

// Client.java
// Created on February 12, 2005, 6:09 PM
// @author Adam Sailer
import java.io.*;
import java.net.*;
import java.nio.channels.*;

public class Client
{

private int port = 10000;
private InetAddress serverAddress = null;
private Socket socket = null;
private ObjectInputStream input = null;
private ObjectOutputStream output = null;
private Object serverObject = null;
private File path = new File("D:\\temp\\copy");
private File in = null;
private File outFile = null;

public Client()
{ }


public static void main(String[] args)
{
Client client = new Client();
client.process();
}

private void process()
{
try
{
byte[] addr = {(byte) 192, (byte) 168, (byte) 0, (byte) 10};
////byte[] addr = {(byte) 192, (byte) 168, (byte) 0, (byte) 2};
serverAddress = InetAddress.getByAddress(addr);
socket = new Socket(serverAddress, port);
System.out.println("\n\n" + socket);

////output = new ObjectOutputStream(new BufferedOutputStream(socket.getOutputStream()));
////input = new ObjectInputStream(new BufferedInputStream(socket.getInputStream()));

output = new ObjectOutputStream(socket.getOutputStream());
input = new ObjectInputStream(socket.getInputStream());


//// test code
String string = "hello world";
output.writeObject(string);
while ((serverObject = input.readObject()) != null)
{
System.out.println("\nObject\t\t" + serverObject);
System.out.println("Class\t\t" + serverObject.getClass());

in = new File("");
in = (File) serverObject;

System.out.println("Name\t\t" + in.getName());
System.out.println("AbsFile\t\t" + in.getAbsoluteFile());
System.out.println("AbsPath\t\t" + in.getAbsolutePath());
System.out.println("CanonFile\t" + in.getCanonicalFile());
System.out.println("CanonPath\t" + in.getCanonicalPath());
System.out.println("Path\t\t" + in.getPath());
System.out.println("Parent\t\t" + in.getParent());
System.out.println("ParentFile\t" + in.getParentFile());
System.out.println("Length\t\t" + in.length());

info();

}
}
catch (Exception e)
{
System.out.println(e);
System.exit(1);
}
}


private void info()
{
try
{
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File(path + "\\" + in.getName())));
oos.writeObject(serverObject);

/*
outFile = new File(path + "\\" + in.getName());
outFile.createNewFile();

System.out.println("outFile\t\t" + outFile);
FileChannel srcChannel = new FileInputStream(in).getChannel();

System.out.println("srcChannel\t" + srcChannel);

// Create channel on the destination
FileChannel dstChannel = new FileOutputStream(path + "\\" + in.getName()).getChannel();
System.out.println("dstChannel\t" + dstChannel);

// Copy file contents from source to destination
dstChannel.transferFrom(srcChannel, 0, srcChannel.size());


// Close the channels
srcChannel.close();
dstChannel.close();

System.exit(1);


// String command = "C:\\Programs\\Winamp\\winamp.exe " + in.getName();
// Runtime.getRuntime().exec(command);
*/

}
catch (Exception e)
{
System.out.println(e);
//// info();
}
}



private void cleanUp()
{
System.out.println("Closing " + socket);

try
{
output.close();
input.close();
socket.close();
}
catch (Exception e)
{
System.out.println(e + "\n\nPlease Restart System.");
System.exit(1);
}
}


}
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct. A File instance is just the name and methods to get to a Channel or Stream to read and write the actual file on disk. You need to read in the mp3 data and send it over the stream. You can read the whole thing or you can read and send chunks. Search back a couple weeks as I and others have posted code to do this in various ways.
 
Adam Sailer
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, David.

I've been working on this for two days straight.. (where did my weekend go?) Could you point me in the right direction?

Adam
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd start with Sun's Streams Tutorials. The key classes you'll use (look them up in the JavaDocs, all in java.io package) are
  • FileInputStream
  • OutputStream
  • BufferedInputStream
  • BufferedOutputStream

  • The last two you can add after you get it all working to spead things up. In fact, since you'll be reading chunks they may be unnecessary.

    Here is a starting point. I'm leaving a lot of the details for you to research so you get familiar with the classes. In English, what you're doing is erading a chunk from the file and sending it over the socket. Do this again and again until the file is completely sent.You need to figure out how to know when you've read the whole file and add error handling.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!