Write a program to demonsrtate concept on networking.

, by Prashant Gunjal


/*
Aim Write a program to demonsrtate concept on networking.
*/

Program : service.java

import java.net.*;
import java.io.*;
class service
{
public static void main(String arg[])
{
try
{
ServerSocket ss=new ServerSocket(123);
Socket s=null;
s=ss.accept();
System.out.println("\n\tAccepted connectivity");
System.out.println("\n\tClient is :  "+s);
String str;

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(s.getOutputStream(),true);

System.out.println("\n\t To Initialize two way communication press Enter");
str=br.readLine();

InetAddress ia =InetAddress.getLocalHost();
Socket cs=new Socket(ia,124);
System.out.println("\n\tconnect to server ");
BufferedReader br1= new BufferedReader(new InputStreamReader(cs.getInputStream()));
System.out.println("\n\t\tChating..................");
do
{
System.out.print("Enter Message : ");
str=br.readLine();
pw.write(str+"\n");
pw.flush();

str=br1.readLine();
System.out.println("Reply is : "+str);
}while(!str.equals("exit"));
s.close();
}catch(Exception e)
{
System.out.println(e);
}
}
}

Program : ( client.java )

import java.net.*;
import java.io.*;
class client
{
public static void main(String arg[])
{
try
{
InetAddress ia =InetAddress.getLocalHost();
System.out.println("\n\tRequest to server");
Socket cs=new Socket(ia,123);
System.out.println("\n\tconnect to server ");
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
BufferedReader br1= new BufferedReader(new InputStreamReader(cs.getInputStream()));
String str;
System.out.println("\n\t To Initialize two way communication press Enter");
str=br.readLine();
ServerSocket ss=new ServerSocket(124);
Socket s=null;
s=ss.accept();
System.out.println("\n\tAccepted connectivity");
PrintWriter pw = new PrintWriter(s.getOutputStream(),true);
System.out.println("\n\t\tChating..................");
do
{
str=br1.readLine();
System.out.println("Message is : "+str);

System.out.print("Reply : ");
str=br.readLine();
pw.write(str+"\n");
pw.flush();

}while(!str.equals("exit"));
cs.close();
}catch(Exception e)
{
System.out.println(e);
}
}
}

1 ) now compile server first and run it in one command prompt
2 ) then compile client and run it on another command prompt





0 comments: