Java Networking Programming Overview
Sockets
- When creating a socket, you pass in the hostname and port number.
- The
java.net.Socketconstructor does thegethostbyname()and thesocket()system call, sets up the server'ssockaddr_instructure, and executes theconnect()call. - All you have to do is catch the errors, which are subclassed from
the familiar
IOException. - You can both read and write on the same Socket.
Reading / Writing Data
- Reading / Writing on the same Socket
- Java allows to both read and write on the same Socket.
- This requirement, copying data in both directions at more or less the same time - from the keyboard to the remote program, and from the remote program to the screen - there are two approaches.
- Some I/O libraries in C have a function called
poll()orselect()that allows you to examine a number of files to see which ones are ready for reading or writing. - Java does not support this model. The other model, which works on most platforms and is the norm in Java, is to use two threads, one to handle the data transfer in each direction.
- The class
Pipeencapsulates 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.
- Reading / Writing Textual Data
- Construct a
BufferedReaderorPrintWriterobject. - Both objects can be created using the socket's
getInputStream()orgetOutputStream()methods. - There is no method to fetch a
ReaderorWriter, partly because some network services are limited to ASCII, but mainly because theSocketclass was decided on before there wereReaderandWriterclasses. You can always create aReaderfrom anInputStreamor aWriterfrom anOutputStreamusing 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);
- Construct a
- Reading / Writing Binary Data
- Construct a
DataInputStreamorDataOutputStreamobject. - Both objects can be created using the socket's
getInputStream()orgetOutputStream()methods. - The simplest paradigm is:
DataInputStream is = new DataInputStream(sock.getInputStream()); DataOutputStream os = new DataOutputStream(sock.getOutputStream());
- If the volumne of data might be large, insert a bufferd
stream for efficiency. The paradigm is:
DataInputStream is = new DataInputStream( new BufferedInputStream(sock.getInputStream())); DataOutputStream os = new DataOutputStream( new BufferedOutputStream(sock.getOutputStream()));
- Construct a
Server Sockets
InetAddress
- The
InetAddressis mainly used for looking up a host's address name or number. - It is also possible to obtain the address at the other end of a connection.
- The
InetAddressobject represents the Internet address of a given computer or host. - There are no public constructors, you can obtain an
InetAddressobject by calling the staticbyNamemethod, passing in either a hostname like wwww.iDevelopment.info or a network address as a String, like "12.4.234.11".