Finding class of the IP address

Finding class of the IP address


Client:
import java.io.*;
import java.net.*;
import java.util.*;
class Clientip
{
            public static void main(String args[])
            {
            try
            {
                        BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
                        Socket clsct=new Socket("127.0.0.1",139);
                        DataInputStream din=new DataInputStream(clsct.getInputStream());
                        DataOutputStream dout=new DataOutputStream(clsct.getOutputStream());

                        System.out.println("Enter the ip address");
                        String str=in.readLine();
                        dout.writeBytes(str+'\n');
                        String str1=din.readLine();
                        System.out.println("The IP is of: "+str1);
                        clsct.close();
            }
            catch (Exception e)
            {
            System.out.println(e);
            }
            }
}

Server:
import java.io.*;
import java.net.*;
import java.util.*;
class Serverip
{
            public static void main(String args[])
            {
            try
            {
                        ServerSocket obj=new ServerSocket(139);
                        while(true)
                        {         
                                    int a[]=new int[4];
                                    Socket obj1=obj.accept();
                                    DataInputStream din=new DataInputStream(obj1.getInputStream());
                                    DataOutputStream dout=new DataOutputStream(obj1.getOutputStream());
                                    String str=din.readLine();
                                    char c[]=new char[4];
                                    str.getChars(0,3,c,1);
                                    for(int i=1;i<=3;i++)
                                    {
                                                a[i]=(int)c[i];
                                                a[i]-=48;
                                    }
                                    String s="Class A";
                                    String s1="Class B";
                                    String s2="Class C";
                                    String s3="Class D";
                                    String s4="Class E";
                                    if(a[1]==0)
                                                dout.writeBytes(s+'\n');
                                    else if(a[1]==1)
                                                {
                                                            if(a[2]==0||a[2]==1||a[2]==2)
                                                            {
                                                                        if(a[2]==2&&a[3]>7)
                                                                                    dout.writeBytes(s1+'\n');
                                                                        else
                                                                                    dout.writeBytes(s+'\n');
                                                            }
                                                            else if(a[2]>2)
                                                            {
                                                                        if(a[2]==9&&a[3]>1)
                                                                                    dout.writeBytes(s2+'\n');
                                                                        else
                                                                                    dout.writeBytes(s1+'\n');
                                                            }
                                                }
                                    else if(a[1]==2)
                                                {
                                                            if(a[2]==0||a[2]==1||a[2]==2)
                                                            {
                                                                        if(a[2]==2&&a[3]>3)
                                                                                    dout.writeBytes(s3+'\n');
                                                                        else
                                                                                    dout.writeBytes(s2+'\n');
                                                            }
                                                            else if(a[2]==3)
                                                                        dout.writeBytes(s3+'\n');
                                                            else if(a[2]==4||a[2]==5)
                                                            {
                                                                        if(a[2]==5&&a[3]<=5)
                                                                                    dout.writeBytes(s4+'\n');
                                                                        else
                                                                                    dout.writeBytes(s4+'\n');
                                                            }
                                                }                     
                                    System.out.println(str);
                        }
            }
            catch(Exception e)
            {
                        System.out.println(e);
            }
            }
}
Output:
Client
Enter the ip address
165.165.1.34
Server
165.165.1.34
Client
The IP is of: Class B

No comments:

Post a Comment

Creating Objects

Creating Objects                  Creating objects means to allocate memory space for all the instance variables of the objects. S...