client-server (file transfer from server to client)
<p>This is a file-transfer program. The client sends a file name and then if that file exists the server send the file to client. I want the server to send a message like "file doesn't exist" when the file doesn't exist.</p>

<pre><code>public static void main(String[] args) {

    Socket socket = null;
String server = "localhost";
    DataInputStream in;
BufferedReader input;
    PrintWriter output;
int port = 1500;

    File file;
    file = new File("G:\\o.txt");
    try{
    if(!file.exists())
         file.createNewFile();
    }
    catch(Exception e){
        System.out.println("error1");
    }


try {
    socket = new Socket(server, port);
    System.out.println("Connected To Server With This Ip" + socket.getInetAddress() + " On This Port :" + socket.getPort());
}
catch (UnknownHostException e) {
    System.out.println(e);

}
catch (IOException e) {
    System.out.println(e);

}


try {
    output = new PrintWriter(socket.getOutputStream(),true);
    input = new BufferedReader(new InputStreamReader(socket.getInputStream()));


    String filewanted="H:\\\\o.txt";
    output.println(filewanted);

    in=new DataInputStream(socket.getInputStream()); 


        int flag=0;  
        FileWriter fw = new FileWriter(file);
        BufferedWriter bw = new BufferedWriter(fw); 
        int b=in.readInt();
        for(int i=1;i&lt;=b;i++)
           {
               String message= input.readLine();
                bw.write(message);
                bw.newLine();

                flag++;
             }
               bw.close();
               if(flag!=0){
                     System.out.println("File Was Transferd Successfuly!");   


               }


      }

catch (IOException e) {
    System.out.println(e);
}

try {
    socket.close();
}
catch (IOException e) {
    System.out.println(e);
}
}



//server


 public static void main(String[] args) {

ServerSocket serversocket;
    DataOutputStream out;
PrintWriter output;
    BufferedReader input;
    String linesent;
    int lines = 0;
    int port=1500;

try {

  serversocket = new ServerSocket(port);
  System.out.println("Server Waiting For Client On Port " + serversocket.getLocalPort()+"...");


  while(true) {
    Socket socket= serversocket.accept();
    System.out.println("Connection Accepted With This Ip: " +socket.getInetAddress() +" On This Port :" + socket.getPort());
    input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            output = new PrintWriter(socket.getOutputStream(),true);



           String f=input.readLine();
           File file;
           file = new File(f);


             BufferedReader reader = new BufferedReader(new FileReader(f));
             if(file.exists())
             {
               while (reader.readLine() != null)
                    lines++;
               reader.close();
               out=new DataOutputStream(socket.getOutputStream());
               out.writeInt(lines);
               Scanner scanner=new Scanner(file);
               while(scanner.hasNext()) 
                 {
                    linesent=scanner.nextLine();
                    output.println(linesent);
                 }         


             }
            else {

                throw new FileNotFoundException(f);


            }

    try {
        socket.close();
        System.out.println("Connection closed by client.");
    }
    catch (IOException e) {
        System.out.println(e);
    }

    }
    }



catch (IOException e) {
    System.out.println(e);
}


}
</code></pre>
