Two Laptop Bag
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

i am in trouble

aarhus arhus

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.util.*;
public class Client2{
public static void main(String[] args){
String server="localhost";
int port=80;

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");
}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.util.*;
public class Server2 {
public static void main(String[] args) {
Socket client;
PrintWriter outputStream;
BufferedReader inputStream;
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);
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();

}catch(IOException e)
Brian Pipa
Ranch Hand

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

My Java Freeware:<br />MACCC - <a href="" target="_blank" rel="nofollow"></a><br />Nator - <a href="" target="_blank" rel="nofollow"></a>
aarhus arhus

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

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: 1873
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.
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:
subject: i am in trouble
It's not a secret anymore!