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 :- #
%
&


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

File Input Output Stream




import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author SHASHI
 */
public class FileOutputStream1 {
    public static void main(String[] args) throws IOException {
        try {
            FileOutputStream file = new FileOutputStream("F:\\check.txt"); // will create file with specified name and in given location
 
            file.write(35); //be specific while writting the byte values .. it has different id  for diferent symbols
            file.close(); // a good practise
        } catch (FileNotFoundException ex) {
            Logger.getLogger(FileOutputStream1.class.getName()).log(Level.SEVERE, null, ex);
        }

FileInputStream f = new FileInputStream("F:check.txt"); // will check for this file
 int i =f.read();  // for reading u have to specify the content i.e, u r going to read integer content
        System.out.println(" the content is " +i);
     
    }
   
   
}
OUTPUT

the content is  17// whatever written in the file