Archive for 2015

Inner Classes

Sunday 5 July 2015
Posted by Unknown

class Outer 
{
     int outer_x = 100;
     void test() 
     {
          Inner inner = new Inner();
          inner.display();
     }
     
     // this is an inner class
     class Inner 
     {
          void display() 
          {
                System.out.println("Display: outer_x = " + outer_x);
          }
     }
}

class InnerClassDemo 
{
      public static void main(String args[])  
      {
           Outer outer = new Outer();
           outer.test();
      }
}

Output:

Display: outer_x = 100

class Stack

{
     private int stck[];
     private int tos;
     // allocate and initialize stack
     Stack(int size) 
     {
          stck = new int[size];
          tos = -1;
     }
     
     // Push an item onto the stack
     void push(int item) 
     {
          if(tos==stck.length-1) // use length member
          System.out.println("Stack is full.");
          else
          stck[++tos] = item;
     }
    
     // Pop an item from the stack
     int pop() 
     {
          if(tos < 0) 
          {
                System.out.println("Stack underflow.");
                return 0;
          }
          else
               return stck[tos--];
          }
     }

class TestStack 
{
       public static void main(String args[]) 
       {
            Stack mystack1 = new Stack(5);
            Stack mystack2 = new Stack(8);
            // push some numbers onto the stack
            for(int i=0; i<5; i++) mystack1.push(i);
            for(int i=0; i<8; i++) mystack2.push(i);
            // pop those numbers off the stack
            System.out.println("Stack in mystack1:");
            for(int i=0; i<5; i++)
            System.out.println(mystack1.pop());
            System.out.println("Stack in mystack2:");
            for(int i=0; i<8; i++)
            System.out.println(mystack2.pop());
       }
}

Output:

Stack in mystack1:
4
3
2
1
0
Stack in mystack2:
7
6
5
4
3
2
1
0


import java.util.Stack;
class TestStack 
{
      public static void main(String args[]) 
      {
            Stack mystack1 = new Stack();
            Stack mystack2 = new Stack();
                  // push some numbers onto the stack
            for(int i=0; i<10; i++)
                  mystack1.push(i);
            for(int i=10; i<20; i++)
                  mystack2.push(i);
                  // pop those numbers off the stack
            System.out.println("Stack in mystack1:");
                  for(int i=0; i<10; i++)
            System.out.println(mystack1.pop());
           
            System.out.println("Stack in mystack2:");
                  for(int i=0; i<10; i++)
            System.out.println(mystack2.pop());
      }
}

Output:

Stack in mystack1:
9
8
7
6
5
4
3
2
1
0
Stack in mystack2:
19
18
17
16
15
14
13
12
11
10

class Factorial 
{
     int fact(int n) 
     {
          int result;
          if(n==1) return 1;
          result = fact(n-1) * n;
          return result;
     }
}

class Recursion 
{
     public static void main(String args[]) 
     {
          Factorial f = new Factorial();
          System.out.println("Factorial of 4 is " + f.fact(4));
     }
}

Output:

Factorial of 4 is 24
class ContinueLabel 
{
     public static void main(String args[]) 
     {
         outer: for (int i=0; i<10; i++) 
         {
             for(int j=0; j<10; j++) 
             {
                  if(j > i) 
                  {
                       System.out.println();
                       continue outer;
                  }
                  System.out.print(" " + (i * j));
             }
         }
     System.out.println();
     }
}

Output:

class Continue 
{
     public static void main(String args[]) 
     {
          for(int i=0; i<10; i++) 
          {
                System.out.print(i + " ");
                if (i%2 == 0) continue;
                System.out.println("");
          }
     }
}

Output:

Nested for Loops

Posted by Unknown
Tag :
class Nested 
{
     public static void main(String args[]) 
     {
        int i, j;
        for(i=0; i<10; i++) 
        {
             for(j=i; j<10; j++)
             System.out.print(".");
             System.out.println();
        }
     }
}

Output:


Display Hello world using simple Applet Program

Thursday 2 July 2015
Posted by Unknown
Tag :
import java.applet.Applet;
public class applet extends Applet
{
    public void paint(Graphics g)
    {
        g.drawString("Hello World!",50,50);
    }
}

Output:

import java.applet.Applet;
import java.awt.Graphics;
public class applet extends Applet
{
    @Override
    public void paint(Graphics g) 
    {
          g.drawArc(10,10,50,50,90,180);
          g.fillArc(60,10,60,60,90,180);
    }
}

Output:

 

 ====================================

import java.applet.Applet;
import java.awt.Graphics;
public class applet extends Applet
{
    @Override
    public void paint(Graphics g) 
    {
         g.drawArc(10,30,120,40,25,130);
         g.fillArc(10,80,180,80,25,-110);
    }
}

Output:

import java.applet.Applet;
import java.awt.Graphics;
public class applet extends Applet
{
    @Override
    public void paint(Graphics g) 
    {
         g.drawOval(10,10,30,30);
         g.fillOval(50,50,80,40);
         g.fillOval(150,110,30,60);
    }
}

Output:

import java.applet.Applet;
import java.awt.Graphics;
public class applet extends Applet
{
       public void paint(Graphics g) 
       {
            int x[] = { 55,64,21,212,498,34 };
            int y[] = { 45,14,64,10,120,50 };
            int p = x.length;
            g.fillPolygon(x,y,p);
       }
}

Output:

import java.applet.Applet;
import java.awt.Graphics;
public class applet extends Applet
{
       public void paint(Graphics g) 
       {
            int x[] = { 55,64,21,212,498,34 };
            int y[] = { 45,14,64,10,120,50 };
            int p = x.length;
            g.drawPolygon(x,y,p);
       }
}

Output:

import java.applet.Applet;
import java.awt.Graphics;
public class applet extends Applet
{
      public void paint(Graphics g) 
      {
          g.drawRoundRect(10,10,50,50,20,20);
          g.fillRoundRect(70,10,90,90,20,20);
      }
}

Output:

import java.applet.Applet;
import java.awt.Graphics;
public class applet extends Applet
{
    public void paint(Graphics g) 
    {
        g.drawRect(10,10,80,80);
        g.fillRect(30,30,50,50);
    }
}

Output:

Draw Lines using Applet

Posted by Unknown
Tag :
import java.applet.Applet;
import java.awt.Graphics;
public class applet extends Applet
{
    public void paint(Graphics g) 
    {
        g.drawLine(10,10,160,160);
    }
}

Output:

String methods

Sunday 28 June 2015
Posted by Unknown

class StringDemo
{
public static void main (String args[])
{
String str = "I am vijay";
System.out.println("The string is: " + str);
System.out.println("The string in upper case: " + str.toUpperCase());
System.out.println("Length of this string: " + str.length());
System.out.println("The character at position 5: "+ str.charAt(5));
System.out.println("The index of the character y: " + str.indexOf('y'));
System.out.println("The substring from 2 to 10: " + str.substring(2, 10));
System.out.println("The index of the beginning of the substring vijay: " + str.indexOf("vijay"));
}
}

Output:

The string is: I am vijay
The string in upper case: I AM VIJAY
Length of this string: 10
The character at position 5: v
The index of the character y: 9
The substring from 2 to 10: am vijay
The index of the beginning of the substring vijay: 5

class arithmetic
{
    public static void main(String args[])
    {
        short x = 8;
        int y = 2;
        float a = 10.5f;
        float b = 6f;
        System.out.println("Addition: "+(x+y));
        System.out.println("Subtraction: "+(x-y));
        System.out.println("Multiplication: "+(x*y));
        System.out.println("Division: "+(x/y));
        System.out.println("Modulus: "+(x%y));
        System.out.println("Floating point Division: "+(a/b));
    }
}

Output:

Addition: 10
Subtraction: 6
Multiplication: 16
Division: 4
Modulus: 0
Floating point Division: 1.75

Print Odd Numbers

Posted by Unknown
Tag :

class printOdd
{
    public static void main(String args[])
    {
        for(int x=1; x<10; x=x+2)
        {
        System.out.println(x);
        }
    }
}

Output:

1
3
5
7
9

Print Even Numbers

Posted by Unknown
Tag :

class printEven
{
    public static void main(String args[])
    {
        for(int x=0; x<10; x=x+2)
        {
        System.out.println(x);
        }
    }
}

Output:

8

The for Loop

Posted by Unknown
Tag :

Syntax:

for(initialization; condition; iteration) 
statement;

Program:

class printNumber
{
    public static void main(String args[])
    {
        for(int x=0; x<10; x++)
        {
        System.out.println(x);
        }
    }
}
 

Output:

0
1
2
3
4
5
6
7
8
9

Syntax:

if(condition 1)
statement 1;
else if (condition 2)
statement 2;
else if (condition 3)
statement 3;
else 
statement 4;

Grade System Program:

import java.util.Scanner;
class result
{
    public static void main(String[] args)
    {
           int mark;
           Scanner s=new Scanner(System.in);
           System.out.print("Enter your mark: ");                   
           mark=s.nextInt();
           if ( mark >= 90 )
           {
            System.out.println( "Grade A" );
           }
           else if ( mark >= 80 )
           {
            System.out.println( "Grade B" );
           }
           else if ( mark >= 70 )
           {
            System.out.println( "Grade C" );
           }
           else if ( mark >= 60 )
           {
            System.out.println( "Grade D" );
           }
           else if ( mark >= 50 )
           {
            System.out.println( "Grade E" );
           }
           else if ( mark >= 40 )
           {
            System.out.println( "Grade F" );
           }
           else 
           {
            System.out.println( "Failed" );
           }    
    }
}

Output:

Enter your mark: 56 
Grade E 

Enter your mark: 10 
Failed

Syntax:

if(condition)
statement 1;
else
statement 2;

Program:

import java.util.Scanner;
class result
{
    public static void main(String[] args)
    {
           int mark;
           Scanner s=new Scanner(System.in);
           System.out.print("Enter your mark: ");                   
           mark=s.nextInt();
           if ( mark <= 60 )
           {
                System.out.println("Fail");
           }
            else
           {
                System.out.println("Pass");
           }
    }
}

Output:

Enter your mark: 23
Fail
Enter your mark: 56
Pass

Syntax:

if(condition)
statement;

Program:

import java.util.Scanner;
class result
{
    public static void main(String[] args)
    {
           int mark;
           Scanner s=new Scanner(System.in);
           System.out.print("Enter your mark: ");                   
           mark=s.nextInt();
           if ( mark >= 40 )
           {
           System.out.println("Pass");
           }
    }
}

Output:

Enter your mark: 44 
Pass

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class palindrome
{
    public static void main(String[] args)
    {
        try
   {
            System.out.print("Enter a number: ");
  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            String num=br.readLine();
            int no=Integer.parseInt(num);
            int temp=no;
            int digit=0,ld;
            while(no!=0)
            {
                ld=no%10;
                digit=digit*10+ld;
                no=no/10;
            }
            if(temp==digit)
            {
                System.out.println("Palindrome number");
            }
            else
     {
                System.out.println("Not a palindrome number");
            }
        }
   catch (IOException | NumberFormatException ex)
   {
       }
    }
}

Output:

Enter a number: 123
Not a palindrome number

Enter a number: 12321
Palindrome number
 

import java.io.BufferedReader;
import java.io.InputStreamReader;
class Reverse
{
    public static void main(String[] args)
    {
        try
   {
            System.out.print("Enter string : ");
  BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            String str=in.readLine();
            for(int i=str.length()-1;i>=0;i--)
            {
              System.out.print(str.charAt(i));
            }
       }
   catch (Exception ex)
   {
      }
    }
}

Output:

Enter string : VIJAY 
YAJIV
import java.util.Scanner;
class fibonacci
{
  public static void main(String[] args)
    {
        System.out.print("Enter the number to display fibonicci series:  ");
        Scanner in=new Scanner(System.in);
        int num=in.nextInt();
        int a=0,b=1,c=0,i=0;
        while(i!=num)
        {
            System.out.println(c);
            a=b;
            b=c;
            c=a+b;
            i++;         
        }
    }
}

Output:

import java.util.Scanner;
class pyramidShape
{
 public static void main(String[] args)
   {
     System.out.print("Enter number of rows: ");
     Scanner in = new Scanner(System.in);
     int num=in.nextInt();
     for(int i=1;i<=num;i++)
     {
          for(int j=num;j>=i;j--)
          {
           System.out.print(" ");
          }
          for(int m=1;m<=i*2-1;m++)
          {
           System.out.print("*");
           }
      System.out.print("\n");
     }             
   }
}

Output:


import java.util.Scanner;
class diamondShape
{
      public static void main(String[] args)
    {
        System.out.print("Enter number of rows: ");
        Scanner in = new Scanner(System.in);
        int num=in.nextInt();
        for(int i=1;i<=num;i++)
        {
           for(int j=num;j>=i;j--)
           {
            System.out.print(" ");
           }
           for(int m=1;m<=i;m++)
           {
           System.out.print(" *");
           }
       System.out.print("\n");
       }    
       for(int i=1;i<=num;i++)
       {
           for(int j=1;j<=i;j++)
           {
           System.out.print(" ");
           }
           for(int m=num;m>=i;m--)
           {
           System.out.print(" *");
           }
        System.out.print("\n");
        }   
    }
   
}

Output:


Sender Side Program

import java.io.*;
import java.net.*;
class slidingSender
{
    public void process()
    {
        try
        {
            ServerSocket server= new ServerSocket(2500);
            Socket client = server.accept();
            BufferedReader user=new BufferedReader(new InputStreamReader(System.in));
            BufferedReader br=new BufferedReader(new InputStreamReader(client.getInputStream()));
            PrintWriter pr=new PrintWriter(client.getOutputStream(),true);
            String buf[]=new String[8];
            int index=0,sws=8,rws;
            String ch;
            do
            {
                System.out.println("Enter the number of frame send to the receiver:");
                int nf=Integer.parseInt(user.readLine());
                pr.println(nf);
                if(nf<=sws-1)
                {
                    System.out.println("Enter the "+nf+" Message send to the receiver");
                    for(int i=0;i<nf;i++)
                    {
                        buf[i]=user.readLine();
                        pr.println(buf[i]);

                    }
                   
                    sws=sws-nf;
                    int ackno=Integer.parseInt(br.readLine());
                    System.out.println("Acknowledge Received For the Frame"+ackno);
                    sws=sws+nf;

                }
                else
                {
                    System.out.println("SENDER WINDOW SIZE EXCEEDS");
                }
                System.out.println("Do You Want To Continue(y/n)");
                ch=user.readLine();
                pr.println(ch);
            }
            while(ch.equals("y"));
        }
        catch(Exception e){}
    }
    public static void main(String arg[])
    {
        slidingSender s= new slidingSender();
        s.process();
    }

}

Receiver Side Program

import java.io.*;
import java.net.*;

class slidingReceiver
{
    public void process()
    {
        try
        {
            Socket client = new Socket(InetAddress.getLocalHost(),2500);
            BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream()));
            BufferedReader user=new BufferedReader(new InputStreamReader(System.in));
            PrintWriter pr=new PrintWriter(client.getOutputStream(),true);
            int index=0,rws=8;
            String buf[]=new String[8];
            String ch;
            while(true)
            {
                do
                {
                    int nf=Integer.parseInt(br.readLine());
                    if(nf<rws-1)
                    {
                        for(int i=0;i<nf;i++)
                        {
                            buf[index]=br.readLine();
                            System.out.println(index);
                            System.out.println(buf[index]);
                            index=(index +1)%rws;
                            System.out.println(index);
                        }
                        rws = rws - nf;                      
                        System.out.println("Acknowledge Sent To Sender");
                        pr.println(index);
                        rws=rws+nf;
                    }
                    else
                    {
                        break;
                    }
                    ch=br.readLine();
                }
                while(ch.equals("y"));
            }
        }
        catch(Exception e){}
    }
    public static void main(String arg[])
    {
        slidingReceiver r= new slidingReceiver();
        r.process();
    }
}

How to run:

  1. First run Sender program 
  2. Then next run Receiver program
  3. Give input from Sender side

Sender Side Output:

Enter the number of frame send to the receiver:
3
Enter the 3 Message send to the receiver
hi
good
evening
Acknowledge Received For the Frame3
Do You Want To Continue(y/n)
n

Receiver Side Output:

0 
hi 
good 
evening 
Acknowledge Sent To Sender 


Multicast Sender

import java.net.*;
import java.io.*;

class MulticastSender implements Runnable
{
    static int port;
    static MulticastSocket ms;
    static String group;
    static int status = 0 ;
    MulticastSender() throws Exception
    {
        port = 5000;
        group = "225.4.5.6";
        ms = new MulticastSocket(4000);
    }
    public void run()
    {
        while(true)
        {
            try
            {
                BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                byte sbuf[] = new byte[2000];
                sbuf = br.readLine().getBytes();
                DatagramPacket spack = new DatagramPacket(sbuf, sbuf.length,
                                                 InetAddress.getByName(group), port);
                ms.send(spack);                                                
               
            }catch(Exception e){ System.out.println(e);}
        }
    }
    public static void main(String args[]) throws Exception
    {
        MulticastSender sender = new MulticastSender();
        Thread t = new Thread(sender);
               
        ms.joinGroup(InetAddress.getByName(group));
        t.start();
       
        int count =0;
        byte rbuf[] = new byte[1000];
        while(true)
        {
            DatagramPacket rpack =new DatagramPacket(rbuf,rbuf.length);
            ms.receive(rpack);
            String str = new String(rpack.getData(),0,rpack.getLength());
            if(str.equalsIgnoreCase("join"))
            {
                count++;
                System.out.println("Client joined the group");
                System.out.println("\ncurrently " + count + " client/clients is/are in the group");
               
            }
            else if(str.equalsIgnoreCase("leave"))
            {
                count--;
                System.out.println("Client left the group");
                System.out.println("\ncurrently " + count + " client/clients is/are in the group");
                if(count == 0)
                {
                    ms.leaveGroup(InetAddress.getByName(group));
                    ms.close();
                    break;
                }
            }
        }
    }
}

Multicast Receiver

import java.io.*;
import java.net.*;
class MulticastReceiver implements Runnable
{
    static MulticastSocket ms;
    static int port;
    static int status = 0;
    MulticastReceiver() throws Exception
    {
        port = 5000;
        ms= new MulticastSocket(port);
    }
    public void run()
    {
        while(true)
        {
            try
            {
                byte rbuf[] = new byte[2000];
                DatagramPacket  rpack = new DatagramPacket(rbuf,rbuf.length);
                if (status == 1)
                {
                    ms.close();
                    break;
                }
                ms.receive(rpack);
                System.out.println(new String(rpack.getData(),0,rpack.getLength()));
            }catch(Exception e){System.out.println(e);}
        }
    }
   
    public static void main(String args[]) throws Exception
    {
        MulticastReceiver mr =new MulticastReceiver();
        Thread t = new Thread(mr);
       
        String group = "225.4.5.6";
      
        System.out.println("Do you wish to join the group?");
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        if(br.readLine().equalsIgnoreCase("yes"))
        {
            ms.joinGroup(InetAddress.getByName(group));
           
            byte sbuf[] = new byte[2000];
            sbuf = "join".getBytes();
            DatagramPacket spack = new DatagramPacket(sbuf, sbuf.length,InetAddress.getByName("localhost"),4000);
            ms.send(spack);
          
            System.out.println("Joined the group");
            t.start();
            while(true)
            {
                if(br.readLine().equalsIgnoreCase("leave"))
                {
                    status = 1;
                    byte sbuf2[] = new byte[2000];
                    sbuf2 = "leave".getBytes();
                    DatagramPacket spack2 = new DatagramPacket(sbuf2,sbuf2.length,InetAddress.getByName("localhost"),4000);
                    ms.send(spack2);
                    ms.leaveGroup(InetAddress.getByName(group));
                    break;
                }
            }
        }
    }
}

How to run:

  1. First run Receiver program 
  2. Then next run Sender program in single or multiple times
  3. Give input "yes" from Receiver side
  4. Next give input in Sender side

Receiver Side Output:

Do you wish to join the group?
yes
Joined the group
hi

Sender Side Output:

Client joined the group

currently 1 client/clients is/are in the group
hi
 

import java.io.*;
class ShortPath
{
     public int vertex;
    public int edge;
    public int arrval[][]=new int[10][10];
    public int neigh[]=new int[10];
    public int nonnei[]=new int[10];
    public int tempnei,ntnei;
    public void getdata() throws Exception
    {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    System.out.println("ENTER NO OF VERTEX==>");
    vertex=Integer.parseInt(br.readLine());   
    System.out.println("ENTER NO OF EDGE==>");
    edge=Integer.parseInt(br.readLine());
    for(int i=0;i<vertex;i++)
    {
        for(int j=0;j<vertex;j++)
        {
            if(i==j)
                arrval[i][j]=0;
            else
                arrval[i][j]=999;
        }
    }
    for(int k=0;k<edge;k++)
    {
        System.out.println("ENTER THE EDGE V1-V2");
        int v1=Integer.parseInt(br.readLine());
        int v2=Integer.parseInt(br.readLine());
        System.out.println("ENTER THE EDGE COST==>");
        int cost=Integer.parseInt(br.readLine());
        arrval[v1][v2]=cost;
        arrval[v2][v1]=cost;
    }
    }

      public void process()throws Exception
    {
        int i,j,k;
        for(i=0;i<vertex;i++)
    {
        tempnei=0;
        ntnei=0;
        for(j=0;j<vertex;j++)
        {
            if(arrval[i][j]!=0 && arrval[i][j]!=999)
                tempnei++;
            else
                ntnei++;
        }
        neigh[i]=tempnei;
        nonnei[i]=ntnei;
    }
    for(k=0;k<vertex;k++)
    {
        System.out.println("THE NUMBER OF NEIGHBOUR VERTEX==>"+neigh[k]);
            System.out.println("THE NUMBER OF NON-NEIGHBOUR VERTEX==>"+nonnei[k]);
    }
    for(k=0;k<vertex;k++)
    {
        for(i=0;i<vertex;i++)
        {
            for(j=0;j<vertex;j++)
            {
             arrval[i][j]=min(arrval[i][j],arrval[i][k]+arrval[k][j]);
            }
        }
}
    }
    public void display()throws Exception
    {
    System.out.println("SHORTEST PATH");
    System.out.println();
    for(int i=0;i<vertex;i++)
    {
            for(int j=0;j<vertex;j++)
            {
                System.out.print(arrval[i][j]+"\t");
            }
            System.out.println();
        }
    }

    public int min(int a,int b)
    {
        int c=a<b?a:b;
        return c;
    }

    public static void main(String arg[])throws Exception
    {
        ShortPath s=new ShortPath();
        s.getdata();
        s.display();
        s.process();
        s.display();
    }
}   

Output:

ENTER NO OF VERTEX==> 
ENTER NO OF EDGE==> 
ENTER THE EDGE V1-V2 
ENTER THE EDGE COST==> 
ENTER THE EDGE V1-V2 
ENTER THE EDGE COST==> 
SHORTEST PATH

0    999     
999    0     
THE NUMBER OF NEIGHBOUR VERTEX==>0 
THE NUMBER OF NON-NEIGHBOUR VERTEX==>2 
THE NUMBER OF NEIGHBOUR VERTEX==>0 
THE NUMBER OF NON-NEIGHBOUR VERTEX==>2 
SHORTEST PATH

0    999     
999    0  

Client Side Program

import java.io.*;
import java.net.*;
class TCPClient {

  public static void main(String args[]) throws Exception
  {
       String sentence;
       String modifiedSentence;
        BufferedReader inFromUser =
         new BufferedReader(new InputStreamReader(System.in));

      Socket clientSocket = new Socket("127.0.0.1", 6324);

        DataOutputStream outToServer =
          new DataOutputStream(clientSocket.getOutputStream());
   
       BufferedReader inFromServer =
          new BufferedReader(new
            InputStreamReader(clientSocket.getInputStream()));

        sentence = inFromUser.readLine();
        outToServer.writeBytes(sentence + '\n');

        modifiedSentence = inFromServer.readLine();

        System.out.println("FROM SERVER: " + modifiedSentence);

        clientSocket.close();
                 
    }
}

Server Side Program

import java.io.*;
import java.net.*;

class TCPServer {

  public static void main(String args[]) throws Exception
    {
      String clientSentence;
      String capitalizedSentence;

      ServerSocket welcomeSocket = new ServerSocket(6324);
 
      while(true) {
 
            Socket connectionSocket = welcomeSocket.accept();

           BufferedReader inFromClient =
              new BufferedReader(new
              InputStreamReader(connectionSocket.getInputStream()));

          
DataOutputStream  outToClient =
             new DataOutputStream(connectionSocket.getOutputStream());

           clientSentence = inFromClient.readLine();
           System.out.println("FROM client:" + clientSentence); 
           capitalizedSentence = clientSentence.toUpperCase() + '\n';
           outToClient.writeBytes(capitalizedSentence);
        }
    }
}

How to run:

  1. First run server program then next run client program
  2. Give input from client side
  3. Next give input in server side

Client Side Output:

hi
FROM SERVER: HI

Server Side Output:

FROM client:hi
hi da
Welcome to My Blog

Translate

Popular Post

Total Pageviews

- Copyright © Learning Java Program - Powered by Blogger -