Java Networking Programming Overview
Return to the Java Programming Corner.
Sockets
java.net.Socket constructor does the
gethostbyname() and the socket() system
call, sets up the server's sockaddr_in structure,
and executes the connect() call.
IOException.
Reading / Writing Data
poll()
or select() that allows you to examine a number of
files to see which ones are ready for reading or writing.
Pipe encapsulates on threads and the
code for copying data in one direction; two instances are used,
one to drive each direction of transfer independently of the
other.
BufferedReader or
PrintWriter object.
getInputStream() or getOutputStream()
methods.
Reader or
Writer, partly because some network services
are limited to ASCII, but mainly because the
Socket class was decided on before there were
Reader and Writer classes. You can
always create a Reader from an
InputStream or a Writer from an
OutputStream using the conversion classes. The
paradigm for the two most common forms is:
BufferedReader is = new BufferedReader (
new InputStreamReader(sock.getInputStream()));
PrintWriter os = new PrintWriter(sock.getOutputStream(), true);
DataInputStream or
DataOutputStream object.
getInputStream() or getOutputStream()
methods.
DataInputStream is = new DataInputStream(sock.getInputStream()); DataOutputStream os = new DataOutputStream(sock.getOutputStream());
DataInputStream is = new DataInputStream(
new BufferedInputStream(sock.getInputStream()));
DataOutputStream os = new DataOutputStream(
new BufferedOutputStream(sock.getOutputStream()));
Server Sockets
InetAddress
InetAddress is mainly used for looking up a host's
address name or number.
InetAddress object represents the Internet address
of a given computer or host.
InetAddress object by calling the static
byName method, passing in either a hostname like
wwww.iDevelopment.info or a network address as a String,
like "12.4.234.11".