Question
Write a Java program to implement client-server interaction using connectionless 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 exception named NegativeNumberException.
Solution:
Credits: Harshita
Write a Java program to implement client-server interaction using connectionless 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 exception named NegativeNumberException.
Solution:
Server Side:
import java.net.*;
public class UDPserver {
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
try{
new thread().start();
}catch(Exception e)
{
e.printStackTrace();
}
}
private static class thread extends Thread{
public void run()
{
try{
DatagramSocket ds=new DatagramSocket(9999);
byte[] b1=new byte[1024];
DatagramPacket dp=new DatagramPacket(b1,b1.length);
ds.receive(dp);
String str=new String(dp.getData(),0,dp.getLength());
int num=Integer.parseInt(str.trim());
int result=1;
try{
if(num>0)
{
for(int i=1;i<=num;i++)
{
result=result*i;
}
}
else
{
throw new NegativeNumberException("it is negative number");
}
}catch(Exception e)
{
e.printStackTrace();
System.exit(0);
}
InetAddress ia=InetAddress.getLocalHost();
byte[] b2=String.valueOf(result).getBytes();
DatagramPacket dp1=new DatagramPacket(b2,b2.length,ia,dp.getPort());
ds.send(dp1);
}catch(Exception x)
{
}
}
}
}
class NegativeNumberException extends Exception
{
String msg;
NegativeNumberException(String msg)
{
this.msg=msg;
}
public String toString()
{
return msg;
}
}
Client Side:
import java.net.*;
import java.util.Scanner;
public class UDPclient {
public static void main(String[] args)throws Exception {
// TODO Auto-generated method stub
Scanner s=new Scanner(System.in);
DatagramSocket ds=new DatagramSocket();
System.out.println("Enter The Number Whose Factorial u Need");
int i=s.nextInt();
byte[] b=String.valueOf(i).getBytes();
InetAddress ia=InetAddress.getLocalHost();
DatagramPacket dp=new DatagramPacket(b,b.length,ia,9999);
ds.send(dp);
byte[] b1=new byte[1024];
DatagramPacket dp1=new DatagramPacket(b1,b1.length);
ds.receive(dp1);
String str=new String(dp1.getData(),0,dp1.getLength());
System.out.println("the Factorial result is "+str); } }
Scanner s=new Scanner(System.in);
DatagramSocket ds=new DatagramSocket();
System.out.println("Enter The Number Whose Factorial u Need");
int i=s.nextInt();
byte[] b=String.valueOf(i).getBytes();
InetAddress ia=InetAddress.getLocalHost();
DatagramPacket dp=new DatagramPacket(b,b.length,ia,9999);
ds.send(dp);
byte[] b1=new byte[1024];
DatagramPacket dp1=new DatagramPacket(b1,b1.length);
ds.receive(dp1);
String str=new String(dp1.getData(),0,dp1.getLength());
System.out.println("the Factorial result is "+str); } }
Credits: Harshita