Tuesday, 27 March 2012

Program – 6


Program – 6
Client
import java.io.*;
import java.net.*;
class cli
{
public static void main(String ar[])
   {
try
    {
       Socket s=new Socket("localhost",1111);
DataInputStream in=new DataInputStream(System.in);
PrintStream p=new PrintStream(s.getOutputStream());
       String str;
System.out.println("Enter Expression");
str=in.readLine();
p.println(str);
    }
catch(Exception e)
    {
    }
  }
 }

Server-1
import java.io.*;
import java.net.*;
importjava.util.*;
class ser1
{
public static void main(String ar[])
   {
try
     {
ServerSocketss=new ServerSocket(1111);
       Socket s=ss.accept();
StringTokenizer t1,t2,t3,t4;
       String str;
       Socket s1=new Socket("localhost",1112);
       Socket s2=new Socket("localhost",1113);
PrintStream p1=new PrintStream(s1.getOutputStream());
PrintStream p2=new PrintStream(s2.getOutputStream());
DataInputStream din=new DataInputStream(s.getInputStream());
str=din.readLine();
       t1=new StringTokenizer(str,"+");
       t2=new StringTokenizer(str,"-");
       t3=new StringTokenizer(str,"*");
       t4=new StringTokenizer(str,"/");
if((t1.countTokens()>1)||(t2.countTokens()>1))
       {
System.out.println("Pluse or Minus");
p1.println(str);
       }
if((t3.countTokens()>1)||(t4.countTokens()>1))
       {
System.out.println("Multiply or Division");
p2.println(str);
       }

     }
catch(Exception e)
     {
     }
   }
}

Server-2
import java.io.*;
import java.net.*;
importjava.util.*;
class ser2
{
public static void main(String ar[])
  {
try
  {
ServerSocketss=new ServerSocket(1112);
    Socket s=ss.accept();
DataInputStream din=new DataInputStream(s.getInputStream());
    String str,str1,str2;
StringTokenizer t1,t2;
inti,j,k;
str=din.readLine();
    t1=new StringTokenizer(str,"+");
    t2=new StringTokenizer(str,"-");
if(t1.countTokens()>1)
    {
System.out.println("Pluse");
     str1=t1.nextToken();
     i=Integer.parseInt(str1);
System.out.println("Op1  "+i);
     str2=t1.nextToken();
     j=Integer.parseInt(str2);
System.out.println("Op2  "+j);
     k=i+j;
System.out.println("Result is "+k);
    }
if(t2.countTokens()>1)
    {
System.out.println("Minus");
     str1=t2.nextToken();
     i=Integer.parseInt(str1);
System.out.println("Op1"+i);
     str2=t2.nextToken();
     j=Integer.parseInt(str2);
System.out.println("Op2"+j);
     k=i-j;
System.out.println("Result is "+k);  
    }
  }
catch(Exception e)
  {
  }
 }
}

Server-3
import java.io.*;
importjava.util.*;
import java.net.*;
class ser3
{
public static void main(String ar[])
  {
try
  {
ServerSocketss=new ServerSocket(1113);
    Socket s=ss.accept();
DataInputStream din=new DataInputStream(s.getInputStream());
    String str,str1,str2;
StringTokenizer t1,t2;
inti,j,k;
str=din.readLine();
    t1=new StringTokenizer(str,"*");
    t2=new StringTokenizer(str,"/");
if(t1.countTokens()>1)
    {
System.out.println("Multiply");
     str1=t1.nextToken();
     i=Integer.parseInt(str1);
System.out.println("Op1:  "+i);
     str2=t1.nextToken();
     j=Integer.parseInt(str2);
System.out.println("Op2 : "+j);
     k=i*j;
System.out.println("Result is :"+k);
    }
if(t2.countTokens()>1)
    {
System.out.println("Divide");
     str1=t2.nextToken();
     i=Integer.parseInt(str1);
System.out.println("Op1 :"+i);
     str2=t2.nextToken();
     j=Integer.parseInt(str2);
System.out.println("Op2 :"+j);
     k=i/j;
System.out.println("Result is "+k); 
    }
  }
catch(Exception e)
  {
  }
 }
}

No comments:

Post a Comment