Cryptography Reference
In-Depth Information
How these exceptions are handled is up to the application. Once the socket exists between
client and server, both client and server can prepare for input and output by using the get-
InputStream() and getOutputStream() methods from the Socket class. Each returns an Input-
Stream object, and an OutputStream object, respectively. We usually pass these objects into
constructors, which transform the streams into objects that can be more easily read from or
written to; for example, if the server needs to send text data to the client, the programmer
may do something like this:
PrintStream toClient =
new PrintStream(connectionServerSide.getOutputStream());
To send text data, we can use any of the methods from the PrintStream class:
toClient.println(“Howdy, client!”);
The client can set up output in the same way. To receive text data, the client can set up
a BufferedReader object, like this:
BufferedReader fromServer = new BufferedReader(
new InputStreamReader(connectionClientSide.getInputStream());
To receive the text data, we have now at our disposal any of the methods from the Buffered-
Reader class:
String greetings = fromServer.readLine();
One should close a socket prior to exiting a program, or at any time during the program
when we wish to break the connection. Either the client or the server can close the socket,
using the close() method from the socket class, as the server does here:
connectionServerSide.close();
In Java, attempting to close a Socket which has already been closed does nothing. Server-
Sockets should also be closed (once the Socket has been closed, of course):
ss.close();
15.2
DIFFIE-HELLMAN KEY EXCHANGE APPLICATION
You now know everything you need to know to set up a line of communication between com-
puters using Java. Hence, I will now show you a couple of programs called DiffieHell-
manListener (the server) and DiffieHellmanInitiator (the client), which set up a connection
with each other and establish a secret key over an unsecure line. Here is the code for the
server side.
import java.security.*;
import java.math.*;
import java.net.*;
import java.io.*;
public class DiffieHellmanListener {
public static void main(String[] args) throws IOException {
Search WWH ::




Custom Search