Factorial TCP
Factorial TCP
BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
int port=5001;
try {
System.out.println("Client Started");
int choice=-1;
do
toServer.flush();
System.out.println(menu);
choice=Integer.parseInt(br.readLine());
toServer.println(choice);
toServer.flush();
switch(choice)
case 1:
System.out.println("ENTER A NO:");
int no=Integer.parseInt(br.readLine());
toServer.println(no);
int result=Integer.parseInt(fromServer.readLine());
System.out.println("FACTORIAL IS:"+result);
break;
case 2:
System.out.println("ENTER A NO:");
no=Integer.parseInt(br.readLine());
toServer.println(no);
result=Integer.parseInt(fromServer.readLine());
break;
case 3:
int stringChoice;
do
System.out.println(stringmenu);
stringChoice=Integer.parseInt(br.readLine());
toServer.println(stringChoice);
toServer.flush();
switch(stringChoice)
case 1:
System.out.println("ENTER A String:");
String str=br.readLine();
toServer.println(str);
result=Integer.parseInt(fromServer.readLine());
System.out.println("LENGTH IS:"+result);
break;
case 2:
String first_str=br.readLine();
toServer.println(first_str);
String second_str=br.readLine();
toServer.println(second_str);
String str_result=fromServer.readLine();
System.out.println(str_result);
break;
case 3:
first_str=br.readLine();
toServer.println(first_str);
second_str=br.readLine();
toServer.println(second_str);
str_result=fromServer.readLine();
break;
case 4:
System.out.println("ENTER A String:");
str=br.readLine();
toServer.println(str);
str_result=fromServer.readLine();
System.out.println("PALINDROME RESULT:"+str_result);
break;
case 5:
first_str=br.readLine();
toServer.println(first_str);
System.out.println("ENTER Second(it is checkd as substring) String:");
second_str=br.readLine();
toServer.println(second_str);
str_result=fromServer.readLine();
System.out.println("Substring Result:"+str_result);
break;
case 6:
break;
}while(stringChoice!=6);
break;
case 4:
break;
default:
System.out.println("INVALID CHOICE");
}while(choice!=4);
} catch (Exception e) {
}
import java.io.*;
import java.net.*;
/**
* @param args
*/
int port=5001;
try {
System.out.println("SERVER WAITING");
Socket clientSocket=serverSocket.accept();
while(true)
int choice=Integer.parseInt(fromClient.readLine());
System.out.print(choice);
switch(choice)
case 1:
int no=Integer.parseInt(fromClient.readLine());
int result=1;
for(int i=2;i<=no;i++)
result*=i;
toClient.flush();
toClient.println(result);
break;
case 2:
no=Integer.parseInt(fromClient.readLine());
result=0;
while(no!=0){
result+=no%10;
no/=10;
toClient.flush();
toClient.println(result);
break;
case 3:
int stringChoice;
do{
stringChoice=Integer.parseInt(fromClient.readLine());;
switch(stringChoice){
case 1:
String first_str=fromClient.readLine();
result=first_str.length();
toClient.flush();
toClient.println(result);
break;
case 2:
first_str=fromClient.readLine();
String second_str=fromClient.readLine();
result=first_str.compareTo(second_str);
String str_result="";
if (result ==0)
else if (result<0)
str_result=second_str+" is greater";
else
str_result=first_str+" is greater";
toClient.flush();
toClient.println(str_result);
break;
case 3:
first_str=fromClient.readLine();
second_str=fromClient.readLine();
result=first_str.compareTo(second_str);
str_result=first_str+second_str;
toClient.flush();
toClient.println(str_result);
break;
case 4:
first_str=fromClient.readLine();
int i,j;
for(i=0,j=first_str.length()-1;i<=j;i++,j--){
if(first_str.charAt(i)!=first_str.charAt(j))
break;
else
str_result="YES";
toClient.flush();
toClient.println(str_result);
break;
case 5:
first_str=fromClient.readLine();
second_str=fromClient.readLine();
result=first_str.indexOf(second_str);
if(result==-1)
str_result="NO";
else
str_result="YES";
toClient.flush();
toClient.println(str_result);
break;
case 6:
break;
}while(stringChoice!=6);
break;
case 4:
break;
} catch (Exception e) {