Sockets (Client and Server Communication) Java In Hindi

Sockets (Client and Server Communication) Java In Hindi

जावा प्रोग्रामिंग लैंग्वेज में, सॉकेट एक एंडपॉइंट क्लाइंट सर्वर कम्युनिकेशन कॉन्सेप्ट है, सॉकेट मेथड का यूज़ ऑनलाइन ऑफलाइन नेटवर्क कम्युनिकेशन पर दो डेडिकेटेड प्रोग्राम के बीच कम्युनिकेशन को प्रॉपर इस्टैब्लिश करने में किया जाता है।

Sockets (Client and Server Communication) Java In Hindi

In Java network communication, socket programming primarily helps you.

java.net

Basic Java client-server communication model.

        Client Server Communication

                                | |

| —- Client Made Connection Request —> |

                                 | |

| <—- Server Connection Accepted — |

                                | |

| —- Send Required Client Data ————> |

| <—- Receive Required Client Data ——— |

                               | |

Java provides two main classes for this.

  • Socket — इसे जावा नेटवर्क कम्युनिकेशन मे क्लाइंट साइड में यूज़ किया जाता है.
  • ServerSocket — इसे जावा नेटवर्क कम्युनिकेशन मे सर्वर साइड में यूज़ किया जाता है.

Server-side Socket Java communication concept.

यह एक सॉकेट सर्वर डेडिकेटेड लोकेशन या मेथड है, जो क्लाइंट रिक्वेस्ट के नेटवर्क में कनेक्ट होने का इंतज़ार करता है।

Basic steps of a Server-side Socket.

सबसे पहले एक ServerSocket बनाएं।

                ↓

क्लाइंट इसका का इंतज़ार करें

               ↓

सर्वर क्लाइंट कनेक्शन को एक्सेप्ट करें

              ↓

इनपुट/आउटपुट स्ट्रीम पाएं

             ↓

फाइल में डेटा पढ़ें/लिखें

            ↓

सॉकेट कनेक्शन को बंद करें

Server-side Socket example in Java.

import java.io.*;

import java.net.*;

public class Server {

public static void main(String[] args)

throws IOException {

ServerSocket server =

new ServerSocket(5000);

System.out.println(“Host Server is turn on”)

Socket socket = server.accept();

BufferedReader input =

new BufferedReader(

new InputStreamReader(

socket.getInputStream()

)

);

String info = input.readLine();

System.out.println(

“Client request -: ” + info

);

socket.close();

server.close();

}

}

Here in the Server-side Socket example.

new ServerSocket(5000)

यह एक सर्वर को पोर्ट 5000 लोकेशन पर स्टार्ट करता है।

Main ServerSocket method.

server.accept()

यह सर्वर सॉकेट कम्युनिकेशन में क्लाइंट रिक्वेस्ट कनेक्शन का इंतज़ार करता है।

Client-side network communication concept.

यह मेथड क्लाइंट साइड नेटवर्क सॉकेट का यूज़ करके एक डेडिकेटेड होस्ट सर्वर से कनेक्ट होता है।

Example of client-side network communication.

import java.io.*;

import java.net.*;

public class Client {

    public static void main(String[] args)

            throws IOException {

        // here we connect to the server on port 5000 location

        Socket socket = new Socket(“localhost”, 5000);

        // here we create output stream to send data

        PrintWriter output = new PrintWriter(

                socket.getOutputStream(), true

        );

        // here we send message to the dedicated server

        output.println(“Hi Server”);

        // here we close the connection after done

        output.close();

        socket.close();

    }

}

जावा प्रोग्राम में क्लाइंट इसका यूज़ करके सर्वर सॉकेट से कनेक्ट होता है.

new Socket(“localhost”, 5000);

In a client-side network communication example.

  • localhost → यह एक लोकल सर्वर एड्रेस लोकेशन है.
  • 5000 → यह सर्वर में एक डेडिकेटेड पोर्ट लोकेशन है.

Java Server Sockets send and receive data in client-server communication.

जावा क्लाइंट सर्वर नेटवर्क कम्युनिकेशन में एक सॉकेट इनपुट और आउटपुट स्ट्रीम प्रोवाइड करता है।

InputStream input =

socket.getInputStream();

OutputStream output =

socket.getOutputStream();

सामान्य रूप से जावा नेटवर्क कम्युनिकेशन में, इन स्ट्रीम्स को इजी क्लास के साथ रैप करने में किया जाता है।

Sending text in network communication.

PrintWriter writer =

new PrintWriter(

socket.getOutputStream(),

true

);

writer.println(“Hi server”);

Receiving text in network communication.

BufferedReader reader =

new BufferedReader(

new InputStreamReader(

socket.getInputStream()

)

);

String info = reader.readLine();

Two-way Java client-server socket communication.

जावा नेटवर्क कम्युनिकेशन में क्लाइंट और सर्वर दोनों आपस में एक दूसरे को डेडिकेटेड नेटवर्क कम्युनिकेशन सॉकेट पोर्ट में इम्पोर्टेन्ट डेटा को सेंड और रिसीव कर सकते हैं।

Server data send-receive communication.

PrintWriter output =

new PrintWriter(

socket.getOutputStream(),

true

);

BufferedReader input =

new BufferedReader(

new InputStreamReader(

socket.getInputStream()

)

;

String info = input.readLine();

System.out.println(“Client communication -: ” + info);

out.println(“Hi Client”);

Client data send-receive communication.

PrintWriter output =

new PrintWriter(

socket.getOutputStream(),

true

);

BufferedReader input =

new BufferedReader(

new InputStreamReader(

socket.getInputStream()

)

);

output.println(“Hi Server”);

String reply = input.readLine();

System.out.println(

“Server output -: ” + reply

);

Thus, it becomes client-server socket communication.

Client server send receive socket communication.

                  | |

| “Hi Server” ————> |

                   | |

| <———— “Hi Client” |

                   | |

Handling multiple clients in a Java Server Socket.

जावा क्लाइंट सर्वर पोर्ट नेटवर्क कम्युनिकेशन में एक बेसिक सर्वर एक बार में केवल एक क्लाइंट को हैंडल या मैनेज करता है।

एक साथ मल्टीप्ल क्लाइंट्स नेटवर्क कम्युनिकेशन के लिए, यह एक कॉमन जावा मेथड है, जिससे कि हर क्लाइंट के लिए एक अलग थ्रेड क्रिएट किया जाता है।

while (true) {

Socket socket =

server.accept();

new Thread(() -> {

// now it can handle multiple client

}).start();

}

Conceptual Java Multiple Client Handling Concept.

                  Dedicated web server.

                                    |

         ServerSocket communication method

                                   |

┌───────────┼───────────┐

 ↓                                ↓                                ↓

क्लाइंट रिक्वेस्ट 1 क्लाइंट रिक्वेस्ट 2   क्लाइंट रिक्वेस्ट 3

↓                                ↓                                 ↓

क्लाइंट थ्रेड 1      क्लाइंट थ्रेड 2               क्लाइंट थ्रेड 3

यहाँ इस एक्साम्प्प्ल में हाई-कन्करेंसी एप्लिकेशन के लिए, सॉकेटचैनल और सिलेक्टर के साथ जावा NIO हर कनेक्शन के लिए एक क्लाइंट नेटवर्क कम्युनिकेशन थ्रेड की ज़रूरत से बच सकता है।

Important Java Client-Server Socket Network Communication Classes

#Important Socket ClassWhat Is the Purpose of Client-Server Socket Class
1.Socket classSocket class helps us to establish client-side TCP network connection
2.ServerSocket classServerSocket class used to listen for TCP client connections request
3.InetAddress classInetAddress class used to display or represents an client system IP address information
4.InputStream classInputStream class used to receive data in socket communication
5.OutputStream classOutputStream class help us to sends data and information in communication
6.BufferedReader classBufferedReader class help us to reads text efficiently in socket communication
7.PrintWriter classPrintWriter class helping us to writes text conveniently in socket communication

TCP vs. UDP in Java client-server socket communication.

जावा क्लाइंट सर्वर नेटवर्क सॉकेट कम्युनिक्शन में TCP और UDP दोनों कम्युनिकेशन को फुल सपोर्ट करता है।

TCP Network Communication port.

Using a TCP port.

  • In a socket.
  • In a ServerSocket.

TCP नेटवर्क कम्युनिकेशन पोर्ट एक कनेक्शन-ओरिएंटेड और एक्यूरेट और रिलाएबल  बाइट स्ट्रीम प्रोवाइड करता है।

UDP Network Communication port.

Using a UDP port.

  • In a DatagramSocket.
  • In a DatagramPacket.

याद रहे, नेटवर्क कम्युनिकेशन में UDP बिना कनेक्शन के मेथड है, और ये इसके यूजर को TCP डाटा इनफार्मेशन सक्सेस डिलीवरी की गारंटी नहीं प्रोवाइड करते है।

Socket communication flow in a Java client-server network.

SERVER socket communication flow.

ServerSocket

          ↓

client request accept()

          ↓

Socket communication

         ↓

InputStream / OutputStream

        ↓

Read / Write task

CLIENT socket communication flow.

ClientSocket

        ↓

Connect to Server port

        ↓

InputStream / OutputStream

        ↓

Read / Write task

Conclusion of Sockets Client and Server Communication.

  • ServerSocket डेडिकेटेड नेटवर्क कम्युनिकेशन में आने वाले TCP कनेक्शन को सुनता है।
  • जिसमे Socket एक क्लाइंट और सर्वर के बीच एक TCP कनेक्शन को डिस्प्ले करता  है।
  • accept() फंक्शन सर्वर में आने वाले सभी क्लाइंट रिक्वेस्ट का इंतज़ार करता है।
  • getInputStream() फंक्शन कम्युनिकेशन में डेटा को एक्सेप्ट करता है।
  • getOutputStream() फंक्शन कम्युनिकेशन में डेटा को सेंड करता है।
  • क्लाइंट सर्वर कम्युनिकेशन में थ्रेड्स या NIO का यूज़ करके कई क्लाइंट्स को हैंडल किया जा सकता है।
  • याद रहे, हमेशा सॉकेट्स और स्ट्रीम्स को क्लोज करें, बेहतर होगा कि try-with-resources का इसमें यूज़ करें।

In short, सॉकेट प्रोग्रामिंग जावा वेब ऑफलाइन एप्लिकेशन्स को क्लाइंट-सर्वर मॉडल का यूज़ करके नेटवर्क पर कम्युनिकेट एस्टाब्लिशमेंट करने में हेल्प करती है, जिसमें ServerSocket क्लाइंट कनेक्शन को एक्सेप्ट करता है, और Socket एक बेहतर कम्युनिकेशन चैनल प्रोवाइड करता है।

Leave a Reply