Web technologies -- Laboratory 1 -- 2007-2008 -- info.uvt.ro

Important! These pages are somehow outdated and it is recommended to consult the newer version at Web technologies -- 2009-2010 -- info.uvt.ro (by Marc Frâncu).
edit


Introduction

edit

Technologies, protocols, frameworks, methods, concepts:


Sockets

edit

Observation

edit

The laboratory notes Computer networks -- 2007-2008 -- info.uvt.ro/Laboratory 2 contains all these topics, plus more.


Application

edit

Implement a simple TCP client that connects to a given IP address and a given port, issues a given request from the following list and prints the responses:

  • hello
  • get-time
  • get-random

Both requests and responses are given on a single line -- ending with \n.

After testing the client implement the server.


Concepts

edit


Client socket steps

edit
  • Creating the client socket:
Socket socket = new Socket ();
  • Binding to the local socket address:
InetAddress localIpAddress = InetAddress.getByName ("0.0.0.0");
int localIpPort = 0;
SocketAddress localSocketAddress = new InetSocketAddress (localIpAddress, localIpPort);
socket.bind (localSocketAddress);
  • Connecting to the remote socket address:
InetAddress remoteIpAddress = InetAddress.getByName ("www.info.uvt.ro");
int remoteIpPort = 80;
SocketAddress remoteSocketAddress = new InetSocketAddress (remoteIpAddress, remoteIpPort);
socket.connect (remoteSocketAddress);
  • Receiving and/or sending data through inbound and outbound streams:
InputStream input = socket.getInputStream ();
OutputStream output = socket.getOutputStream ();
input.read (buffer, offset, size); ...
output.write (buffer, offset, size); ...
output.flush ();
  • Shutting-down the inbound and outbound streams:
socket.shutdownInput ();
socket.shutdownOutput ();
  • Closing the socket:
socket.close ();


Server socket steps

edit
  • Creating the server socket:
ServerSocket socket = new ServerSocket ();
  • Binding to the local socket address -- this is the one the clients should be connecting to:
InetAddress localIpAddress = InetAddress.getByName ("0.0.0.0");
int localIpPort = 80;
SocketAddress localSocketAddress = new InetSocketAddress (localIpAddress, localIpPort);
socket.bind (localSocketAddress);
  • For each connection accepting a client socket, and:
Socket client = socket.accept ();
    • Receiving and/or sending data;
    • Shutting-down the inbound and outbound streams;
    • Closing the client socket;
These steps are just like the ones described above for the client socket.
  • Closing the server socket;
socket.close ();

Java API

edit


Client implementation

edit
import java.io.*;
import java.net.*;


public class Client
{
    
    public static void main (String[] arguments) throws Exception
    {
        
        if (arguments.length != 3) {
            System.err.println ("Bad usage.");
            return;
        }
        
        String hostName = arguments[0];
        String portName = arguments[1];
        String request = arguments[2];
        
        
        // Creating the client socket
        Socket socket = new Socket ();
        
        // Binding the socket
        socket.bind (new InetSocketAddress (InetAddress.getByName ("0.0.0.0"), 0));
        
        // Resolving the server address and port
        InetAddress address = InetAddress.getByName (hostName);
        int port = Integer.parseInt (portName);
        
        // Connecting the socket
        socket.connect (new InetSocketAddress (address, port));
        
        // Creating a reader and writer from the socket streams
        BufferedReader reader = new BufferedReader (new InputStreamReader (socket.getInputStream ()));
        BufferedWriter writer = new BufferedWriter (new OutputStreamWriter (socket.getOutputStream ()));
        
        // Writing the request
        writer.write (request);
        writer.newLine ();
        
        // Flushing the writer
        writer.flush ();
        
        // Reading the response
        String response = reader.readLine ();
        
        // Closing the socket
        socket.close ();
        
        // Printing the response
        System.out.println (response);
    }
}


Server implementation

edit
import java.io.*;
import java.net.*;


public class Server
{
    
    public static void main (String[] arguments) throws Exception
    {
        
        // Creating the server socket
        ServerSocket server = new ServerSocket ();
        
        // Binding the socket
        server.bind (new InetSocketAddress (InetAddress.getByName ("0.0.0.0"), 7654));
        
        // Looping
        while (true) {
            
            // Accepting a new connection => a new client socket
            final Socket client = server.accept ();
            
            // Starting a new Thread for each client
            new Thread () {
                
                public void run () {
                    
                    // Catching any exceptions
                    try {
                        
                        // Creating a reader and a writer from the socket streams
                        BufferedReader reader = new BufferedReader (new InputStreamReader (client.getInputStream ()));
                        BufferedWriter writer = new BufferedWriter (new OutputStreamWriter (client.getOutputStream ()));
                        
                        // Reading the first request
                        String request = reader.readLine ();
                        
                        // While the connection is still open
                        while (request != null) {
                            
                            // Creating a response based on the request
                            String response;
                            if (request.equals ("hello"))
                                response = "hello back";
                            else if (request.equals ("get-time"))
                                response = "" + System.currentTimeMillis ();
                            else if (request.equals ("get-random"))
                                response = "" + Math.rint (Math.random () * 1000);
                            else if (request.equals ("quit")) {
                                break;
                            } else
                                response = "please?";
                        
                            // Writing the response
                            writer.write (response);
                            writer.newLine ();
                            
                            // Flushing the writer
                            writer.flush ();
                            
                            // Reading the next request
                            request = reader.readLine ();
                        }
                        
                        // Closing the client socket
                        client.close ();
                        
                    } catch (Throwable error) {
                        
                        // Handling the possible errors
                        error.printStackTrace ();
                    }
                }
            } .start ();
        }
    }
}



Ciprian Dorin Crăciun, 2007-10-03, ccraciun@info.uvt.ro