aspose file tools*
The moose likes Beginning Java and the fly likes i am in trouble Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "i am in trouble" Watch "i am in trouble" New topic
Author

i am in trouble

aarhus arhus
Greenhorn

Joined: Feb 12, 2004
Posts: 5
i am running these two client server programmes on local machine.Actually i want to send a request from client that send me(file name) file so server will send this file to client without open it.File is on local machine and the size of file is 1 MB.So plz update both code for this requirment.Thankz in advance
thats my client programme
------------------------------------------------------------------------
import java.net.*;
import java.io.*;
import java.util.*;
public class Client2{
public static void main(String[] args){
String server="localhost";
int port=80;

try{
Socket socket=new Socket(server,port);
BufferedReader inputStream=new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter outputStream=new PrintWriter(new OutputStreamWriter(socket.getOutputStream()),true);
outputStream.println("I need a file named atif.txt");
System.out.println(inputStream.readLine());
socket.close();
}catch(ConnectException e){System.out.println(e);}
catch(UnknownHostException e){System.out.println(e);}
catch(IOException e){System.out.println(e);}

}
}
------------------------------------------------------------------
thats my server programme
--------------------------------------------------------------
import java.net.*;
import java.io.*;
import java.util.*;
public class Server2 {
public static void main(String[] args) {
Socket client;
PrintWriter outputStream;
BufferedReader inputStream;
try
{
ServerSocket server = new ServerSocket(80);
System.out.println("Server listening on port 80");
client = server.accept();
inputStream = new BufferedReader(new InputStreamReader(client.getInputStream()));
outputStream = new PrintWriter(client.getOutputStream(),true);
while(true)
{
String request = inputStream.readLine();
System.out.println("Client says:" + request);
String reply = "Ya i have this file";
outputStream.println(reply );
DataInputStream input=new DataInputStream(new FileInputStream("atif.txt"));
String s=input.toString();
outputStream.println(s);
client.close();
}

}catch(IOException e)
{
System.out.println(e);
}
}
}
----------------------------------------------------------------
Brian Pipa
Ranch Hand

Joined: Sep 29, 2003
Posts: 299
Is there a question here?
brian


My Java Freeware:<br />MACCC - <a href="http://maccc.pipasoft.com" target="_blank" rel="nofollow">http://maccc.pipasoft.com</a><br />Nator - <a href="http://nator.pipasoft.com" target="_blank" rel="nofollow">http://nator.pipasoft.com</a>
aarhus arhus
Greenhorn

Joined: Feb 12, 2004
Posts: 5
actually this programme only send a line of text i want to send a file from server to client
aarhus arhus
Greenhorn

Joined: Feb 12, 2004
Posts: 5
actually this programme only send a line of text i want to send a 1BM file from server to client
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
Hi aarhus
I already see the same question by you on "three" forms- java beg, inter, advanced. From which forum would you like to get answer from me?
I don't think if you do DataInputStream.toString() it would read the "whole" file magically. You have to read data from the file by read methods on the input stream.
Also, you need to determine what is level of the question rather than just posting to all the forms, you find on list and before anything else I would recommend you to read more about streams and how they work.
Thanks
Maulin
Adrian Yan
Ranch Hand

Joined: Oct 02, 2000
Posts: 688
Umm... from the code, it looks like your client is requesting a file from server, but the server never actually read the file and send it back. As per previous post, toString() method doesn't read the file, also, if you are sending a text file, why are you using DataInputStream to read it?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: i am in trouble