[go: up one dir, main page]

0% found this document useful (0 votes)
12 views7 pages

Client Server

The document contains multiple Java programs demonstrating client-server architecture, including displaying server date and time on the client, finding the primary IP address of a hostname, sending and displaying file contents, checking the existence of multiple files, and echoing messages until a termination command is received. Each program is structured with separate client and server classes, showcasing socket programming and data handling. The examples illustrate various networking concepts and error handling in Java.

Uploaded by

truptinik27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views7 pages

Client Server

The document contains multiple Java programs demonstrating client-server architecture, including displaying server date and time on the client, finding the primary IP address of a hostname, sending and displaying file contents, checking the existence of multiple files, and echoing messages until a termination command is received. Each program is structured with separate client and server classes, showcasing socket programming and data handling. The examples illustrate various networking concepts and error handling in Java.

Uploaded by

truptinik27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

1.

Write a client-server program which displays the server machine’s date and time on the client
machine.
Solution:

Client code:
import java.net.*;
import java.io.*;
import java.util.*;

class s14q1Client
{
public static void main(String[] a)throws IOException
{
Socket st=new Socket("localhost",5917);
DataInputStream dis=new DataInputStream(st.getInputStream());
String d=dis.readUTF()+"";
System.out.println("date:"+d);
}

Server code:
import java.net.*;
import java.io.*;
import java.util.*;

class s14q1Server
{
public static void main(String[] a)throws IOException
{
ServerSocket st=new ServerSocket(5917);
Socket s= st.accept();
DataOutputStream dos=new DataOutputStream(s.getOutputStream());
Date d=new Date();
System.out.println("date:"+d);
dos.writeUTF(d+"");
}

2.Write a program to find primary IP address of the host name which you passed as a parameter
import java.net.*;

public class OReillyByName {

public static void main (String[] args) {


try {
InetAddress address = InetAddress.getByName("www.oreilly.com");
System.out.println(address);
} catch (UnknownHostException ex) {
System.out.println("Could not find www.oreilly.com");
}
}
}

3.Write 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 java.io.*;
import java.util.*;
import java.net.*;
class seta2serv
{
public static void main(String a[])
{
try
{
ServerSocket sc=new ServerSocket(4200);
System.out.println("waiting for connection");
Socket s=sc.accept();
System.out.println("connection is established");
InputStream in=s.getInputStream();
DataInputStream din= new DataInputStream(in);
String name=din.readUTF();
System.out.println("File Name" +name);
OutputStream out=s.getOutputStream();
DataOutputStream dout=new DataOutputStream(out);
File f1=new File(name);
if(f1.exists())
{
String msg="",str="";
FileReader fr=new FileReader(name);
BufferedReader br=new BufferedReader(fr);
while((str=br.readLine())!=null)
msg=msg+str+"\n";
dout.writeUTF("CONTENTS OF FILE:-\n"+msg);
}
else
dout.writeUTF("0");
}
catch(Exception e)
{
System.out.println("Error="+e);
}
}
}

Client :

import java.io.*;
import java.net.*;

class seta2clin
{
public static void main(String a[])
{

try
{
Socket s=new Socket("localhost",4200);
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter the file name is");
String name=br.readLine();

OutputStream out=s.getOutputStream();
DataOutputStream dout=new DataOutputStream(out);
dout.writeUTF(name);
InputStream in=s.getInputStream();
DataInputStream din=new DataInputStream(in);
String msg=din.readUTF();
if(msg.equals("0"))
System.out.println("file not found");
else
System.out.println(msg);

}
catch(Exception e)
{
System.out.println("Error is:"+e);

}
}
}
4.Write 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 java.io.*;
import java.net.*;

class Slip10_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());
while(true)
{
String fname =(String)dis.readUTF();
if(fname.equals("End"))
{ break;

}
File f = new File(fname);
if(f.exists())
{
dos.writeUTF("1");
}
else dos.writeUTF("0");
}
}
}
/* Client_Slip10_2 */

import java.io.*;
import java.net.*;

class Slip_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));


while(true)
{
System.out.println("Stop proceesing enter End");
System.out.println("Enter file name : ");
String fname = br.readLine();

dos.writeUTF(fname);
if(fname.equals("End"))
{
break;
}
String msg = (String)dis.readUTF();
if(msg.equals("0"))
System.out.println("File not present ");
else
{
System.out.println("File Present");
//System.out.println(msg);
}
}
}
}

5.Write a server program which echoes messages sent by the client. The process continues till
the client types “END”.

import java.io.*;
import java.net.*;

class Server_Slip6_2
{
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 ios=new DataInputStream(s.getInputStream());
DataOutputStream dos=new DataOutputStream(s.getOutputStream());

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));


String s1,s2;
while(true)
{
s1 = (String)ios.readUTF();
if(s1.equals("end") || s1.equals("END"))
{
System.out.println("chatting terminated");
break;
}
System.out.println("Client "+s1);
System.out.println("Server ...");
s2 = br.readLine();
dos.writeUTF(s2);
if(s2.equals("end") || s2.equals("END"))
{
System.out.println("chatting terminated");
break;
}
}
}
}
/*client_Slip6_2*/

import java.io.*;
import java.net.*;

class Client_Slip6_2
{
public static void main(String a[]) throws Exception
{
Socket s = new Socket("localhost",1000);
System.out.println("client is connected : ");
DataInputStream ios=new DataInputStream(s.getInputStream());
DataOutputStream dos=new DataOutputStream(s.getOutputStream());

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));


String s1,s2;
while(true)
{
System.out.println("Server ....");
s1=br.readLine();
dos.writeUTF(s1);
s2=(String)ios.readUTF();
if(s2.equals("end") || s2.equals("END"))
{
System.out.println("chatting terminated");
break;
}
System.out.println("Client "+s2);
}
}
}

You might also like