About socket programming and REST design <p>We need to design a server that will serve a webpage to several clients but also query a remote database for these clients. One of the requirement for this project is that the whole system must be compliant with the REST architecture style. We need use Java as programming language but many questions arised while we were designing it.</p> <p>We want to have a main thread that will get connections, as shown in this example:</p> <pre><code>// System.out.println("Starting a new web server using port " + port) try { ServerSocket reciever = new ServerSocket(port); while (true) { try { Socket s = reciever.accept(); Client c = new Client(s); } catch (IOException e) { System.err.println("New item creation failed."); IOUtil.close(reciever); } catch (InterruptedException e) { e.printStackTrace(); } } } catch (IOException e) { System.err.println("ServerSocket problem."); } </code></pre> <p>Then each connection will be created as a new thread (the Client object in the code) that will take care of reading ONE request. If the request is a GET, then the thread will serve the resource to the client. If it is a POST, then it will add the request to a buffer and let another thread handle the query to the database and also the answer back to the client. After handling this only request, the thread closes the socket and terminates. </p> <p>Is the use of sockets violating the REST principle? In order to respect the REST architecture, do we need to destroy every Client object (thread &amp; socket) after each HTTP message? Is there another way of client-server communication that does not use sockets?</p>