anitharamunni
asked on
IO Streams
What is the best way to transfer files thru sockets?? What kind of streams can be used? Can anybody help me in this??
regards
regards
Hi,
DataInputStream dis = new DataInputStream(socket.get InputStrea m());
// Not a bug, but you don't use DataInputStream specific functionality.
// Plain InputStream will work
int data;
while((data = dis.read()) != -1 ){
fw.write(data);
// You need to cast data to byte: fw.write((byte)data);
fw.flush();
// It's not a bug, but it slows you down. You don't really need to
// flush on each byte. I would even wrap FileOutputStream
// into BufferedOutputStream
}
FileReader fr = new FileReader(new File("proposal1.doc"));
// BufferedReader br = new BufferedReader(fr);
while((data = fr.read()) != -1 ){
pw.print(data);
// Now, you need to cast data to char
pw.flush();
}
pw.close();
BTW, why your client and server are not symmetric? Why don't you use OutputStream on server side?
And server doesn't need to open InputStream--client doesn't send anything.
Better server would spawn new Thread upon accept return to serve several clients concurrently.
Regards,
Igor Bazarny,
Brainbench MVP for Java 1
www.brainbench.com
DataInputStream dis = new DataInputStream(socket.get
// Not a bug, but you don't use DataInputStream specific functionality.
// Plain InputStream will work
int data;
while((data = dis.read()) != -1 ){
fw.write(data);
// You need to cast data to byte: fw.write((byte)data);
fw.flush();
// It's not a bug, but it slows you down. You don't really need to
// flush on each byte. I would even wrap FileOutputStream
// into BufferedOutputStream
}
FileReader fr = new FileReader(new File("proposal1.doc"));
// BufferedReader br = new BufferedReader(fr);
while((data = fr.read()) != -1 ){
pw.print(data);
// Now, you need to cast data to char
pw.flush();
}
pw.close();
BTW, why your client and server are not symmetric? Why don't you use OutputStream on server side?
And server doesn't need to open InputStream--client doesn't send anything.
Better server would spawn new Thread upon accept return to serve several clients concurrently.
Regards,
Igor Bazarny,
Brainbench MVP for Java 1
www.brainbench.com
> Better server would spawn new Thread upon accept return to serve several clients concurrently.
And wrap accept into infinite loop to serve more then one client.
And wrap accept into infinite loop to serve more then one client.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
oops!!
here is the modified server code!!
public class Server{
static ServerSocket listen;
static DataOutputStream dos;
static DataInputStream dis;
public static void main(String as[]){
int data;
try{
listen = new ServerSocket(8888);
Socket socket = listen.accept();
dos = new DataOutputStream(socket.ge tOutputStr eam());
//dis = new DataInputStream(socket.get InputStrea m());
FileInputStream fis = new FileInputStream(new File("Server.java"));
int count = 0;
// BufferedReader br = new BufferedReader(fr);
while((data = fis.read()) != -1 ){
count++;
dos.write(data);
dos.flush();
}
System.out.println( "Total number of char written : " + count );
dos.close();
fis.close();
}
catch(Exception e){
System.out.println("error " + e);
}
}
}
here is the modified server code!!
public class Server{
static ServerSocket listen;
static DataOutputStream dos;
static DataInputStream dis;
public static void main(String as[]){
int data;
try{
listen = new ServerSocket(8888);
Socket socket = listen.accept();
dos = new DataOutputStream(socket.ge
//dis = new DataInputStream(socket.get
FileInputStream fis = new FileInputStream(new File("Server.java"));
int count = 0;
// BufferedReader br = new BufferedReader(fr);
while((data = fis.read()) != -1 ){
count++;
dos.write(data);
dos.flush();
}
System.out.println( "Total number of char written : " + count );
dos.close();
fis.close();
}
catch(Exception e){
System.out.println("error " + e);
}
}
}
Hi,
I revoke my long comment--after inspection of doc I found that write() has int parameter. But there is still conversion problem in server code: pw.print(data); will convert int to decimal string representation, while you need simply char with correspondent code to be written. Replacing print() by write() should solve the problem.
Oops. One more conversion problem: on client side you read byte from InputStream and write it as char to Writer. That at least unsafe--there is a big range of bytes which will be changed bu such operation.
Compare Viswa's suggestion to your code--it looks like (s)he did it right.
Regards,
Igor Bazarny
I revoke my long comment--after inspection of doc I found that write() has int parameter. But there is still conversion problem in server code: pw.print(data); will convert int to decimal string representation, while you need simply char with correspondent code to be written. Replacing print() by write() should solve the problem.
Oops. One more conversion problem: on client side you read byte from InputStream and write it as char to Writer. That at least unsafe--there is a big range of bytes which will be changed bu such operation.
Compare Viswa's suggestion to your code--it looks like (s)he did it right.
Regards,
Igor Bazarny
ASKER
Thanks viswa. The code works perfect now
welcome!!
And thanx for the points
And thanx for the points
ASKER
public class Client{
static Socket socket;
public static void main(String as[]){
try{
socket = new Socket("ecomserv",8888);
DataInputStream dis = new DataInputStream(socket.get
FileWriter fw = new FileWriter(new File("proposal1.doc"));
int data;
while((data = dis.read()) != -1 ){
fw.write(data);
fw.flush();
}
fw.close();
}
catch(UnknownHostException
System.out.println("Not Connected " + e );
return;
}
catch(IOException ee){
System.out.println("IO Exception " + ee );
return;
}
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~
public class Server{
static ServerSocket listen;
static PrintWriter pw;
static DataInputStream dis;
public static void main(String as[]){
int data;
try{
listen = new ServerSocket(8888);
Socket socket = listen.accept();
pw = new PrintWriter(socket.getOutp
dis = new DataInputStream(socket.get
FileReader fr = new FileReader(new File("proposal1.doc"));
// BufferedReader br = new BufferedReader(fr);
while((data = fr.read()) != -1 ){
pw.print(data);
pw.flush();
}
pw.close();
}
catch(Exception e){
System.out.println("error " + e);
}
}
}
I am sending a file from the server to the client.
The file is transferring... but contains only junk... Can anybody explain what is going wrong here??
thankyou