SLIP 9:
Write a
program which sends the name of text file from the client to server and display
the contents of that file on the client machine. If the file does not exists
display proper error msg.
/* server_Slip9_2 */
import
java.io.*;
import
java.net.*;
class
Slip9_Server
{
public static void main(String a[])
throws Exception
{
ServerSocket ss = new
ServerSocket(1000);
System.out.println("Server
is waiting for client : ");
Socket s =ss.accept();
System.out.println("Client
is connected");
DataInputStream dis=new
DataInputStream(s.getInputStream());
DataOutputStream dos=new
DataOutputStream(s.getOutputStream());
String fname
=(String)dis.readUTF();
File f = new
File(fname);
if(f.exists())
{
System.out.println("file
is exists");
FileInputStream
fin = new FileInputStream(fname);
int ch;
String msg =
"";
while((ch=fin.read())!=-1)
{
msg=msg+(char)ch;
}
dos.writeUTF(msg);
}
else
dos.writeUTF("0");
}
}
/* client_Slip9_2 */
import
java.io.*;
import
java.net.*;
class
Slip9_Client
{
public static void main(String a[])
throws Exception
{
Socket s = new
Socket("localhost",1000);
System.out.println("client
is connected : ");
DataInputStream dis=new
DataInputStream(s.getInputStream());
DataOutputStream dos=new
DataOutputStream(s.getOutputStream());
BufferedReader br = new
BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter
file name : ");
String fname =
br.readLine();
dos.writeUTF(fname);
String msg =
(String)dis.readUTF();
if(msg.equals("0"))
System.out.println("File
not present ");
else
{
System.out.println("Content
of the file is : \n");
System.out.println(msg);
}
}
}
No comments:
Post a Comment