Public Class Inputstreamreaderextends Reader

Public Class Inputstreamreaderextends Reader

public classInputStreamReaderextendsReader

An InputStreamReader is a bridge from byte streams to character streams: It reads bytes and decodes them into characters using a specifiedcharset. The charset that it uses may be specified by name or may be given explicitly, or the platform's default charset may be accepted.

public classBufferedReader extendsReader

Read text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines.

The buffer size may be specified, or the default size may be used. The default is large enough for most purposes.

public classFileInputStream extendsInputStream

AFileInputStreamobtains input bytes from a file in a file system. What files are available depends on the host environment.

FileInputStreamis meant for reading streams of raw bytes such as image data. For reading streams of characters, consider usingFileReader.

public classInetAddressextendsObjectimplementsSerializable

This class represents an Internet Protocol (IP) address.

public InputStreamgetInputStream() throws IOException

Returns input stream bytes for this socket.

public OutputStreamgetOutputStream()

throws IOException

Returns output stream bytes for this socket.

public classPrintWriter extendsWriter

Print formatted representations of objects to a text-output stream. This class implements all of the print methods found in PrintStream. It does not contain methods for writing raw bytes, for which a program should use unencoded byte streams.

public classServerSocket extendsObject

This class implements server sockets. A server socket waits for requests to come in over the network. It performs some operation based on that request, and then possibly returns a result to the requester.

accept()

This method returns a reference to a regular socket that can be used to read and write to the connection client

Using TCP Sockets

Client side:

Socket sock = new Socket(host, port);

String host = host to contact, int port = port

Host can also be InetAddress instead of String

Server side

ServerSocket sock = new ServerSocket(port);

Listen for incoming connections

Socket client = sock.accept();

Reading from a Socket

Typical code:

InputStream is = socket.getInputStream();

InputStreamReader isr = new InputStreamReader(is);

BufferedReader br = new BufferedReader(isr);

Read text by calling br.readLine()

Writing to a Socket

Typical code

OutputStream os = socket.getOutputStream();

OutputStreamWriter osw = new OutputStreamWriter(os);

BufferedWriter bw = new BufferedWriter(osw);

Write by calling one of many write()-functions

See the different classes for different possibilities

Strings need to be converted to bytes with getBytes()

Can also write directly to OutputStream

BufferedWriter only for text output!

DataInputStream

DataInputStream can read binary data from socket

Also can send primitive data types

Typical code

InputStream is = socket.getInputStream();

DataInputStream dis = new DataInputStream(is);

Read binary data with read()

Bonus functionality: Read text with readLine()

But DataInputStream.readLine() is deprecated

DataOutputStream

DataOutputStream can be used to write

Typical code:

OutputStream os = socket.getOutputStream();

DataOutputStream dos = new DataOutputStream(os);

DataOutputStream can also write text and binary

Has writeBytes()-function

no need for String.getBytes()