Sunday, November 20, 2016

Process-to-Process Delivery: UDP and TCP

Data Deliveries Types



Process-To-Process Delivery
Types
A. TCP : Transmission Control Protocol
B. UDP : User Datagram Protocol
C. SCTP : Stream Control Transmission Protocol

  TCP Working 


 Code

Communicate Between Client & Server Program , when both are running on Same Machine through Transmission Control Protocol

// This code is for Client save it as cli.java


import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class cli{

public static void main(String args[]) throws UnknownHostException, IOException
 {
  int number,temp;
 Scanner sc =new Scanner(System.in);
 Socket s =new Socket("127.0.0.1",1344);// for ip address & port number
 Scanner sc1= new Scanner(s.getInputStream());//get input stream
 
  System.out.println("Enter any number");
  number= sc.nextInt();
  PrintStream P= new PrintStream(s.getOutputStream());
  P.println(number);
  temp=sc1.nextInt(); // creating temporary variable in order to accepting result from the server
  System.out.println(temp);
   
 }
}

// This code is for Server save it as ser.java


import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
  
public class ser {
 public static void main(String args[]) throws IOException
 {
  int number,temp;
  ServerSocket s1=new ServerSocket(1344);// specified same port number as specified in the client  
  Socket ss=s1.accept(); // for accept incoming request from the client.
  Scanner sc=new Scanner(ss.getInputStream()); // accept some data from the client. 
  number=sc.nextInt();
 
  temp=number*2; // result saving on the operation and pass it.
 
  PrintStream p=new PrintStream(ss.getOutputStream());
  p.println(temp);
 }

}


//*OutputScreen*/





 UDP Working 




Code

Communicate Between Client & Server , when both are running on Same Machine through User DataGram Protocol (UDP)

// this is code for Server

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

      DatagramSocket serverSocket = new DatagramSocket(9876);

      byte[] receiveData = new byte[1024];
      byte[] sendData  = new byte[1024];

      while(true)
        {
          System.out.println("SERVER is waiting to receive a string");
          DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
          serverSocket.receive(receivePacket);
          String sentence = new String(receivePacket.getData());

          InetAddress IPAddress = receivePacket.getAddress();

          int port = receivePacket.getPort();

          String capitalizedSentence = sentence.toUpperCase();

          sendData = capitalizedSentence.getBytes();

          DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);

          serverSocket.send(sendPacket);
          System.out.println("SERVER has converted a string from lowercase to uppercase");
        }
      } 


// this is code for Client 

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

// this program wil send a string to server & server in return send the same string in capital

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

      BufferedReader inFromUser =
        new BufferedReader(new InputStreamReader(System.in));

      DatagramSocket clientSocket = new DatagramSocket();

      InetAddress IPAddress = InetAddress.getByName("localhost");

      byte[] sendData = new byte[1024];
      byte[] receiveData = new byte[1024];

      String sentence = inFromUser.readLine();
      sendData = sentence.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);

      clientSocket.send(sendPacket);

      DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);

      clientSocket.receive(receivePacket);

      String modifiedSentence =  new String(receivePacket.getData());

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

//*OutputScreen*/








No comments:

Post a Comment