1.
Write a client-server program which displays the server machine’s date and time on the client
machine.
Solution:
Client code:
import [Link].*;
import [Link].*;
import [Link].*;
class s14q1Client
{
public static void main(String[] a)throws IOException
{
Socket st=new Socket("localhost",5917);
DataInputStream dis=new DataInputStream([Link]());
String d=[Link]()+"";
[Link]("date:"+d);
}
Server code:
import [Link].*;
import [Link].*;
import [Link].*;
class s14q1Server
{
public static void main(String[] a)throws IOException
{
ServerSocket st=new ServerSocket(5917);
Socket s= [Link]();
DataOutputStream dos=new DataOutputStream([Link]());
Date d=new Date();
[Link]("date:"+d);
[Link](d+"");
}
[Link] a program to find primary IP address of the host name which you passed as a parameter
import [Link].*;
public class OReillyByName {
public static void main (String[] args) {
try {
InetAddress address = [Link]("[Link]");
[Link](address);
} catch (UnknownHostException ex) {
[Link]("Could not find [Link]");
}
}
}
[Link] a program which sends the name of a text file from the client to server and displays the
contents of the file on the client machine. If the file is not found, display an error message.
server
import [Link].*;
import [Link].*;
import [Link].*;
class seta2serv
{
public static void main(String a[])
{
try
{
ServerSocket sc=new ServerSocket(4200);
[Link]("waiting for connection");
Socket s=[Link]();
[Link]("connection is established");
InputStream in=[Link]();
DataInputStream din= new DataInputStream(in);
String name=[Link]();
[Link]("File Name" +name);
OutputStream out=[Link]();
DataOutputStream dout=new DataOutputStream(out);
File f1=new File(name);
if([Link]())
{
String msg="",str="";
FileReader fr=new FileReader(name);
BufferedReader br=new BufferedReader(fr);
while((str=[Link]())!=null)
msg=msg+str+"\n";
[Link]("CONTENTS OF FILE:-\n"+msg);
}
else
[Link]("0");
}
catch(Exception e)
{
[Link]("Error="+e);
}
}
}
Client :
import [Link].*;
import [Link].*;
class seta2clin
{
public static void main(String a[])
{
try
{
Socket s=new Socket("localhost",4200);
BufferedReader br= new BufferedReader(new InputStreamReader([Link]));
[Link]("enter the file name is");
String name=[Link]();
OutputStream out=[Link]();
DataOutputStream dout=new DataOutputStream(out);
[Link](name);
InputStream in=[Link]();
DataInputStream din=new DataInputStream(in);
String msg=[Link]();
if([Link]("0"))
[Link]("file not found");
else
[Link](msg);
}
catch(Exception e)
{
[Link]("Error is:"+e);
}
}
}
[Link] a program to accept a list of file names on the client machine and check how many exist
on the server. Display appropriate messages on the client side.
import [Link].*;
import [Link].*;
class Slip10_Server
{
public static void main(String a[]) throws Exception
{
ServerSocket ss = new ServerSocket(1000);
[Link]("Server is waiting for client : ");
Socket s =[Link]();
[Link]("Client is connected");
DataInputStream dis=new DataInputStream([Link]());
DataOutputStream dos=new DataOutputStream([Link]());
while(true)
{
String fname =(String)[Link]();
if([Link]("End"))
{ break;
}
File f = new File(fname);
if([Link]())
{
[Link]("1");
}
else [Link]("0");
}
}
}
/* Client_Slip10_2 */
import [Link].*;
import [Link].*;
class Slip_Client
{
public static void main(String a[]) throws Exception
{
Socket s = new Socket("localhost",1000);
[Link]("client is connected : ");
DataInputStream dis=new DataInputStream([Link]());
DataOutputStream dos=new DataOutputStream([Link]());
BufferedReader br = new BufferedReader(new InputStreamReader([Link]));
while(true)
{
[Link]("Stop proceesing enter End");
[Link]("Enter file name : ");
String fname = [Link]();
[Link](fname);
if([Link]("End"))
{
break;
}
String msg = (String)[Link]();
if([Link]("0"))
[Link]("File not present ");
else
{
[Link]("File Present");
//[Link](msg);
}
}
}
}
[Link] a server program which echoes messages sent by the client. The process continues till
the client types “END”.
import [Link].*;
import [Link].*;
class Server_Slip6_2
{
public static void main(String a[]) throws Exception
{
ServerSocket ss = new ServerSocket(1000);
[Link]("Server is waiting for client : ");
Socket s =[Link]();
[Link]("Client is connected");
DataInputStream ios=new DataInputStream([Link]());
DataOutputStream dos=new DataOutputStream([Link]());
BufferedReader br = new BufferedReader(new InputStreamReader([Link]));
String s1,s2;
while(true)
{
s1 = (String)[Link]();
if([Link]("end") || [Link]("END"))
{
[Link]("chatting terminated");
break;
}
[Link]("Client "+s1);
[Link]("Server ...");
s2 = [Link]();
[Link](s2);
if([Link]("end") || [Link]("END"))
{
[Link]("chatting terminated");
break;
}
}
}
}
/*client_Slip6_2*/
import [Link].*;
import [Link].*;
class Client_Slip6_2
{
public static void main(String a[]) throws Exception
{
Socket s = new Socket("localhost",1000);
[Link]("client is connected : ");
DataInputStream ios=new DataInputStream([Link]());
DataOutputStream dos=new DataOutputStream([Link]());
BufferedReader br = new BufferedReader(new InputStreamReader([Link]));
String s1,s2;
while(true)
{
[Link]("Server ....");
s1=[Link]();
[Link](s1);
s2=(String)[Link]();
if([Link]("end") || [Link]("END"))
{
[Link]("chatting terminated");
break;
}
[Link]("Client "+s2);
}
}
}