...

Monday, March 8, 2010

Java 02

I'm short of time so I'm going to make this quick. I'll now implement communication through Java. To demonstrate this, I'll have a client and a server. The server will actually echo whatever the client sends.
To do this, we'll need to import the following packages:
import java.net.*;
import java.io.*;


java.net.* is for the Socket class mainly, and java.io.* is for the PrintStream.

We begin by declaring and instantiating a Socket. The socket will be the object you communicate to the opposite through, therefore the socket specifies the IP and Port (which makes a "Socket") of the peer. The socket is instantiated as shown:
Socket socket = new Socket("127.0.0.1",13337);

Now we'll have to work with input and output streams. When you instantiate a socket, you have access to both an input and an output stream. To send, write to the output stream. To read, assign a BufferedReader to handle an input stream and read from the BufferedReader.

First we'll set up the input stream. Instantiate as follows:
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));

The above code instantiates an InputStreamReader which reads the input stream of the socket, which then passes the output to the BufferedReader.

Next, we'll set up the output stream. To write to a stream, we use the PrintStream class. PrintStream can then be used similarly to System.out.println(). To instantiate a PrintStream to use the output stream of the socket, do:
PrintStream printStream = new PrintStream(socket.getOutputStream());

To write to the output stream, use:
printStream.println("Hello!");

Right after writing, we'd expect the server to echo back the reply. The reply is then read by the InputStreamReader and stored in the BufferedReader. We can expect to read from the BufferedReader right after writing, through:
System.out.println(bufferedReader.readLine());

When we're done, close as usual:
printStream.close();
bufferedReader.close();
socket.close();


Now that the client is done, we'll set up the server. In this example, the server will await a connection, and once a connection is made, it will echo any data received back to the client. We'll now import the packages as usual:
import java.net.*;
import java.io.*;


We now specify to listen to connections coming into port 13337:
ServerSocket serverSocket = new ServerSocket(13337);

There is a method called serverSocket.accept() which would return a Socket (remember, with an input and output stream) whenever data is received on the listening port. To read and reply data, we'll first have to instantiate a null Socket object, then repeatedly attempt to assign the output of accept() to it. accept() will block until a connection is made, so no sleep in between each attempt is required.

Implementation is like this:
ServerSocket serverSocket = new ServerSocket(13337);
Socket socket = null;
while(true)
{
socket = serverSocket.accept();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintStream printStream = new PrintStream(socket.getOutputStream());
printStream.println(bufferedReader.readLine()); //Echoes whatever is read
}


This post isn't as short as I want it to be but oh well :)

1 comment :

<