About HTTP/2

About HTTP/2 HTTP/2 is an advance version of HTTP/1.x . It has lots of advantages over its previous versions.. It supports multiplexing, binary message framing,server push, header compression and decompression, prioritization, re-prioritization of request, flow control, congestion control and many more... Initially, between client and server, a connection is established by sending a header frame which contains "headers", "h2"or "h2c". Where h2 refers to connection security(mainly of TLS/SSL) i.e, a negotiation between TLS-ALPN extension. In this negotiation, a list of protocol is sent from client to server .Now, server select the protocol and send the selected protocol certificate, server key and certification request.
From the client side it accept these things and send client key, confirmation of certificate verification. After that the handshake is complete and both endpoint negotiate to exchange data on verified protocol. In "h2c" it checks that the client request is cleartext or not. After header frame, the settings franme, containing 64 bit octet is sent from client . Finally, a connection preface frame is sent from one endpoint and client start sending headers frames before waiting from other endpoint the connection preface to come . This way connection is established in HTTP/2.

Two way interaction of client server in java

// here from both ends the message will be sent and read


//client code

public class TwoWayInteractionClient {

   
    public static void main(String[] args) throws IOException {
       
        Socket obj = new Socket("localhost",8844);
        DataOutputStream dout = new DataOutputStream(obj.getOutputStream());
        dout.writeUTF("This message is from client side");
        dout.flush();
       
       
        //ServerSocket rec = new ServerSocket(8134);
        DataInputStream din = new DataInputStream(obj.getInputStream());
        String newdata = (String)din.readUTF();
        System.out.println(" the new msg is " +newdata);
       
    }
   
   
}

output
the new msg is This msg is send from server


//server code

public class TwoWayInteractionServer {
     public static void main(String[] args) throws IOException {
       
    ServerSocket obj1 = new ServerSocket(8844);
        Socket obj = obj1.accept();
        DataInputStream din = new DataInputStream(obj.getInputStream());
        String str = (String)din.readUTF();
        System.out.println("The message from client is  " +str);
       
       // while two way interaction no need to create new port
        DataOutputStream dout =  new DataOutputStream(obj.getOutputStream());
        dout.writeUTF("This msg is send from server");
        dout.flush();
     }
   
   
}

output
The message from client is  This message is from client side











Client server callback of message

//code for client

public class MyClient {
 
 
    public static void main(String[] args) throws IOException {
     
        Socket obj = new Socket("localhost",8040);
        DataOutputStream dout = new DataOutputStream(obj.getOutputStream());
        dout.writeUTF("This message is from client side");
        dout.flush();
     
     
    }
}





// code for server

public class MyServer {
    public static void main(String[] args) throws IOException {
     
        ServerSocket obj1 = new ServerSocket(8040);
        Socket obj = obj1.accept();
        DataInputStream din = new DataInputStream(obj.getInputStream());
        String str = (String)din.readUTF();
        System.out.println("The message from client is  " +str);

     
    }
 
 
 
}

output (in server console)
This message is from client side



//here first we have to run server code then client code ..
//the message sent by the client will be readed in server concole
// the message are delivered through port no which is listened at sockets














Sequence Input Output Stream

import java.io.*;


// the purpose this api is to combine content of two input stream file  together
public class SequenceInputStreamEx    {
    public static void main(String[] args) throws Exception {
     
        FileOutputStream file1 = new FileOutputStream("F:\\file1.txt");
     
        FileOutputStream file2 = new FileOutputStream("F:\\file2.txt");

        String s1 = "He is awaken";
        byte b[]= s1.getBytes();
        file1.write(b);
        String s2 = "He will conquer all";
        byte c[]=s2.getBytes();
        file2.write(c);
        file1.close();
         file2.close();


// will put the above data in two files respectively
     

*********************************************************************************
// will combine the two file data , here we display in console and write in file also.     

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

        FileInputStream  file1 = new FileInputStream("F:\\file1.txt");
        FileInputStream  file2 = new FileInputStream("F:\\file2.txt");
     
        FileOutputStream fout = new FileOutputStream("F:\\SeqFile.txt");
     
        SequenceInputStream seqFile = new SequenceInputStream(file1, file2); // the two file data are combined together
     
        int i;
     
        while((i=seqFile.read())!=-1)
        {
            fout.write(i);          //writting the combined data in differnt file
         
            System.out.print((char)i);
        }
     

     
    }
}


output

He is awakenHe will conquer all

Use of Buffered , ByteArray Input and Output Stream



import com.sun.xml.internal.messaging.saaj.util.ByteInputStream;
import java.io.*;


public class BufferOutputStreamrExample {


    public static void main(String[] args) throws FileNotFoundException, IOException {

        FileOutputStream  file = new FileOutputStream("F:\\check.txt");
     
     
        BufferedOutputStream bout = new BufferedOutputStream(file); //collection of data //here it is now connected to fileoutputStream to flush large amount of data together
        String s = "shashi is not dead";
        byte b[] = s.getBytes();
        bout.write(b);
        bout.flush();//it is used for flushing the buffer output stream data .
        bout.close();
        file.close();
       
    }
   
*********************************************************************************
    // whereas in byte array output stream we can write it to a different file

    public static void main(String[] args) throws FileNotFoundException, IOException {

        FileOutputStream  file = new FileOutputStream("F:\\check.txt");
     
     ByteArrayOutputStream bout1 = new ByteArrayOutputStream();
        bout1.write(65); // byte gives the ascii code so 65==A
        bout1.writeTo(file);


    // like  bout.writeTo(file1);
   
}

*********************************************************************************

   
     public static void main(String[] args) throws FileNotFoundException, IOException {
        FileInputStream  file = new FileInputStream("F:\\check.txt");
        BufferedInputStream bin = new BufferedInputStream(file); //collection of data //here it is now connected to fileoutputStream
     
     int i=0 ;
         while((i=bin.read())!=-1)
         {
             char ch =(char)i;
             System.out.println(ch);
           
         }
       
        bin.close();
        file.close();
       
         
}

******************************************************************************

public static void main(String[] args) throws FileNotFoundException, IOException {

byte []g = {35,37,38}; // be spefic while using byte as it has differnt code for different symbols.
      ByteArrayInputStream bin = new ByteArrayInputStream(g);// it will only read the byte data
 int i=0 ;
         while((i=bin.read())!=-1)
         {
             char ch =(char)i;
             System.out.println(ch);
           
         }
        }

output :- #
%
&


*******************************************************************************
}