Tag Archives: client

WebClient – Client application that can talk interactively to Web servers. Requires the components listed below

Huge Sell on Popular Electronics

import java.awt.*; // For BorderLayout, GridLayout, Font, Color.
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

/** A graphical client that lets you interactively connect to
 *  Web servers and send custom request lines and
 *  request headers.
 *
 *  Taken from Core Web Programming from
 *  Prentice Hall and Sun Microsystems Press,
 *  .
 *  © 2001 Marty Hall and Larry Brown;
 *  may be freely used or adapted.
 */

public class WebClient extends JPanel
    implements Runnable, Interruptible, ActionListener {
  public static void main(String[] args) {
    WindowUtilities.setNativeLookAndFeel();
    WindowUtilities.openInJFrame(new WebClient(), 600, 700,
                                 "Web Client",
                                 SystemColor.control);
  }

  private LabeledTextField hostField, portField,
          requestLineField;
  private JTextArea requestHeadersArea, resultArea;
  private String host, requestLine;
  private int port;
  private String[] requestHeaders = new String[30];
  private JButton submitButton, interruptButton;
  private boolean isInterrupted = false;

  public WebClient() {
    setLayout(new BorderLayout(5, 30));
    int fontSize = 14;
    Font labelFont =
      new Font("Serif", Font.BOLD, fontSize);
    Font headingFont =
      new Font("SansSerif", Font.BOLD, fontSize+4);
    Font textFont =
      new Font("Monospaced", Font.BOLD, fontSize-2);
    JPanel inputPanel = new JPanel();
    inputPanel.setLayout(new BorderLayout());
    JPanel labelPanel = new JPanel();
    labelPanel.setLayout(new GridLayout(4,1));
    hostField = new LabeledTextField("Host:", labelFont,
                                     30, textFont);
    portField = new LabeledTextField("Port:", labelFont,
                                     "80", 5, textFont);
    // Use HTTP 1.0 for compatibility with the most servers.
    // If you switch this to 1.1, you *must* supply a
    // Host: request header.
    requestLineField =
      new LabeledTextField("Request Line:", labelFont,
                           "GET / HTTP/1.0", 50, textFont);
    labelPanel.add(hostField);
    labelPanel.add(portField);
    labelPanel.add(requestLineField);
    JLabel requestHeadersLabel =
      new JLabel("Request Headers:");
    requestHeadersLabel.setFont(labelFont);
    labelPanel.add(requestHeadersLabel);
    inputPanel.add(labelPanel, BorderLayout.NORTH);
    requestHeadersArea = new JTextArea(5, 80);
    requestHeadersArea.setFont(textFont);
    JScrollPane headerScrollArea =
      new JScrollPane(requestHeadersArea);
    inputPanel.add(headerScrollArea, BorderLayout.CENTER);
    JPanel buttonPanel = new JPanel();
    submitButton = new JButton("Submit Request");
    submitButton.addActionListener(this);
    submitButton.setFont(labelFont);
    buttonPanel.add(submitButton);
    inputPanel.add(buttonPanel, BorderLayout.SOUTH);
    add(inputPanel, BorderLayout.NORTH);
    JPanel resultPanel = new JPanel();
    resultPanel.setLayout(new BorderLayout());
    JLabel resultLabel =
      new JLabel("Results", JLabel.CENTER);
    resultLabel.setFont(headingFont);
    resultPanel.add(resultLabel, BorderLayout.NORTH);
    resultArea = new JTextArea();
    resultArea.setFont(textFont);
    JScrollPane resultScrollArea =
      new JScrollPane(resultArea);
    resultPanel.add(resultScrollArea, BorderLayout.CENTER);
    JPanel interruptPanel = new JPanel();
    interruptButton = new JButton("Interrupt Download");
    interruptButton.addActionListener(this);
    interruptButton.setFont(labelFont);
    interruptPanel.add(interruptButton);
    resultPanel.add(interruptPanel, BorderLayout.SOUTH);
    add(resultPanel, BorderLayout.CENTER);
  }

  public void actionPerformed(ActionEvent event) {
    if (event.getSource() == submitButton) {
      Thread downloader = new Thread(this);
      downloader.start();
    } else if (event.getSource() == interruptButton) {
      isInterrupted = true;
    }
  }

  public void run() {
    isInterrupted = false;
    if (hasLegalArgs())
      new HttpClient(host, port, requestLine,
             requestHeaders, resultArea, this);
  }

  public boolean isInterrupted() {
    return(isInterrupted);
  }

  private boolean hasLegalArgs() {
    host = hostField.getTextField().getText();
    if (host.length() == 0) {
      report("Missing hostname");
      return(false);
    }
    String portString =
      portField.getTextField().getText();
    if (portString.length() == 0) {
      report("Missing port number");
      return(false);
    }
    try {
      port = Integer.parseInt(portString);
    } catch(NumberFormatException nfe) {
      report("Illegal port number: " + portString);
      return(false);
    }
    requestLine =
      requestLineField.getTextField().getText();
    if (requestLine.length() == 0) {
      report("Missing request line");
      return(false);
    }
    getRequestHeaders();
    return(true);
  }

  private void report(String s) {
    resultArea.setText(s);
  }

  private void getRequestHeaders() {
    for(int i=0; i
   *    LabeledTextField ltf = new LabeledTextField(...);
   *    ltf.getLabel().someLabelMethod(...);
   *

*/ public JLabel getLabel() { return(label); } /** The TextField at the right side of the * LabeledTextField. */ public JTextField getTextField() { return(textField); } } Interruptible.java Polls to see if the user choose to interrupt the current network download. /** An interface for classes that can be polled to see * if they've been interrupted. Used by HttpClient * and WebClient to allow the user to interrupt a network * download. * * Taken from Core Web Programming from * Prentice Hall and Sun Microsystems Press, * . * © 2001 Marty Hall and Larry Brown; * may be freely used or adapted. */ public interface Interruptible { public boolean isInterrupted(); } NetworkClient.java Starting point for a network client to communicate with a remote computer. import java.net.*; import java.io.*; /** A starting point for network clients. You'll need to * override handleConnection, but in many cases connect can * remain unchanged. It uses SocketUtil to simplify the * creation of the PrintWriter and BufferedReader. * * Taken from Core Web Programming from * Prentice Hall and Sun Microsystems Press, * © 2001 Marty Hall and Larry Brown; * may be freely used or adapted. */ public class NetworkClient { protected String host; protected int port; /** Register host and port. The connection won't * actually be established until you call * connect. */ public NetworkClient(String host, int port) { this.host = host; this.port = port; } /** Establishes the connection, then passes the socket * to handleConnection. */ public void connect() { try { Socket client = new Socket(host, port); handleConnection(client); } catch(UnknownHostException uhe) { System.out.println("Unknown host: " + host); uhe.printStackTrace(); } catch(IOException ioe) { System.out.println("IOException: " + ioe); ioe.printStackTrace(); } } /** This is the method you will override when * making a network client for your task. * The default version sends a single line * ("Generic Network Client") to the server, * reads one line of response, prints it, then exits. */ protected void handleConnection(Socket client) throws IOException { PrintWriter out = SocketUtil.getWriter(client); BufferedReader in = SocketUtil.getReader(client); out.println("Generic Network Client"); System.out.println ("Generic Network Client:\n" + "Made connection to " + host + " and got '" + in.readLine() + "' in response"); client.close(); } /** The hostname of the server we're contacting. */ public String getHost() { return(host); } /** The port connection will be made on. */ public int getPort() { return(port); } } SocketUtil.java: Provides utilities for wrapping a BufferedReader and PrintWriter around the Socket's input and output streams, respectively. import java.net.*; import java.io.*; /** A shorthand way to create BufferedReaders and * PrintWriters associated with a Socket. * * Taken from Core Web Programming from * Prentice Hall and Sun Microsystems Press, * . * © 2001 Marty Hall and Larry Brown; * may be freely used or adapted. */ public class SocketUtil { /** Make a BufferedReader to get incoming data. */ public static BufferedReader getReader(Socket s) throws IOException { return(new BufferedReader( new InputStreamReader(s.getInputStream()))); } /** Make a PrintWriter to send outgoing data. * This PrintWriter will automatically flush stream * when println is called. */ public static PrintWriter getWriter(Socket s) throws IOException { // Second argument of true means autoflush. return(new PrintWriter(s.getOutputStream(), true)); } } WindowUtilities.java Simplifies the setting of native look and feel. import javax.swing.*; import java.awt.*; // For Color and Container classes. /** A few utilities that simplify using windows in Swing. * * Taken from Core Web Programming from * Prentice Hall and Sun Microsystems Press, * © 2001 Marty Hall and Larry Brown; * may be freely used or adapted. */ public class WindowUtilities { /** Tell system to use native look and feel, as in previous * releases. Metal (Java) LAF is the default otherwise. */ public static void setNativeLookAndFeel() { try { UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName()); } catch(Exception e) { System.out.println("Error setting native LAF: " + e); } } public static void setJavaLookAndFeel() { try { UIManager.setLookAndFeel( UIManager.getCrossPlatformLookAndFeelClassName()); } catch(Exception e) { System.out.println("Error setting Java LAF: " + e); } } public static void setMotifLookAndFeel() { try { UIManager.setLookAndFeel( "com.sun.java.swing.plaf.motif.MotifLookAndFeel"); } catch(Exception e) { System.out.println("Error setting Motif LAF: " + e); } } /** A simplified way to see a JPanel or other Container. Pops * up a JFrame with specified Container as the content pane. */ public static JFrame openInJFrame(Container content, int width, int height, String title, Color bgColor) { JFrame frame = new JFrame(title); frame.setBackground(bgColor); content.setBackground(bgColor); frame.setSize(width, height); frame.setContentPane(content); frame.addWindowListener(new ExitListener()); frame.setVisible(true); return(frame); } /** Uses Color.white as the background color. */ public static JFrame openInJFrame(Container content, int width, int height, String title) { return(openInJFrame(content, width, height, title, Color.white)); } /** Uses Color.white as the background color, and the * name of the Container's class as the JFrame title. */ public static JFrame openInJFrame(Container content, int width, int height) { return(openInJFrame(content, width, height, content.getClass().getName(), Color.white)); } } ExitListener.java WindowListener for applications in this chapter. import java.awt.*; import java.awt.event.*; /** A listener that you attach to the top-level JFrame of * your application, so that quitting the frame exits the * application. * * Taken from Core Web Programming from * Prentice Hall and Sun Microsystems Press, * © 2001 Marty Hall and Larry Brown; * may be freely used or adapted. */ public class ExitListener extends WindowAdapter { public void windowClosing(WindowEvent event) { System.exit(0); } }

NetworkClientTest.java Makes a simple connection to the host and port specified on the command line. Uses the following classes

Huge Sell on Popular Electronics

NetworkClientTest.java  Makes a simple connection to the host and port specified on the command line. Uses the following classes:

/** Make simple connection to host and port specified.
 *
 *  Taken from Core Web Programming from
 *  Prentice Hall and Sun Microsystems Press,
 *  .
 *  © 2001 Marty Hall and Larry Brown;
 *  may be freely used or adapted.
 */

public class NetworkClientTest {
  public static void main(String[] args) {
    String host = "localhost";
    int port = 8088;
    if (args.length > 0) {
      host = args[0];
    }
    if (args.length > 1) {
      port = Integer.parseInt(args[1]);
    }
    NetworkClient nwClient = new NetworkClient(host, port);
    nwClient.connect();
  }
}



NetworkClient.java  Starting point for a network client to communicate with a remote computer.

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

/** A starting point for network clients. You'll need to
 *  override handleConnection, but in many cases connect can
 *  remain unchanged. It uses SocketUtil to simplify the
 *  creation of the PrintWriter and BufferedReader.
 *
 *  Taken from Core Web Programming from
 *  Prentice Hall and Sun Microsystems Press,
 *  .
 *  © 2001 Marty Hall and Larry Brown;
 *  may be freely used or adapted.
 */

public class NetworkClient {
  protected String host;
  protected int port;

  /** Register host and port. The connection won't
   *  actually be established until you call
   *  connect.
   */

  public NetworkClient(String host, int port) {
    this.host = host;
    this.port = port;
  }

  /** Establishes the connection, then passes the socket
   *  to handleConnection.
   */

  public void connect() {
    try {
      Socket client = new Socket(host, port);
      handleConnection(client);
    } catch(UnknownHostException uhe) {
      System.out.println("Unknown host: " + host);
      uhe.printStackTrace();
    } catch(IOException ioe) {
      System.out.println("IOException: " + ioe);
      ioe.printStackTrace();
    }
  }

  /** This is the method you will override when
   *  making a network client for your task.
   *  The default version sends a single line
   *  ("Generic Network Client") to the server,
   *  reads one line of response, prints it, then exits.
   */

  protected void handleConnection(Socket client)
    throws IOException {
    PrintWriter out = SocketUtil.getWriter(client);
    BufferedReader in = SocketUtil.getReader(client);
    out.println("Generic Network Client");
    System.out.println
      ("Generic Network Client:\n" +
       "Made connection to " + host +
       " and got '" + in.readLine() + "' in response");
    client.close();
  }

  /** The hostname of the server we're contacting. */

  public String getHost() {
    return(host);
  }

  /** The port connection will be made on. */

  public int getPort() {
    return(port);
  }
}


SocketUtil.java  Provides utilities for wrapping a BufferedReader around the Socket's input stream and a PrintWriter  around the Socket's output stream

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

/** A shorthand way to create BufferedReaders and
 *  PrintWriters associated with a Socket.
 *
 *  Taken from Core Web Programming from
 *  Prentice Hall and Sun Microsystems Press,
 *  .
 *  © 2001 Marty Hall and Larry Brown;
 *  may be freely used or adapted.
 */

public class SocketUtil {
  /** Make a BufferedReader to get incoming data. */

  public static BufferedReader getReader(Socket s)
      throws IOException {
    return(new BufferedReader(
       new InputStreamReader(s.getInputStream())));
  }

  /** Make a PrintWriter to send outgoing data.
   *  This PrintWriter will automatically flush stream
   *  when println is called.
   */

  public static PrintWriter getWriter(Socket s)
      throws IOException {
    // Second argument of true means autoflush.
    return(new PrintWriter(s.getOutputStream(), true));
  }
}

ডেটা কমিউনিকেশন ও কম্পিউটার নেটওয়ার্ক : ক্লায়েন্ট সার্ভার মডেল : (DCN – Client Server Model)

Huge Sell on Popular Electronics

রিদওয়ান বিন শামীম

 

দুটি রিমোট এপ্লিকেশন প্রক্রিয়া দুই ধরণের উপায়ে যোগাযোগ রাখতে পারে,

  • সদৃশ থেকে সদৃশ এপ্লিকেশনে যোগাযোগঃ দুটি রিমোট এপ্লিকেশন একই লেভেলে শেয়ারড রিসোর্স ব্যবহার করে এটি করতে পারে,
  • ক্লায়েন্ট থেকে সার্ভারে যোগাযোগঃ একটি রিমোট প্রক্রিয়া ক্লায়েন্ট হিসেবে সার্ভার রূপে ক্রিয়াশীল অন্য রিমোট প্রক্রিয়ার কাছে রিকোয়েস্ট পাঠাতে পারে। ক্লায়েন্ট সার্ভার মডেলে যেকোনো প্রক্রিয়া ক্লায়েন্ট অথবা সার্ভার হিসেবে কাজ করতে পারে।

ক্লায়েন্ট সার্ভার মডেল

 

যোগাযোগ

ক্লায়েন্ট সার্ভার মডেলে দুটি প্রক্রিয়া বিভিন্ন উপায়ে নিজেদের মধ্যে যোগাযোগ রাখতে পারে,

  • সকেটের মাধ্যমে,
  • রিমোট প্রসেস সেল আরপিসি

 

সকেট

সার্ভাররূপে ক্রিয়াশীল প্রসেস, পোর্ট ব্যবহার করার মাধ্যমে সকেট খোলে, এবং ক্লায়েন্টের রিকোয়েস্টের জন্য অপেক্ষা করে। ক্লায়েন্টরূপে ক্রিয়াশীল প্রক্রিয়া অনুরূপ সকেট খোলে তবে তার কাজ হল রিকোয়েস্ট প্রেরণ করা।

 

রিমোট প্রক্রিয়া সেল

এটি সেই প্রক্রিয়া যেখানে একটি প্রক্রিয়া অন্য আরেকটি প্রক্রিয়ার সাথে সমন্বিত হয়। প্রসেস সেলের মাধ্যমে ক্লায়েন্ট প্রসেস রিমোট হোষ্টে ন্যস্ত থাকে। এদের উভয় প্রক্রিয়া স্লাবের মাধ্যমে সম্পন্ন হয়, যোগাযোগগুলো নিচের প্রক্রিয়াতে হয়ে থাকে,

  • ক্লায়েন্ট প্রসেস ক্লায়েন্ট স্লাবকে কল করে, এটি প্রোগ্রাম সঙ্ক্রান্ত সকল প্যারামিটার প্রেরণ করে।
  • এরপর সকল প্যারামিটার প্যাকড(মার্শালড) হয় এবং সিস্টেম এগুলোকে নেটওয়ার্কের অপর প্রান্তে প্রেরণের উদ্দেশে একটি কল দেয়।
  • কার্নেল নেটওয়ার্কের মাধ্যমে ডাটা প্রেরণ করে এবং অন্য প্রান্ত সেটিকে গ্রহণ করে।
  • রিমোট হোষ্ট সার্ভার স্লাবে ডাটা প্রেরণ করে যেখানে এটি আনমার্শালড।
  • এরপর প্যারামিটারকে প্রক্রিয়ায় পাঠানো হয় এবং এরপরে প্রক্রিয়াটি সম্পন্ন হয়।
  • একই প্রক্রিয়ায় ক্লায়েন্টের কাছে ফলাফল পাঠানো হয়।

 

তথ্যসূত্রঃ http://www.tutorialspoint.com/data_communication_computer_network/client_server_model.htm

 

২৩০ ওয়ার্ড, বোনাস আশা করছি। ধন্যবাদ।

 

অ্যাপ এম এল ক্লায়েন্ট (The AppML Client)

Huge Sell on Popular Electronics

রিদওয়ান বিন শামীম

 

পরবর্তী অধ্যায়গুলোতে আমরা ওয়েব ব্রাউজারে ওয়েব এপ্লিকেশন তৈরি করব।

 

অ্যাপ এম এল ক্লায়েন্ট

অ্যাপ এম এল ক্লায়েন্ট হল একধরনের জাভাস্ক্রিপ্ট যা যেকোনো ওয়েব ব্রাউজারে চলতে পারে।

এটি এক লাইনের কোডের মাধ্যমেই যেকোনো এইচটিএমএল পেজে যোগ করা যায়,


<scriptsrc="http://www.w3schools.com/appml/2.0.3/appml.js"></script>


 

অ্যাপ এম এল ক্লায়েন্ট এইচটিএমএল এট্রিবিউট ব্যবহার করে যেকোনো এইচটিএমএল উপাদানে এক্সটারনাল ডাটা যোগ করার সুবিধা দেয়।


<tableappml-data="customers.js">


 

 

এটির বিল্ট ইন একটি সুবিধা হল, এটি এইচটিএমএলের যেকোনো জায়গায় ডাটা প্রদর্শন করতে পারে।


<td>{{CustomerName}}</td>


 

 

{{ ... }} হল অ্যাপ এম এল ডাটার সংস্থাপন চিহ্ন।

 

এটির বিল্ট ইন আর একটি সুবিধা হল ডাটার ভেতর থাকা কোনও অ্যারি ব্যবহার করে এইচটিএমএলের উপাদানকে পুনরাবৃত্তি করা যায় ।


 <tr appml-repeat="records">
 ..
 .
 </tr>

 

 

অ্যাপ এম এল ক্লায়েন্ট সিএসএস বা এইচটিএমএলের সাথে কোনও সমস্যা করে না, এটি সিএসএসের সাথে ভাল সমন্বয় করতে পারে, আমরা উদাহরণে বুটস্ট্রেপ ব্যবহার করেছি।

 

অ্যাপ এম এল ওয়েব এপ্লিকেশন

অ্যাপ এম এল ওয়েব এপ্লিকেশন বানানোর জন্য খুবই উপযোগী, এর সবচেয়ে বড় গুন হল ব্রাউজারে ডাটাবেস সিআরইউডি এপ্লিকেশনসহ প্রোটোটাইপ বানানোর ক্ষমতা, এবং কোনও ওয়েব সার্ভারেরও দরকার হয় না।


সিআরইউডি বা CRUD: Create, Read, Update, Delete.


 

 

অ্যাপ এম এল সার্ভার

অ্যাপ এম এল দুটি সার্ভার টাইপ ব্যবহার করে, পিএইচপি ও ডটনেট।

অ্যাপ এম এল সার্ভার স্ক্রিপ্ট ব্যবহার করে এসকিউএল ডাটাবেসে যেমন মাইএসকিউএল ও এসকিউএল ডাটাবেসে প্রবেশ করা যায়। এই সার্ভার স্ক্রিপ্ট অত্যন্ত শক্তিশালী, যেকোনো পিএইচপি ও ডটনেট সার্ভারে ইন্সটল করা যায়।

 

অ্যাপ এম এল ওয়েব এসকিউএল

দ্রুত ওয়েব এপ্লিকেশন ডেভলাপমেন্ট ও প্রোটোটাইপিঙের জন্য অ্যাপ এম এল ওয়েব এসকিউএল ব্যবহার করে ব্রাউজারে ওয়েব সার্ভারকে নকল করতে পারে। ওয়েব এসকিউএল হল একধরণের ওয়েবপেজ এপিআই যা এসকিউএল ব্যবহার করে ব্রাউজারে ডাটা সংরক্ষণের কাজ করে। এই এপিআই গুগল ক্রোম, অপেরা, সাফারি ও এনড্রয়েড ব্রাউজারে সমর্থিত। শুধু ব্রাউজারে নিচের স্ক্রিপ্ট সংযোজন করতে হবে,


<scriptsrc="http://www.w3schools.com/appml/2.0.3/appml_sql.js"></script>