Web technologies -- Laboratory 1 -- 2007-2008 -- info.uvt.ro
Navigation
edit- Web technologies -- 2007-2008 -- info.uvt.ro
- Web technologies -- Laboratory 1 -- 2007-2008 -- info.uvt.ro
- Web technologies -- Laboratory 2 -- 2007-2008 -- info.uvt.ro
- Web technologies -- Laboratory 3 -- 2007-2008 -- info.uvt.ro
- Web technologies -- Laboratory 4 -- 2007-2008 -- info.uvt.ro
- Web technologies -- Laboratory 5 -- 2007-2008 -- info.uvt.ro
- Web technologies -- Laboratory 6 -- 2007-2008 -- info.uvt.ro
- Web technologies -- Laboratory 7 -- 2007-2008 -- info.uvt.ro
- Web technologies -- Laboratory 8 -- 2007-2008 -- info.uvt.ro
- Web technologies -- Laboratory 9 -- 2007-2008 -- info.uvt.ro
- Web technologies -- Laboratory 10 -- 2007-2008 -- info.uvt.ro
Introduction
editTechnologies, protocols, frameworks, methods, concepts:
- Internet
- World Wide Web
- HTTP -- Hyper Text Transfer Protocol
- URI -- Uniform Resource Identifier
- URL -- Uniform Resource Locator
- URN -- Uniform Resource Name
- HTTPS -- HTTP Secure
- TCP/IP
- DNS -- Domain Name System
- HTML -- Hyper Text Markup Language
- CSS -- Cascading Style Sheet
- Web 2.0
- W3C -- World Wide Web Consortium
- Semantic web
- Application server
- Web application
- Web server
- Web service
- Web browser
- Thin client
- Java Applet
- Java Servlet
- JSP -- Java Server Pages
- JSF -- Java Server Faces
- AJAX -- Asynchronous JavaScript and XML
- XML -- Extensible Markup Language
- XHTML
- XForms
- XSL -- Extensible Stylesheet Language
- SOAP -- Simple Object Access Protocol
- Proxy
- CGI -- Common Gateway Interface
- RDF -- Resource Description Framework
- RSS
- REST -- Representational State Transfer
Sockets
editObservation
editThe laboratory notes Computer networks -- 2007-2008 -- info.uvt.ro/Laboratory 2 contains all these topics, plus more.
Application
editImplement 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
editimport 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
editimport 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