• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

i am in trouble

 
aarhus arhus
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a question here?
brian
 
aarhus arhus
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
actually this programme only send a line of text i want to send a file from server to client
 
aarhus arhus
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
actually this programme only send a line of text i want to send a 1BM file from server to client
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 688
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic