Vous êtes sur la page 1sur 9

1.

BufferedReader
BufferedReader improves performance by buffering input. It has two constructors:
• BufferedReader(Reader inputStream)
→ This form creates a buffered character stream using a default buffer size.
• BufferedReader(Reader inputStream, int bufSize)
→ In this, the size of the buffer is passed in bufSize.

2. PrintWriter
PrintWriter is essentially a character-oriented version of PrintStream. It provides the
formatted output methods print( ) and println( ). PrintWriter has four constructors:
• PrintWriter(OutputStream outputStream)
• PrintWriter(OutputStream outputStream, boolean flushOnNewline)
• PrintWriter(Writer outputStream)
• PrintWriter(Writer outputStream, boolean flushOnNewline)
where flushOnNewline controls whether Java flushes the output stream every time
println( ) is called. If flushOnNewline is true, flushing automatically takes place. If
false,flushing is not automatic.
The first and third constructors do not automatically flush.
Java’s PrintWriter objects support the print( ) and println( ) methods for all types,
including Object. If an argument is not a simple type, the PrintWriter methods will call
the object’s toString( ) method and then print out the result.

3. Socket
A network socket is a lot like an electrical socket.
The same idea applies to network sockets, except we talk about TCP/IP packets and IP
addresses rather than electrons and street addresses.

4. Internet Protocol (IP)


Internet Protocol (IP) is a low-level routing protocol that breaks data into small
packets and sends them to an address across a network, which does not guarantee to
deliver said packets to the destination.

5. Transmission Control Protocol (TCP)


Transmission Control Protocol (TCP) is a higher-level protocol that manages to
robustly string together these packets, sorting and retransmitting them as necessary to
reliably transmit your data.
6. User Datagram Protocol (UDP)
User Datagram Protocol (UDP) is a third protocol that sits next to TCP and can be
used directly to support fast, connectionless, unreliable transport of packets.

7. Client/Server
A server is anything that has some resource that can be shared. There are computer
servers, which provide computing power; print servers, which manage a collection of
printers; disk servers, which provide networked disk space; and web servers, which
store web pages. A client is simply any other entity that wants to gain access to a
particular server.

8. Port
A port is a numbered socket on a particular machine.
A server is allowed to accept multiple clients connected to the same port number,
although each session is unique. To manage multiple client connections, a server
process must be multithreaded or have some other means of multiplexing the
simultaneous I/O.
Port numbers:
• 21 is for FTP
• 23 is for Telnet
• 25 is for e-mail
• 79 is for finger
• 80 is for HTTP
• 119 is for netnews

9. Proxy Server
A proxy server speaks the client side of a protocol to another server. This is often
required when clients have certain restrictions on which servers they can connect to.
Thus, a client would connect to a proxy server, which did not have such restrictions,
and the proxy server would in turn communicate for the client.

10. Classes contained in the java.net package


Some of the classes contained in the java.net package are listed here:
→ Socket
→ ServerSocket
→ SocketImpl
→ SocketPermission
→ MulticastSocket
→ DatagramPacket
→ DatagramSocket
→ DatagramSocketImpl

11. InetAddress
The InetAddress class is used to encapsulate both the numerical IP address we
discussed earlier and the domain name for that address.
The InetAddress class has no visible constructors. To create an InetAddress object,
you have to use one of the available factory methods.
Factory methods are merely a convention whereby static methods in a class return an
instance of that class. Three commonly used InetAddress factory methods are shown
here.
• static InetAddress getLocalHost( )
throws UnknownHostException
• static InetAddress getByName(String hostName)
throws UnknownHostException
• static InetAddress[ ] getAllByName(String hostName)
throws UnknownHostException
The getLocalHost( ) method simply returns the InetAddress object that represents the
local host.
The getByName( ) method returns an InetAddress for a host name passed to it. If
these methods are unable to resolve the host name, they throw an
UnknownHostException.
The getAllByName( ) factory method returns an array of InetAddresses that represent
all of the addresses that a particular name resolves to. It will also throw an
UnknownHostException if it can’t resolve the name to at least one address.
The factory method getByAddress( ), which takes an IP address and returns an
InetAddress object.

12. TCP/IP Client Sockets


A socket can be used to connect Java’s I/O system to other programs that may reside
either on the local machine or on any other machine on the Internet.
There are two kinds of TCP sockets in Java. One is for servers, and the other is for
clients. The ServerSocket class is designed to be a “listener,” which waits for clients to
connect before doing anything. The Socket class is designed to connect to server
sockets and initiate protocol exchanges.
The creation of a Socket object implicitly establishes a connection between the client
and server. Here are two constructors used to create client sockets:
• Socket(String hostName, int port)
→ Creates a socket connecting the local host to the named host and port
→ Can throw an UnknownHostException or an IOException.
• Socket(InetAddress ipAddress, int port)
→ Creates a socket using a preexisting InetAddress object and a port
→ Can throw an IOException.
Methods:
• InetAddress getInetAddress( )
→ Returns the InetAddress associated with the Socket object.
• int getPort( )
→ Returns the remote port to which this Socket object is connected.
• int getLocalPort( )
→ Returns the local port to which this Socket object is connected.

• InputStream getInputStream( )
→ Returns the InputStream associated with the invoking socket.
• OutputStream getOutputStream( )
→ Returns the OutputStream associated with the invoking socket.

13. TCP/IP ServerSocket


The ServerSocket class is used to create servers that listen for either local or remote
client programs to connect to them on published ports. ServerSockets are quite
different from normal Sockets. When you create a ServerSocket, it will register itself
with the system as having an interest in client connections.
Here are the constructors:
• ServerSocket(int port)
→ Creates server socket on the specified port with a queue length of 50.
• ServerSocket(int port, int maxQueue)
→ Creates a server socket on the specified port with a maximum queue
length of maxQueue.
• ServerSocket(int port, int maxQueue, InetAddress localAddress)
→ Creates a server socket on the specified port with a maximum queue
length of maxQueue. On a multihomed host, localAddress specifies the
IP address to which this socket binds.
The method called accept( ), which is a blocking call that will wait for a client to
initiate communications, and then return with a normal Socket that is then used for
communication with the client.

14. Datagrams
Datagrams are bundles of information passed between machines. Once the datagram
has been released to its intended target, there is no assurance that it will arrive or even
that someone will be there to catch it. Likewise, when the datagram is received, there
is no assurance that it hasn’t been damaged in transit or that whoever sent it is still
there to receive a response.
Java implements datagrams on top of the UDP protocol by using two classes: The
DatagramPacket object is the data container, while the DatagramSocket is the
mechanism used to send or receive the DatagramPackets.

15. DatagramPacket
Here are the four constructors:
• DatagramPacket(byte data[ ], int size)
→ This constructor specifies a buffer that will receive data, and the size of a
packet. It is used for receiving data over a DatagramSocket.
• DatagramPacket(byte data[ ], int offset, int size)
→ This form allows you to specify an offset into the buffer at which data will
be stored.
• DatagramPacket(byte data[ ], int size, InetAddress ipAddress, int port)
→ This form specifies a target address and port, which are used by a
DatagramSocket to determine where the data in the packet will be sent.
• DatagramPacket(byte data[ ], int offset, int size, InetAddress ipAddress, int
port)
→ This form transmits packets beginning at the specified offset into the data.

Here are some of the most commonly used methods:


• InetAddress getAddress( )
→ Returns the destination InetAddress, typically used for sending.
• int getPort( )
→ Returns the port number.
• byte[ ] getData( )
→ Returns the byte array of data contained in the datagram. Mostly used to
retrieve data from the datagram after it has been received.
• int getLength( )
→ Returns the length of the valid data contained in the byte array that would
be returned from the getData( ) method. This typically does not equal the
length of the whole byte array.

16. Setting up a server process requires following steps...


i. Create a ServerSocket object. The ServerSocket constructor requires a
port number (1024-65535, for non-reserved ones) as an argument. For
example:
→ ServerSocket servSock = new ServerSocket(1234);
ii. Put the server into a waiting state. The server waits indefinitely
('blocks') for a client to connect. It does this by calling method accept ()
of class ServerSocket, which returns a Socket object when a connection is
made. For example:
→ Socket link = servSock.accept ();
iii. Set up input and output streams. Methods getInputStream () and
getOutputStream () of classSocket are used to get references to streams
associated with the socket returned in step 2. These streams will be used
for communication with the client that has just made connection. For
example:
→ Scanner input = new Scanner (link.getInputStream ());
Similarly, we can wrap a PrintWriter object around the OutputStream object
returned by method getOutputStream ().For example:
→ PrintWriter output = new PrintWriter(link.getOutputStream(),true);
iv. Send and receive data. Having set up our Scanner and PrintWriter
objects, sending and receiving data is very straightforward. We simply
use method nextLine () for receiving data and method println () for
sending data. For example:
→ output.println ("Awaiting data...");
→ String input = input.nextLine();
v. Close the connection (after completion of the dialogue). This is achieved
via method close of class Socket. For example:
→ link.close();
17. Setting up the corresponding client involves following steps...
i. Establish a connection to the server. We create a Socket object, supplying its
constructor with the following two arguments:
→ The server's IP address (of type InetAddress);
→ The appropriate port number for the service.
ii. For simplicity's sake, we shall place client and server on the same host, which
will allow us to retrieve the IP address by calling static method getLocalHost ()
of class InetAddress. For example:
→ Socket link = new Socket(InetAddress.getLocalHost(),1234);
iii. Set up input and output streams. These are set up in exactly the same way as
the server streams were set up (by calling methods getInputStream () and
getOutputStream () of the Socket object that was created in step 2).
iv. Send and receive data. The Scanner object at the client end will receive
messages sent by the PrintWriter object at the server end, while the PrintWriter
object at the client end will send messages that are received by the Scanner
object at the server end (using methods nextLine and println respectively).
v. Close the connection. This is exactly the same as for the server process (using
method close of class Socket).

18. Setting up a datagram socket requires following steps...


i. Create a DatagramSocket object. Just as for the creation of a ServerSocket
object, this means supplying the object's constructor with the port number. For
example:
→ DatagramSocket datagramSocket = new DatagramSocket (1234);
ii. Create a buffer for incoming datagrams. This is achieved by creating an
array of bytes. For example:
→ byte [] buffer = new byte [256];
iii. Create a DatagramPacket object for the incoming datagrams. The
constructor for this object requires two arguments:
• The previously-created byte array;
• The size of this array.
For example:
→ DatagramPacket inPacket =new Datagram Packet(buffer, buffer. length);
iv. Accept an incoming datagram. This is effected via the receive () method of
our DatagramSocket object, using our DatagramPacket object as the receptacle.
For example:
→ datagramSocket.receive(inPacket);
v. Accept the sender's address and port from the packet. Methods getAddress
() and getPort () of our DatagramPacket object are used for this. For example:
→ InetAddress clientAddress = inPacket.getAddress();
→ int clientPort = inPacket.getPort();
vi. Retrieve the data from the buffer. For convenience of handling, the data will
be retrieved as a string, using an overloaded form of the String constructor that
takes three arguments:
→ A byte array;
→ The start position within the array (= 0 here);
→ The number of bytes (= full size of buffer here).
For example:
→ String message = new String(inPacket.getData(), 0,inPacket.getLength());
vii. Create the response datagram. Create a DatagramPacket object, using an
overloaded form of the constructor that takes four arguments:
• The byte array containing the response message;
• The size of the response;
• The client's address;
• The client's port number.
The first of these arguments is returned by the getBytes () method of the String
class. For example:
→ DatagramPacket outPacket = new DatagramPacket(response.getBytes(),
response.length(),clientAddress, clientPort);
viii. Send the response datagram. This is achieved by calling method send of our
DatagramSocket object, supplying our outgoing DatagramPacket object as an
argument. For example:
→ datagramSocket.send(outPacket);

Under normal circumstances, the server would probably not be closed down at all.
However, if an exception occurs, then the associated DatagramSocket should be
closed
ix. Close the DatagramSocket. This is effected simply by calling method close of
our DatagramSocket object. For example:
→ datagramSocket.close();

Vous aimerez peut-être aussi