shoooot
asked on
Passing files through sockets...
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
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
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)).accep t;
InputStream in = s.getInputStream();
int r;
while ((r = in.read(buffer)) != -1)
out.write(buffer, 0, r);
out.flush();
out.close();
// 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)).accep
InputStream in = s.getInputStream();
int r;
while ((r = in.read(buffer)) != -1)
out.write(buffer, 0, r);
out.flush();
out.close();
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
//Sending
byte[] buf = new byte[1024];
int length = fileinputstream.read(buf);
while (length > 0){
socketoutputstream.write(b
length = fileinputstream.read(buf);
}
//Receiving
byte[] buf = new byte[1024];
int length = socketinputstream.read(buf
while (length > 0){
fileoutputstream.write(buf
length = socketinputstream.read(buf
}