SL5Exp4

client.java

/* right click in background->run as -> run configurations-> arguments->
->program arguments->type localhost (or ip address of server)->Run */
package exp4;
import java.net.*;
import java.io.*;
public class client {
public static void main (String args[]) {

Socket s = null;
int n;
int ch,ch1;
String st,st1,st2,first,last;

try{
int serverPort = 7896;
s = new Socket(args[0], serverPort);
DataInputStream in = new DataInputStream( s.getInputStream());
DataOutputStream out =new DataOutputStream( s.getOutputStream());
BufferedReader object = new BufferedReader(new InputStreamReader(System.in));

do
{

System.out.println(“1.Factorial\n2.Adddition of digits\n3.String operations\n4.Exit\nEnter ur choice”);
ch= Integer.parseInt(object.readLine());
out.writeUTF(Integer.toString(ch));
switch(ch)
{
case 1:

System.out.println(“Enter a number”);
n= Integer.parseInt(object.readLine());

out.writeUTF(Integer.toString(n));
int fact =Integer.parseInt(in.readUTF());
System.out.println(“Factorial of “+n+”is “+fact) ;

break;
case 2:

//out.writeUTF(Integer.toString(ch));
System.out.println(“Enter a number”);
n= Integer.parseInt(object.readLine());
out.writeUTF(Integer.toString(n));
int sum =Integer.parseInt(in.readUTF());
System.out.println(“Addition of digits of “+n+” is “+sum) ;

break;

case 3:
do
{
System.out.println(“1.concatenation\n2.substring\n3.palindrome\n4.Exit\nEnter ur choice”);
ch1= Integer.parseInt(object.readLine());
out.writeUTF(Integer.toString(ch1));
switch(ch1)
{
case 1:
System.out.println(“Enter First string”);
st1=object.readLine();
out.writeUTF(st1);

System.out.println(“Enter second string”);
st2=object.readLine();
out.writeUTF(st2);

st=in.readUTF();
System.out.println(“Concatenated String of “+st1+” and “+st2+” is ::  “+st) ;
break;

case 2:
System.out.println(“Enter The string”);
st1=object.readLine();
out.writeUTF(st1);

System.out.println(“Enter The Start Position of the substring”);
first=object.readLine();
out.writeUTF(first);

System.out.println(“Enter The end Position of the substring”);
last=object.readLine();
out.writeUTF(last);

st=in.readUTF();
System.out.println(“Substring of string “+st1+” is ::  “+st) ;
break;
case 3:
System.out.println(“Enter The string”);
st=object.readLine();
out.writeUTF(st);

n=Integer.parseInt(in.readUTF());

if(n==0)
System.out.println(“string”+st+” is Palindrome”) ;

else
System.out.println(“string”+st+” is not Palindrome”) ;

break;

}
}while(ch1>0 && ch1!=4);
break;

case 4:
System.exit(0);
break;
}
}while(ch>0);

}
catch (UnknownHostException e)
{
System.out.println(“Socket:”+e.getMessage());
}
catch (EOFException e)
{
System.out.println(“EOF:”+e.getMessage());
}catch (IOException e)
{
System.out.println(“readline:”+e.getMessage());
}
finally {if(s!=null) try {s.close();}
catch (IOException e)
{System.out.println(“close:”+e.getMessage());}}
}
}

 

server.java

// right click in background->run as ->java application
package exp4;
import java.net.*;
import java.io.*;
public class server {

public server() {
System.out.println(“\nServer is Reday”) ;

}

public static void main (String args[]) {
try{
int serverPort = 7896; // the server port

ServerSocket listenSocket = new ServerSocket(serverPort);
while(true) {
Socket clientSocket = listenSocket.accept();
Connection c = new Connection(clientSocket);
}
} catch(IOException e) {System.out.println(“Listen socket:”+e.getMessage());}
}
}
class Connection extends Thread {
DataInputStream in;
DataOutputStream out;
Socket clientSocket;
int n,ch,ch1,f,l;
String st,st1,st2;

public Connection (Socket aClientSocket) {
try {
clientSocket = aClientSocket;
in = new DataInputStream( clientSocket.getInputStream());
out =new DataOutputStream( clientSocket.getOutputStream());

this.start();
} catch(IOException e) {System.out.println(“Connection:”+e.getMessage());}
}
public void run(){
try {
while(true)

{
ch =Integer.parseInt(in.readUTF());
switch(ch)
{
case 1:
int fact= 1;
n =Integer.parseInt(in.readUTF());

for (int i= 1; i<=n; i++)
{
fact=fact*i;
}
out.writeUTF(Integer.toString(fact));
break;

case 2:

int sum=0,rem;

n =Integer.parseInt(in.readUTF());

while(n!=0)
{
rem = n%10;
n = n/10;
sum=sum + rem;
}
out.writeUTF(Integer.toString(sum));
break;

case 3:

while(true)

{
ch1 =Integer.parseInt(in.readUTF());
switch(ch1)
{
case 1:

st1=in.readUTF();
st2=in.readUTF();
st=st1.concat(st2);

out.writeUTF(st);

break;

case 2:

st1=in.readUTF();
f =Integer.parseInt(in.readUTF());
l =Integer.parseInt(in.readUTF());
st=st1.substring(f,l);
out.writeUTF(st);
st=null;
break;
case 3:
st=in.readUTF();
st1 = new StringBuffer(st).reverse().toString();
n=st.compareTo(st1);
out.writeUTF(Integer.toString(n));

break;

}
}

}

}

}catch (EOFException e){System.out.println(“EOF:”+e.getMessage());
} catch(IOException e) {System.out.println(“readline:”+e.getMessage());
}finally{ try {clientSocket.close();}catch (IOException e){/*close failed*/}}

}
}


Archives