Rahul Singh, Aakash Singh Rajput, Jyoti Rani Sahu NP Implementation of Generic Networkclient

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Implementing a

Generic Client
Implementing a 01 Create a Socket object.
Generic Client
consists of five 02 Create an output stream that can be used to
send information to the Socket.
basic steps
03
Create an input stream to read the
response from the server.

04 Do I/O with input and output streams.

05 Close the Socket when done


Create a Socket object

A 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);


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);


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());
Do I/O with input and output streams.
A 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_Stream.
PrintStream 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.
Close the Socket when
done
When you are done, close the socket with the close method:

Client.close();

This method closes the associated input and output streams as well.
RAHUL SINGH

OUR TEAM
AAKASH SINGH RAJPUT

JYOTI RANI SAHU


THANK YOU

You might also like