PROGRAM: (SERVER)
import
java.net.*;
import
java.io.*;
public class
TcpServer
{
public static void main(String args[]) throws IOException
{
ServerSocket serverSocket=null;
try
{
serverSocket=new ServerSocket(1234);
}
catch(IOException e)
{
System.err.println("Could not Listen on Port:1234.");
System.exit(1);
}
Socket clientSocket=null;
try
{
clientSocket=serverSocket.accept();
}
catch(IOException e)
{
System.err.println("Accept Failed.");
System.exit(1);
}
PrintWriter out=new PrintWriter(clientSocket.getOutputStream(),
true);
BufferedReader
in=new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
String int1,int2;
int num1=0,num2=0;
int1=in.readLine();
System.out.println("Received Number1:: "+int1);
int2=in.readLine();
System.out.println("Received Number2:: "+int2);
System.out.println(int1+"*"+int2);
try
{
num1=Integer.parseInt(int1);
num2=Integer.parseInt(int2);
}
catch(NumberFormatException nfe)
{
System.out.println("Numbers not Intergers");
out.println("Numbers not intergers");
}
System.out.print("="+num1*num2);
out.println(String.valueOf(num1*num2));
out.close();
in.close();
clientSocket.close();
serverSocket.close();
}
}
PROGRAM: (CLIENT)
import
java.io.*;
import
java.net.*;
public class
TcpClient
{
public static void main(String[] args) throws IOException
{
Socket socket=null;
PrintWriter out=null;
BufferedReader in=null;
try
{
socket=new Socket("127.0.0.1",1234);
out=new PrintWriter(socket.getOutputStream(),true);
in=new
BufferedReader(new InputStreamReader(socket.getInputStream()));
}
catch(UnknownHostException e)
{
System.err.println("Don't know about Host!!");
System.exit(1);
}
catch(IOException e)
{
System.err.println("Couldn't get I/o for the
connection!!");
System.exit(1);
}
BufferedReader
read=new BufferedReader(new InputStreamReader(System.in));
String num1,num2;
System.out.println("Enter Number1:: ");
num1=read.readLine();
out.println(num1);
System.out.println("Enter Number2:: ");
num2=read.readLine();
out.println(num2);
System.out.println("Product of two given Numbers are:: ");
System.out.println(in.readLine());
out.close();
in.close();
read.close();
socket.close();
}
}
OUTPUT:(SERVER)
Received
Number1::5
Received
Number2::5
5*5
=25
OUTPUT:(CLIENT)
Enter Number1::
5
Enter
Number2::
5
Product of two
given Numbers are::
25
No comments:
Post a Comment
Thanks for your valuable comments