Monday, March 9, 2020

Creation of client side

Here we are discussing how to write java code for creating login form for LAN based examination system for client side as follows
User Login form

The client is the program that initiates a network connection. 

Implementing a client consists of five basic steps:
  1. Create a Socket object.
  2. Create an output stream that can be used to send information to the Socket.
  3. Create an input stream to read the response from the server.
  4. Do I/O with input and output streams.
  5. Close the Socket when done.

Note that most of the methods described in these sections throw an IOException and need to be wrapped in a try/catch block.


1. Create a Socket object

Socket is the Java object corresponding to a network connection. A client connects to an existing server that is listening on a numbered network port for a connection. The standard way of making a socket is to supply a hostname or IP address and port as follows:

Socket client = new Socket("hostname", portnumber);


2. Create an output stream that can be used to send information to the Socket

The Java programming language does not have separate methods to send data to files, sockets, and standard output. Instead, Java starts with different underlying objects, then layers standard output streams on top of them. So, any variety of OutputStream available for files is also available for sockets. A common one is PrintWriter. This stream lets you use print and println on the socket in exactly the same way as you would print to the screen. The PrintWriter constructor takes a generic Out putStream as an argument, which you can obtain from the Socket by means of getOutputStream. In addition, you should specify true in the constructor to force autoflush. Normally, the contents written to the stream will remain in a buffer until the buffer becomes completely full. Once the buffer is full, the contents are flushed out the stream. Autoflush guarantees that the buffer is flushed after every println, instead of waiting for the buffer to fill. Here's an example:

printwriter out= new Printwriter(client.getOutputStream(), true);


3. Create an input stream to read the response from the server

Once you send data to the server, you will want to read the server's response. Again, there is no socket-specific way of doing this; you use a standard input stream layered on top of the socket. The most common one is an InputStreamReader, for handling character-based data. Here is a sample:

InputStreamReader in= new InputStreamReader(client.getInputStream());

public void LoginPage()
{
      some code for JFrame.............
}


4. Do I/O with input and output streams

PrintStream has print and println methods that let you send a single primitive value, a String, or a string representation of an Object over the network. If you send an Object, the object is converted to a String by calling the toString method of the class. Most likely you are already familiar with these methods, since System.out is in fact an instance of Print_StreamPrintStream also inherits some simple write methods from OutputStream. These methods let you send binary data by sending an individual byte or an array of bytes.
PrintWriter is similar to PrintStream and has the same print and println methods. The main difference is that you can create print writers for different Unicode character sets, and you can't do that with PrintStream.
BufferedReader has two particularly useful methods: read and readLine. The read method returns a single char (as an int); readLine reads a whole line and returns a String. Both of these methods are blocking; they do not return until data is available. Because readLine will wait until receiving a carriage return or an EOF (the server closed the connection), readLine should be used only when you are sure the server will close the socket when done transmitting or when you know the number of lines that will be sent by the server. The readLine method returns null upon receiving an EOF.
DataInputStream in;
DataOutputStream  out;
   String marks;
Client() throws UnknownHostException, IOException
        {
                LoginPage();
        }
5. Close the Socket when done.
When you are done, close the socket with the close method: 
client.close();

Thankyou!!


29 comments:

  1. Good write up. Easy to understand

    ReplyDelete
  2. Very helpful... keep posting....

    ReplyDelete