Create a client/server application where the client requests for a particular file on the server. If the file exists on the server, then write the contents of the file to the client.


/*
clientServerApplicationFileRetriveServerSideProgramming.java
*/

import java.io.*;
import java.net.*;
class clientServerApplicationFileRetriveServerSideProgramming
{
public static void main(String args[]) throws Exception
{
ServerSocket ss = new ServerSocket(8888);
Socket s = ss.accept();
System.out.println("Success!! Connection Established!!");
BufferedReader cbf = new BufferedReader(new InputStreamReader(s.getInputStream()));
String FileName = cbf.readLine();
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
FileReader fr = null;
BufferedReader filebr = null;
File fileNew = new File(FileName);
if(fileNew.exists())
{
dos.writeBytes("Find"+"\n");
fr = new FileReader(FileName);
filebr = new BufferedReader(fr);
String str;
while( (str = filebr.readLine()) != null)
{
dos.writeBytes(str+"\n");
}
filebr.close();
dos.close();
cbf.close();
fr.close();
s.close();
ss.close();
}
else
{
dos.writeBytes("Oops!! File Not Found!!"+"\n");
}
}
}

/*
clientServerApplicationFileRetriveClientSideProgramming.java
*/

import java.io.*;
import java.net.*;
class clientServerApplicationFileRetriveClientSideProgramming
{
public static void main(String args[]) throws Exception
{
Socket s = new Socket("localhost",8888);
BufferedReader cbf = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter Filename : ");
String FileName = cbf.readLine();
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
dos.writeBytes(FileName+"\n");
BufferedReader sbf = new BufferedReader(new InputStreamReader(s.getInputStream()));
String str;
str = sbf.readLine();
if(str.equals("Find"))
{
while((str = sbf.readLine()) != null)
{
System.out.println(str);
}
cbf.close();
dos.close();
sbf.close();
s.close();
}
}
}
Posted By Admin

Make Comments..!!


Oops!! No posts from user.

Download Android App