Interviews Questions, Algorithms, Aptitude, C Interview Program, C Theory Question, Aptitude Tricks, Test Series,

Saturday 6 April 2019

UDP Factorial

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:

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); } }

Credits: Harshita

1 comment:

  1. Alternative

    Server


    package socket;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;

    class ServerThread extends Thread{
    int n;
    DatagramSocket s;
    public ServerThread(int n,DatagramSocket s) {
    // TODO Auto-generated constructor stub
    this.n=n;
    this.s=s;
    }
    @Override
    public void run() {
    int res=1;
    try {
    if(n<0)
    throw new NegativeNumberException();
    while(n!=0) {
    res*=n--;
    }
    byte[] b2=String.valueOf(res).getBytes();
    DatagramPacket d1=new DatagramPacket(b2, b2.length,InetAddress.getLocalHost(),3334);
    s.send(d1);
    System.out.println("ans = " + res);
    }catch (Exception e) {
    System.out.println(e);
    }
    }
    }
    public class Udp {
    public static void main(String[] args){
    try {
    DatagramSocket d=new DatagramSocket(3333);
    byte[] buf=new byte[1024];
    DatagramPacket p=new DatagramPacket(buf, buf.length);
    d.receive(p);
    String s=new String(p.getData(), 0, p.getLength());
    int n=Integer.parseInt(s);
    new ServerThread(n,d).start();

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

    }
    class NegativeNumberException extends Exception{
    @Override
    public String toString() {
    return "NegativeNumberException";
    }
    }



    Client

    package socket;

    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    import java.util.Scanner;

    public class UdpClient {
    public static void main(String[] args) {
    try{
    DatagramSocket s=new DatagramSocket(3334);
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter number");
    byte[] b=String.valueOf(sc.nextLine()).getBytes();
    DatagramPacket d=new DatagramPacket(b, b.length, InetAddress.getLocalHost(),3333);
    s.send(d);
    byte[] b2=new byte[1024];
    DatagramPacket r=new DatagramPacket(b2,b2.length);
    s.receive(r);
    System.out.println("Recived = " + new String(r.getData(),0,r.getLength()));
    }catch (Exception e) {
    // TODO: handle exception
    System.out.println(e);
    e.printStackTrace();
    }

    }
    }

    ReplyDelete