Question Label Internal 5 TCP
Write a Java program to implement client-server interaction using connection-oriented sockets.
The client must send a number to a server as a request. The server must process the request as a thread and return the factorial of the requested number as a response to the client.
If a negative number is sent from the client, the server must throw a user-defined exceptionnamed NegativeNumberException.
// It contains two classes : Server and
//ClientHandler
// Save file as Server.java
import java.io.*;
import java.net.*;
//Exception Class
class NegativeNumberException extends Exception {
private String op;
public NegativeNumberException(String op) {
this.op = op;
}
public String toString() {
return "NegativeNumberException: Operation Factorial is Denied by server";
}
}
// Server class
public class ServerFactorial
{
public static void main(String[] args) throws IOException
{
// server is listening on port 5080
ServerSocket ss = new ServerSocket(5080);
Socket s = null;
try
{
// socket object to receive incoming client requests
s = ss.accept();
// obtaining input and out streams
DataInputStream dis = new DataInputStream(s.getInputStream());
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
// create a new thread object
Thread t = new ClientHandle(s, dis, dos);
// Invoking the start() method
t.start();
}
catch (Exception e){
s.close();
e.printStackTrace();
}
}
}
// ClientHandler class
class ClientHandle extends Thread{
final DataInputStream dis;
final DataOutputStream dos;
final Socket s;
// Constructor
public ClientHandle(Socket s, DataInputStream dis, DataOutputStream dos)
{
this.s = s;
this.dis = dis;
this.dos = dos;
}
@Override
public void run()
{
String received;
String toreturn;
int num,count = 1;
try {
// Ask user what he wants
dos.writeUTF("Press any number to get Factorial..\n");
// receive the answer from client
received = dis.readUTF();
num=Integer.parseInt(received);
try{
if (num<0) {
throw new NegativeNumberException(received);
}
}catch(Exception e) {
toreturn=e.toString();
dos.writeUTF(toreturn);
System.out.println("Closing this connection.");
this.s.close();
}
if(num<=9) {
count=1;
for(int i=1;i<=num;i++) {
count=count*i;
}
// write the
// answer from the client
toreturn = "Factorial of "+num+"="+count;
dos.writeUTF(toreturn);
}
//Today's Modification Ma'am Asked
//If the number is Greather than 9 Step //Down It to One's Place And
// Find It one's place FFactorial
//Example: 24=>4 => Factorial Of 4=24
else {
count=1;
int n=num%10;
for(int i=1;i<=n;i++) {
count=count*i;
}
// write the
// answer from the client
toreturn="Step Down the num:"+num+" to: "+n+"\n Factorial="+count;
dos.writeUTF(toreturn);
}
Thread.sleep(1000);
}catch(Exception ef)
{
ef.printStackTrace();
}
try
{
// closing resources
this.dis.close();
this.dos.close();
}catch(IOException ee){
ee.printStackTrace();
}
}
}
Client Side:
import java.io.*;
import java.net.*;
import java.util.Scanner;
// Client class
public class ClientFactorial
{
public static void main(String[] args) throws IOException
{
try
{
Scanner scn = new Scanner(System.in);
// getting localhost ip
InetAddress ip = InetAddress.getByName("localhost");
// establish the connection with server port 5080
Socket s = new Socket(ip, 5080);
// obtaining input and out streams
DataInputStream dis = new DataInputStream(s.getInputStream());
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
System.out.println(dis.readUTF());
String tosend = scn.nextLine();
dos.writeUTF(tosend);
String received = dis.readUTF();
System.out.println(received);
}catch(Exception e){
e.printStackTrace();
}
}
}
Note: Green mark one is Modification Asked in B1 Lab Ia.
Write a Java program to implement client-server interaction using connection-oriented sockets.
The client must send a number to a server as a request. The server must process the request as a thread and return the factorial of the requested number as a response to the client.
If a negative number is sent from the client, the server must throw a user-defined exceptionnamed NegativeNumberException.
Server Side
// Java implementation of Server side // It contains two classes : Server and
//ClientHandler
// Save file as Server.java
import java.io.*;
import java.net.*;
//Exception Class
class NegativeNumberException extends Exception {
private String op;
public NegativeNumberException(String op) {
this.op = op;
}
public String toString() {
return "NegativeNumberException: Operation Factorial is Denied by server";
}
}
// Server class
public class ServerFactorial
{
public static void main(String[] args) throws IOException
{
// server is listening on port 5080
ServerSocket ss = new ServerSocket(5080);
Socket s = null;
try
{
// socket object to receive incoming client requests
s = ss.accept();
// obtaining input and out streams
DataInputStream dis = new DataInputStream(s.getInputStream());
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
// create a new thread object
Thread t = new ClientHandle(s, dis, dos);
// Invoking the start() method
t.start();
}
catch (Exception e){
s.close();
e.printStackTrace();
}
}
}
// ClientHandler class
class ClientHandle extends Thread{
final DataInputStream dis;
final DataOutputStream dos;
final Socket s;
// Constructor
public ClientHandle(Socket s, DataInputStream dis, DataOutputStream dos)
{
this.s = s;
this.dis = dis;
this.dos = dos;
}
@Override
public void run()
{
String received;
String toreturn;
int num,count = 1;
try {
// Ask user what he wants
dos.writeUTF("Press any number to get Factorial..\n");
// receive the answer from client
received = dis.readUTF();
num=Integer.parseInt(received);
try{
if (num<0) {
throw new NegativeNumberException(received);
}
}catch(Exception e) {
toreturn=e.toString();
dos.writeUTF(toreturn);
System.out.println("Closing this connection.");
this.s.close();
}
if(num<=9) {
count=1;
for(int i=1;i<=num;i++) {
count=count*i;
}
// write the
// answer from the client
toreturn = "Factorial of "+num+"="+count;
dos.writeUTF(toreturn);
}
//Today's Modification Ma'am Asked
//If the number is Greather than 9 Step //Down It to One's Place And
// Find It one's place FFactorial
//Example: 24=>4 => Factorial Of 4=24
else {
count=1;
int n=num%10;
for(int i=1;i<=n;i++) {
count=count*i;
}
// write the
// answer from the client
toreturn="Step Down the num:"+num+" to: "+n+"\n Factorial="+count;
dos.writeUTF(toreturn);
}
Thread.sleep(1000);
}catch(Exception ef)
{
ef.printStackTrace();
}
try
{
// closing resources
this.dis.close();
this.dos.close();
}catch(IOException ee){
ee.printStackTrace();
}
}
}
Client Side:
import java.io.*;
import java.net.*;
import java.util.Scanner;
// Client class
public class ClientFactorial
{
public static void main(String[] args) throws IOException
{
try
{
Scanner scn = new Scanner(System.in);
// getting localhost ip
InetAddress ip = InetAddress.getByName("localhost");
// establish the connection with server port 5080
Socket s = new Socket(ip, 5080);
// obtaining input and out streams
DataInputStream dis = new DataInputStream(s.getInputStream());
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
System.out.println(dis.readUTF());
String tosend = scn.nextLine();
dos.writeUTF(tosend);
String received = dis.readUTF();
System.out.println(received);
}catch(Exception e){
e.printStackTrace();
}
}
}
Note: Green mark one is Modification Asked in B1 Lab Ia.