উদাহরণ সহ SocketChannel এর ব্যবহার

TCP Networking with SocketChannel - জাভা নিও (Java Nio) - Java Technologies

293

SocketChannel হল Java NIO এর একটি ক্লাস যা নেটওয়ার্কে TCP/IP প্রোটোকল ব্যবহার করে ক্লায়েন্ট এবং সার্ভারের মধ্যে ডেটা আদান-প্রদান করতে ব্যবহৃত হয়। এটি non-blocking এবং blocking I/O অপারেশন পরিচালনা করতে সক্ষম এবং ক্লায়েন্ট এবং সার্ভারের মধ্যে TCP/IP সংযোগে ডেটা পাঠানো ও গ্রহণের জন্য একটি শক্তিশালী উপাদান।

এখানে, আমরা SocketChannel ব্যবহার করে একটি ক্লায়েন্ট এবং সার্ভার তৈরি করার উদাহরণ দেখব। ক্লায়েন্ট এবং সার্ভার উভয়ই TCP সংযোগের মাধ্যমে একে অপরের সাথে ডেটা পাঠাবে ও গ্রহণ করবে। এটি blocking এবং non-blocking মোডে কাজ করতে পারে।


SocketChannel এর কার্যপ্রণালী

  • Blocking Mode: সাধারণভাবে, SocketChannel ব্লকিং মোডে কাজ করে, যার মানে হল যে একটি I/O অপারেশন (যেমন ডেটা পাঠানো বা গ্রহণ করা) সম্পন্ন না হওয়া পর্যন্ত থ্রেডটি অপেক্ষা করবে।
  • Non-blocking Mode: নন-ব্লকিং মোডে, SocketChannel কোনো I/O অপারেশনে ব্লক না হয়ে থ্রেডটি অন্য কাজ করতে পারবে। এটি একাধিক সংযোগ এবং I/O অপারেশনকে একযোগে পরিচালনা করতে সাহায্য করে।

উদাহরণ: Blocking SocketChannel এর মাধ্যমে ক্লায়েন্ট এবং সার্ভার তৈরি

১. Blocking Server (TCP)

import java.io.*;
import java.net.*;
import java.nio.*;
import java.nio.channels.*;

public class BlockingServer {
    public static void main(String[] args) throws IOException {
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        serverSocketChannel.bind(new InetSocketAddress(8080));

        System.out.println("Server started. Waiting for client connection...");

        // Accepting client connection
        SocketChannel socketChannel = serverSocketChannel.accept();
        System.out.println("Client connected: " + socketChannel.getRemoteAddress());

        // Read data from client
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        socketChannel.read(buffer);
        buffer.flip();
        
        // Display the received message
        String message = new String(buffer.array(), 0, buffer.limit());
        System.out.println("Received from client: " + message);

        // Send a response to the client
        buffer.clear();
        buffer.put("Hello from server!".getBytes());
        buffer.flip();
        socketChannel.write(buffer);

        // Closing connection
        socketChannel.close();
        serverSocketChannel.close();
    }
}

ব্যাখ্যা:

  1. ServerSocketChannel ব্যবহার করে সার্ভার তৈরি করা হয়েছে যা 8080 পোর্টে ক্লায়েন্টের সংযোগ গ্রহণ করবে।
  2. socketChannel.read() ব্যবহার করে ক্লায়েন্ট থেকে ডেটা পড়া হচ্ছে এবং প্রাপ্ত ডেটা প্রিন্ট করা হচ্ছে।
  3. সার্ভার ক্লায়েন্টকে একটি বার্তা পাঠাচ্ছে এবং তারপর সংযোগ বন্ধ করছে।

২. Blocking Client (TCP)

import java.io.*;
import java.net.*;
import java.nio.*;
import java.nio.channels.*;

public class BlockingClient {
    public static void main(String[] args) throws IOException {
        SocketChannel socketChannel = SocketChannel.open();
        socketChannel.connect(new InetSocketAddress("localhost", 8080));

        // Sending data to server
        String message = "Hello from client!";
        ByteBuffer buffer = ByteBuffer.wrap(message.getBytes());
        socketChannel.write(buffer);
        System.out.println("Message sent to server: " + message);

        // Read the response from the server
        buffer.clear();
        socketChannel.read(buffer);
        buffer.flip();
        String response = new String(buffer.array(), 0, buffer.limit());
        System.out.println("Received from server: " + response);

        // Closing connection
        socketChannel.close();
    }
}

ব্যাখ্যা:

  1. SocketChannel ব্যবহার করে ক্লায়েন্ট সার্ভারের সাথে সংযুক্ত হচ্ছে।
  2. ByteBuffer.wrap() ব্যবহার করে ক্লায়েন্ট একটি বার্তা সার্ভারে পাঠাচ্ছে।
  3. সার্ভার থেকে উত্তর গ্রহণ করার জন্য socketChannel.read() ব্যবহার করা হচ্ছে।

উদাহরণ: Non-blocking SocketChannel এর মাধ্যমে ক্লায়েন্ট এবং সার্ভার তৈরি

১. Non-blocking Server (TCP)

import java.nio.*;
import java.nio.channels.*;
import java.net.*;
import java.io.IOException;

public class NonBlockingServer {
    public static void main(String[] args) throws IOException {
        // Create a non-blocking server
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        serverSocketChannel.bind(new InetSocketAddress(8080));
        serverSocketChannel.configureBlocking(false);

        Selector selector = Selector.open();
        serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);

        while (true) {
            selector.select();  // Block until an event occurs

            for (SelectionKey key : selector.selectedKeys()) {
                if (key.isAcceptable()) {
                    SocketChannel socketChannel = serverSocketChannel.accept();
                    socketChannel.configureBlocking(false);
                    socketChannel.register(selector, SelectionKey.OP_READ);
                    System.out.println("Client connected: " + socketChannel.getRemoteAddress());
                } else if (key.isReadable()) {
                    SocketChannel clientChannel = (SocketChannel) key.channel();
                    ByteBuffer buffer = ByteBuffer.allocate(1024);
                    int bytesRead = clientChannel.read(buffer);
                    if (bytesRead == -1) {
                        clientChannel.close();
                    } else {
                        buffer.flip();
                        String message = new String(buffer.array(), 0, buffer.limit());
                        System.out.println("Received from client: " + message);
                    }
                }
            }
            selector.selectedKeys().clear();
        }
    }
}

ব্যাখ্যা:

  1. ServerSocketChannel.configureBlocking(false) দিয়ে সার্ভারকে non-blocking mode এ কনফিগার করা হয়েছে।
  2. Selector ব্যবহার করে সার্ভার একাধিক ক্লায়েন্ট সংযোগ এবং I/O অপারেশন একযোগে পরিচালনা করছে।

২. Non-blocking Client (TCP)

import java.nio.*;
import java.nio.channels.*;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.io.IOException;

public class NonBlockingClient {
    public static void main(String[] args) throws IOException {
        SocketChannel socketChannel = SocketChannel.open();
        socketChannel.configureBlocking(false);  // Non-blocking mode
        socketChannel.connect(new InetSocketAddress("localhost", 8080));

        while (!socketChannel.finishConnect()) {
            System.out.println("Connecting to server...");
        }

        String message = "Hello from non-blocking client!";
        ByteBuffer buffer = ByteBuffer.wrap(message.getBytes(StandardCharsets.UTF_8));
        socketChannel.write(buffer);
        System.out.println("Message sent to server.");

        socketChannel.close();
    }
}

ব্যাখ্যা:

  1. SocketChannel.configureBlocking(false) দিয়ে non-blocking mode সক্রিয় করা হয়েছে।
  2. finishConnect() মেথড ব্যবহার করে ক্লায়েন্ট সার্ভারের সাথে সংযোগ স্থাপন করার জন্য অপেক্ষা করছে।

ব্লকিং এবং নন-ব্লকিং TCP সংযোগের তুলনা

বৈশিষ্ট্যব্লকিং TCP সংযোগনন-ব্লকিং TCP সংযোগ
থ্রেড ব্লকিংহ্যাঁ, থ্রেড ব্লক হয়ে অপেক্ষা করেনা, থ্রেড অন্য কাজ চালিয়ে যায়
পারফরম্যান্সকম পারফরম্যান্স (একই থ্রেডের মাধ্যমে একাধিক I/O অপারেশন)উচ্চ পারফরম্যান্স (একাধিক I/O অপারেশন একযোগে)
এমনকি একাধিক ক্লায়েন্টএকযোগে কাজ করা কঠিনএকযোগে অনেক ক্লায়েন্টকে পরিচালনা করা সহজ
অপারেশন টাইমদীর্ঘ I/O অপারেশন হতে পারেদ্রুত I/O অপারেশন

Java তে Blocking এবং Non-blocking TCP সংযোগ ব্যবস্থাপনার দুটি গুরুত্বপূর্ণ পদ্ধতি। ব্লকিং TCP সংযোগ একটি সাধারণ এবং সহজ পদ্ধতি হলেও, Non-blocking TCP সংযোগ I/O অপারেশনগুলির পারফরম্যান্স বাড়ায় এবং একাধিক ক্লায়েন্ট বা সংযোগকে দ্রুত এবং দক্ষভাবে পরিচালনা করতে সক্ষম। SocketChannel এবং ServerSocketChannel ব্যবহার করে Java NIO এ আপনি ব্লকিং এবং নন-ব্লকিং I/O অপারেশন কার্যকরভাবে পরিচালনা করতে পারবেন।


Content added By
Promotion

Are you sure to start over?

Loading...