Java code to implement Math Server with multi client support using socket programming

Simple Java Math Server (Factorial, Fibonacci, Prime, and Palindrome) with multi client support using socket programming

Code:


Server:
package javaapplication7;

import java.io.*;

import java.net.*;

import java.util.Scanner;

import java.lang.String;



public class MyServer{

public static void main(String args[]){

try{



ServerSocket ss = new ServerSocket(1111);



Socket s = ss.accept();



DataInputStream dis = new DataInputStream(s.getInputStream());

DataOutputStream dout=new DataOutputStream(s.getOutputStream());



dout.writeUTF("Math Server");

dout.writeUTF("Enter your option from below:");

dout.writeUTF("1.Factorial of the number");

dout.writeUTF("2.Fibonacci series ");

dout.writeUTF("3.Check prime or not");

dout.writeUTF("4.Check the string is palindrome or not");



int option=Integer.parseInt(dis.readUTF());

switch(option){

case 1:

{

dout.writeUTF("Enter the number for factorial:");dout.flush();

int number=Integer.parseInt(dis.readUTF());

if(number<=0){dout.writeUTF("Negative number or 0 is not allowed");dout.flush();}

else{

int fact=1;

for(int i=1;i<=number;i++){

fact=fact*i;



}

dout.writeUTF("Factorial: "+fact);dout.flush();





}

break;

}





case 2:

{

dout.writeUTF("Enter the number of step for Fibonacci series:");dout.flush();

int number=Integer.parseInt(dis.readUTF());

int n1=0,n2=1,n3,i,count=number;

StringBuilder sb = new StringBuilder(256);

sb.append("Fibonacci Series: ").append("0 ").append("1 ");



for(i=2;i<count;++i)//loop starts from 2 because 0 and 1 are already printed

{

n3=n1+n2;

sb.append(Integer.toString(n3)+" ");

n1=n2;

n2=n3;

}

dout.writeUTF(sb.toString());dout.flush();

break;

}

case 3:

{

dout.writeUTF("Enter the number for Prime:");dout.flush();

int n=Integer.parseInt(dis.readUTF());

if(n<0){dout.writeUTF("Negative number not allowed");dout.flush();}

else{

int i,m=0,flag=0;

m=n/2;

if(n==0||n==1){

dout.writeUTF(n+" is not prime number"); dout.flush();

}else{

for(i=2;i<=m;i++){

if(n%i==0){

dout.writeUTF(n+" is not prime number");dout.flush();

flag=1;

break;

}

}

if(flag==0) { dout.writeUTF(n+" is prime number"); dout.flush();}

}

}

break;

}

case 4:

{

dout.writeUTF("Enter the String to be checked:");dout.flush();

String statement = dis.readUTF();

StringBuilder sb = new StringBuilder(statement);

sb.reverse();

if(statement.compareTo(sb.toString())!=0){dout.writeUTF("Not Pelindrome");dout.flush();}

else{dout.writeUTF("Pelindrome");dout.flush();}

break;

}

default:dout.writeUTF("Not Right Choice!\nPress Enter Key To Exit.");dout.flush();break;

}

}catch(Exception e){System.out.println(e);}

}//psvm

}//class



Client:


package javaapplication7;

import java.net.*;

import java.io.*;

import java.util.Scanner;



public class MyClient {

public static void main(String[] args){

try {

Socket s=new Socket("localhost",1111);

DataOutputStream dout=new DataOutputStream(s.getOutputStream());

DataInputStream dis = new DataInputStream(s.getInputStream());

Scanner sc = new Scanner(System.in);



for (int i = 0; i < 6; i++) {

System.out.println(dis.readUTF());

}



dout.writeUTF(sc.nextLine());

dout.flush();

System.out.println(dis.readUTF());

dout.writeUTF(sc.nextLine());

dout.flush();

System.out.println(dis.readUTF());

}//try

catch(Exception e)

{System.out.println(e);}

} //psvm

}//class

Comments

Popular posts from this blog

C program to evaluate Prefix Expression using Stack data structure

Java Program to Implement sorting algorithm using TCP on Server application

C++ program to perform data transformation Min-max and Z score Normalization