Socket API (Step)

Socket API (Step)

Socket API : Socket API programming write must be import package import java.net.*;

                       Client   Server
1 Creating the client socket Transmit or receive data must be encapsulated in the packet DatagramSocket constructor is used to create the UDP socket. DatagramSocket client_socket = new DatagramSocket(); 1 Creating the socket for server 1234: Port DatagramSocket server_socket = new DatagramSocket(1234);
2 user_input BufferedReader user_input = new BufferedReader(new InputStreamReader(System.in)); 2 Getting data client Array which reserves the input data getting from client byte in_data[] = new byte[1024];
3 Getting IP Address InetAddress IP_add = InetAddress.getByName(“localhost”);   while (true) {    DatagramPacket Packet2 = new DatagramPacket(in_data, in_data.length);    server_socket.receive(Packet2);    String str = new String(Packet2.getData());    System.out.println(str); }
4 Buffer sending data byte out_data[] = new byte[1024]; 3 Creating the datagram packet DatagramPacket Packet2 = new DatagramPacket(in_data, in_data.length);
5 Reading the input through keyboard String str = user_input.readLine(); out_data = str.getBytes();    
6 Encapsulated DatagramPacket Packet1 = new DatagramPacket(out_data, out_data.length, IP_add, 1234);    
7 Sending the packet to the server client_socket.send(Packet1); 4 The data from the client is received in the packet server_socket.receive(Packet2);
8 Closing the client’s connection client_socket.close();    
    5 Printing the received data on the console of server String str = new String(Packet2.getData()); System.out.println(str);

  Client receiving data (Server get data)   Sending data client
1 byte in_data[] = new byte[1024];    
2 DatagramPacket Packet4 = new DatagramPacket(in_data, in_data.length); 2 user_input BufferedReader user_input = new BufferedReader(new InputStreamReader(System.in));
3 The data from the server is received in the packet server_socket.receive(Packet2); 3 InetAddress IP_add1 = Packet2.getAddress(); int port = Packet2.getPort();
4 Printing the received data on the console of client String receive_str = new String(Packet4.getData()); 4   String send_str = server_input.readLine();  out_data = send_str.getBytes();
5 System.out.println(receive_str); 5 Sending the packet to the client DatagramPacket Packet3 = new DatagramPacket(out_data, out_data.length,IP_add1, port);
    6 server_socket.send(Packet3);

Client.java

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

class Client
{
    public static void main(String[] args) throws Exception
    {
        BufferedReader user_input = new BufferedReader(new InputStreamReader(System.in));
        //Creating the client socket
        DatagramSocket client_socket = new DatagramSocket();
        //Getting the IP address of host "Localhost"
        InetAddress IP_add = InetAddress.getByName("localhost");

        //Creating the buffer for sending the data
        byte out_data[] = new byte[1024];
        byte in_data[] = new byte[1024];

        //Reading the input through keyboard
        String str = user_input.readLine();
        out_data = str.getBytes();
        //Creating the datagram packet in which data is
        //Encapsulated
        DatagramPacket Packet1 = new DatagramPacket(out_data, out_data.length, IP_add, 1234);
        //Sending the packet to the server
        client_socket.send(Packet1);

        DatagramPacket Packet4 = new DatagramPacket(in_data, in_data.length);
        //The data from the server is received in the packet
        client_socket.receive(Packet4);

        String receive_str = new String(Packet4.getData());

        //Printing the received data on te console of client
        System.out.println(receive_str);

        //Closing the client's connecton
        client_socket.close();
    }
}

Server.java

import java.io.*;
import java.net.*;
import java.util.Date;

class Server
{
    public static void main(String[] args) throws Exception
    {
        //creating the socket for server
        DatagramSocket server_socket = new DatagramSocket(1234);
        //array which reserves the input data getting from
        //client
        byte in_data[] = new byte[1024];
        byte out_data[] = new byte[1024];
        while (true)
        {
            Thread.sleep(1000); //1 second
            //creating the datagram packet
            DatagramPacket Packet2 = new DatagramPacket(in_data, in_data.length);
            //The data from the client is received in the packet
            server_socket.receive(Packet2);
            String str = new String(Packet2.getData());
            //Printing the received data on the console of server
            System.out.println(str);

            InetAddress IP_add1 = Packet2.getAddress();
            int port = Packet2.getPort();

            Date d = new Date();
            String send_str = d.toString();
            out_data = send_str.getBytes();
            DatagramPacket Packet3 = new DatagramPacket(out_data, out_data.length, IP_add1, port);
            //sending the packet to the client
            server_socket.send(Packet3);
        }
    }
}

TCP / IP Step

  Client   Server
1 Create TCP socket Socket client_socket = new Socket(“hostname”, port); 1 ServerSocketserver_socket = new ServerSocket(1234);
2 BufferedReader user_input = new BufferedReader(new InputStreamReader(System.in));    
  String str; str = user_input.readLine(); server_out.writeBytes(str + “\n”); client_socket.close();   Socket Listen_socket = server_socket.accept(); BufferedReaderclient_input = new BufferedReader(new InputStreamReader(Listen_socket.getInputStream())); String client_str; client_str = client_input.readLine(); System.out.println(client_str);
3 Client initiates the communication by reading the user input str = user_input.readLine(); 2 Client connection Socket Listen_socket = server_socket.accept();  
4 Then sends it to server server_out.writeBytes(str + “\n”); 3 Server listens client client_str = client_input.readLine();
5 Close client_socket.close();    

The flutter tutorial  is a website that bring you the latest and amazing resources of code. All the languages codes are included in this website. The languages like flutter, android, java,kotlin etc.with the help of this languages any user can develop the beautiful application

For more information about Flutter. visit www.fluttertutorial.in