/*Client Socket program that accepts file name from Client and send it to Server, Server Side Socket reads the Content of a file and display them onto the Client Socket*/
/*Server Program (severprac.java)*/
import java.io.*;
import java.net.*;
public class serverprac
{
public static void main(String[] args) {
ServerSocket ss;
Socket client;
String nm;
try
{
ss = new ServerSocket(1212);
System.out.println("Server is Waitng For Client");
client = ss.accept();
System.out.println("Client is Connected");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedReader br1 = new BufferedReader(new InputStreamReader(client.getInputStream()));
PrintStream ps = new PrintStream(client.getOutputStream());
String filenm = br1.readLine();
File f = new File(".");
File[] flist = f.listFiles();
for(File n:flist)
{
nm = n.getName();
if(nm.equals(filenm))
{
System.out.println("File is Present");
FileReader fr = new FileReader(nm);
BufferedReader bfr = new BufferedReader(fr);
String lne;
while(true)
{
lne = bfr.readLine();
if (lne == null)
break;
//System.out.println(lne);
ps.println(lne);
}
}
}
ps.close();
br1.close();
br.close();
ss.close();
}//try
catch(Exception e)
{
System.out.println(e);
}
}//main
}
/*Client Program (clientprac.java)*/
import java.io.*;import java.net.*;
public class clientprac
{
public static void main(String[] args)
{
Socket s;
try
{
s = new Socket("127.0.0.1",1212);
System.out.println("Clent is Conneccted to the Server");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedReader br1 = new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintStream ps = new PrintStream(s.getOutputStream());
System.out.println("Enter File name");
ps.println(br.readLine());
while(br1.readLine() !=null)
System.out.println(br1.readLine());
br1.close();
ps.close();
br.close();
br1.close();
s.close();
}
catch(Exception e)
{
System.out.println(""+e);
}//catch
}//main
}