{"id":10200,"date":"2015-08-25T07:40:43","date_gmt":"2015-08-25T11:40:43","guid":{"rendered":"http:\/\/bangla.salearningschool.com\/?p=10200"},"modified":"2015-08-24T08:40:36","modified_gmt":"2015-08-24T12:40:36","slug":"implementing-a-server-network-server","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=10200","title":{"rendered":"Implementing a Server : Network Server"},"content":{"rendered":"<p>NetworkServerTest.java\u00a0 Establishes a network Server that listens for client requests on the port specified (command-line argument). Uses the following classes:<\/p>\n<p>&nbsp;<\/p>\n<pre>\/** Taken from Core Web Programming from\r\n\u00a0*\u00a0 Prentice Hall and Sun Microsystems Press,\r\n\u00a0*\u00a0 .\r\n\u00a0*\u00a0 \u00a9 2001 Marty Hall and Larry Brown;\r\n\u00a0*\u00a0 may be freely used or adapted.\r\n\u00a0*\/\r\n\r\n\r\npublic class NetworkServerTest {\r\n\u00a0 public static void main(String[] args) {\r\n\u00a0\u00a0\u00a0 int port = 8088;\r\n\u00a0\u00a0\u00a0 if (args.length &gt; 0) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 port = Integer.parseInt(args[0]);\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0 NetworkServer nwServer = new NetworkServer(port, 1);\r\n\u00a0\u00a0\u00a0 nwServer.listen();\r\n\u00a0 }\r\n}\r\n\r\n\r\n# NetworkServer.java\u00a0 A starting point for network servers. \r\n\r\nimport java.net.*;\r\nimport java.io.*;\r\n\r\n\/** A starting point for network servers. You'll need to\r\n\u00a0*\u00a0 override handleConnection, but in many cases listen can\r\n\u00a0*\u00a0 remain unchanged. NetworkServer uses SocketUtil to simplify\r\n\u00a0*\u00a0 the creation of the PrintWriter and BufferedReader.\r\n\u00a0*\r\n\u00a0*\u00a0 Taken from Core Web Programming from\r\n\u00a0*\u00a0 Prentice Hall and Sun Microsystems Press,\r\n\u00a0*\u00a0 .\r\n\u00a0*\u00a0 \u00a9 2001 Marty Hall and Larry Brown;\r\n\u00a0*\u00a0 may be freely used or adapted.\r\n\u00a0*\/\r\n\r\npublic class NetworkServer {\r\n\u00a0 private int port, maxConnections;\r\n\r\n\u00a0 \/** Build a server on specified port. It will continue to\r\n\u00a0\u00a0 *\u00a0 accept connections, passing each to handleConnection until\r\n\u00a0\u00a0 *\u00a0 an explicit exit command is sent (e.g., System.exit) or\r\n\u00a0\u00a0 *\u00a0 the maximum number of connections is reached. Specify\r\n\u00a0\u00a0 *\u00a0 0 for maxConnections if you want the server to run\r\n\u00a0\u00a0 *\u00a0 indefinitely.\r\n\u00a0\u00a0 *\/\r\n\r\n\u00a0 public NetworkServer(int port, int maxConnections) {\r\n\u00a0\u00a0\u00a0 setPort(port);\r\n\u00a0\u00a0\u00a0 setMaxConnections(maxConnections);\r\n\u00a0 }\r\n\r\n\u00a0 \/** Monitor a port for connections. Each time one is\r\n\u00a0\u00a0 *\u00a0 established, pass resulting Socket to handleConnection.\r\n\u00a0\u00a0 *\/\r\n\r\n\u00a0 public void listen() {\r\n\u00a0\u00a0\u00a0 int i=0;\r\n\u00a0\u00a0\u00a0 try {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 ServerSocket listener = new ServerSocket(port);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 Socket server;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 while((i++ &lt; maxConnections) || (maxConnections == 0)) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 server = listener.accept();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 handleConnection(server);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0 } catch (IOException ioe) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 System.out.println(\"IOException: \" + ioe);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 ioe.printStackTrace();\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0 }\r\n\r\n\u00a0 \/** This is the method that provides the behavior to the\r\n\u00a0\u00a0 *\u00a0 server, since it determines what is done with the\r\n\u00a0\u00a0 *\u00a0 resulting socket. Override this method in servers\r\n\u00a0\u00a0 *\u00a0 you write.\r\n\u00a0\u00a0 * \u00a0\r\n\r\n\r\n\u00a0\u00a0 *\u00a0 This generic version simply reports the host that made\r\n\u00a0\u00a0 *\u00a0 the connection, shows the first line the client sent,\r\n\u00a0\u00a0 *\u00a0 and sends a single line in response.\r\n\u00a0\u00a0 *\/\r\n\r\n\u00a0 protected void handleConnection(Socket server)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 throws IOException{\r\n\u00a0\u00a0\u00a0 BufferedReader in = SocketUtil.getReader(server);\r\n\u00a0\u00a0\u00a0 PrintWriter out = SocketUtil.getWriter(server);\r\n\u00a0\u00a0\u00a0 System.out.println\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 (\"Generic Network Server: got connection from \" +\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 server.getInetAddress().getHostName() + \"\\n\" +\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \"with first line '\" + in.readLine() + \"'\");\r\n\u00a0\u00a0\u00a0 out.println(\"Generic Network Server\");\r\n\u00a0\u00a0\u00a0 server.close();\r\n\u00a0 }\r\n\r\n\u00a0 \/** Gets the max connections server will handle before\r\n\u00a0\u00a0 *\u00a0 exiting. A value of 0 indicates that server should run\r\n\u00a0\u00a0 *\u00a0 until explicitly killed.\r\n\u00a0\u00a0 *\/\r\n\r\n\u00a0 public int getMaxConnections() {\r\n\u00a0\u00a0\u00a0 return(maxConnections);\r\n\u00a0 }\r\n\r\n\u00a0 \/** Sets max connections. A value of 0 indicates that server\r\n\u00a0\u00a0 *\u00a0 should run indefinitely (until explicitly killed).\r\n\u00a0\u00a0 *\/\r\n\r\n\u00a0 public void setMaxConnections(int maxConnections) {\r\n\u00a0\u00a0\u00a0 this.maxConnections = maxConnections;\r\n\u00a0 }\r\n\r\n\u00a0 \/** Gets port on which server is listening. *\/\r\n\r\n\u00a0 public int getPort() {\r\n\u00a0\u00a0\u00a0 return(port);\r\n\u00a0 }\r\n\r\n\u00a0 \/** Sets port. You can only do before \"connect\" is\r\n\u00a0\u00a0 *\u00a0 called. That usually happens in the constructor.\r\n\u00a0\u00a0 *\/\r\n\r\n\u00a0 protected void setPort(int port) {\r\n\u00a0\u00a0\u00a0 this.port = port;\r\n\u00a0 }\r\n}\r\n\r\nSocketUtil.java\u00a0 Simplifies the creation of a PrintWriter and BufferedReader.\r\n\r\n\r\nimport java.net.*;\r\nimport java.io.*;\r\n\r\n\/** A shorthand way to create BufferedReaders and\r\n\u00a0*\u00a0 PrintWriters associated with a Socket.\r\n\u00a0*\r\n\u00a0*\u00a0 Taken from Core Web Programming from \r\n\u00a0*\u00a0 Prentice Hall and Sun Microsystems Press,\r\n\u00a0*\u00a0 \u00a9 2001 Marty Hall and Larry Brown;\r\n\u00a0*\u00a0 may be freely used or adapted. \r\n\u00a0*\/\r\n\r\npublic class SocketUtil {\r\n\u00a0 \/** Make a BufferedReader to get incoming data. *\/\r\n\r\n\u00a0 public static BufferedReader getReader(Socket s)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 throws IOException {\r\n\u00a0\u00a0\u00a0 return(new BufferedReader(\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 new InputStreamReader(s.getInputStream())));\r\n\u00a0 }\r\n\r\n\u00a0 \/** Make a PrintWriter to send outgoing data.\r\n\u00a0\u00a0 *\u00a0 This PrintWriter will automatically flush stream\r\n\u00a0\u00a0 *\u00a0 when println is called.\r\n\u00a0\u00a0 *\/\r\n\r\n\u00a0 public static PrintWriter getWriter(Socket s)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 throws IOException {\r\n\u00a0\u00a0\u00a0 \/\/ Second argument of true means autoflush.\r\n\u00a0\u00a0\u00a0 return(new PrintWriter(s.getOutputStream(), true));\r\n\u00a0 }\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>NetworkServerTest.java\u00a0 Establishes a network Server that listens for client requests on the port specified (command-line argument). Uses the following classes: &nbsp; \/** Taken from Core Web Programming from \u00a0*\u00a0 Prentice Hall and Sun Microsystems Press, \u00a0*\u00a0 . \u00a0*\u00a0 \u00a9 2001 Marty Hall and Larry Brown; \u00a0*\u00a0 may be freely used or adapted. \u00a0*\/ public class &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"http:\/\/bangla.sitestree.com\/?p=10200\">Continue reading<\/a><\/p>\n","protected":false},"author":130,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1417,1424],"tags":[706,308,418,1033,285],"class_list":["post-10200","post","type-post","status-publish","format-standard","hentry","category-code-programming-samples--","category-javaj2eej2me","tag-code","tag-java","tag-network","tag-server","tag-285","item-wrap"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":26599,"url":"http:\/\/bangla.sitestree.com\/?p=26599","url_meta":{"origin":10200,"position":0},"title":"Implementing a Server : Network Server #Programming Code Examples #Java\/J2EE\/J2ME #Network Programming","author":"Author-Check- Article-or-Video","date":"April 29, 2021","format":false,"excerpt":"NetworkServerTest.java Establishes a network Server that listens for client requests on the port specified (command-line argument). Uses the following classes: \/** Taken from Core Web Programming from * Prentice Hall and Sun Microsystems Press, * . * \u00a9 2001 Marty Hall and Larry Brown; * may be freely used or\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":26743,"url":"http:\/\/bangla.sitestree.com\/?p=26743","url_meta":{"origin":10200,"position":1},"title":"ThreadedEchoServer.java  A multithreaded version of EchoServer, where each client request is serviced on a separate thread. Requires the following classes: #Programming Code Examples #Java\/J2EE\/J2ME #Network Programming","author":"Author-Check- Article-or-Video","date":"April 30, 2021","format":false,"excerpt":"ThreadedEchoServer.java A multithreaded version of EchoServer, where each client request is serviced on a separate thread. Requires the following classes: import java.net.*; import java.io.*; \/** A multithreaded variation of EchoServer. * * Taken from Core Web Programming from * Prentice Hall and Sun Microsystems Press, * . * \u00a9 2001\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":26741,"url":"http:\/\/bangla.sitestree.com\/?p=26741","url_meta":{"origin":10200,"position":2},"title":"EchoServer.java  A simple HTTP server that creates a Web page showing all data sent from the client (browser), including all HTTP request headers sent form the client. Uses the following classes: #Programming Code Examples #Java\/J2EE\/J2ME #Network Programming","author":"Author-Check- Article-or-Video","date":"April 30, 2021","format":false,"excerpt":"EchoServer.java A simple HTTP server that creates a Web page showing all data sent from the client (browser), including all HTTP request headers sent form the client. Uses the following classes: import java.net.*; import java.io.*; import java.util.StringTokenizer; \/** A simple HTTP server that generates a Web page showing all *\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10204,"url":"http:\/\/bangla.sitestree.com\/?p=10204","url_meta":{"origin":10200,"position":3},"title":"ThreadedEchoServer.java A multithreaded version of EchoServer, where each client request is serviced on a separate thread. Requires the following classes","author":"","date":"August 25, 2015","format":false,"excerpt":"import java.net.*; import java.io.*; \/** A multithreaded variation of EchoServer. \u00a0* \u00a0*\u00a0 Taken from Core Web Programming from \u00a0*\u00a0 Prentice Hall and Sun Microsystems Press, \u00a0*\u00a0 . \u00a0*\u00a0 \u00a9 2001 Marty Hall and Larry Brown; \u00a0*\u00a0 may be freely used or adapted. \u00a0*\/ public class ThreadedEchoServer extends EchoServer \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 implements\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10202,"url":"http:\/\/bangla.sitestree.com\/?p=10202","url_meta":{"origin":10200,"position":4},"title":"EchoServer.java A simple HTTP server that creates a Web page showing all data sent from the client (browser), including all HTTP request headers sent form the client. Uses the following classes","author":"","date":"August 25, 2015","format":false,"excerpt":"EchoServer.java\u00a0 A simple HTTP server that creates a Web page showing all data sent from the client (browser), including all HTTP request headers sent form the client. Uses the following classes: \u00a0 import java.net.*; import java.io.*; import java.util.StringTokenizer; \/** A simple HTTP server that generates a Web page showing all\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":26489,"url":"http:\/\/bangla.sitestree.com\/?p=26489","url_meta":{"origin":10200,"position":5},"title":"UriRetriever.java  Accepts a host, port, and file from the command line and retrieves the implied URL from the HTTP server by issuing a GET request. Uses the following classes: #Programming Code Examples #NULL #Network Programming","author":"Author-Check- Article-or-Video","date":"April 26, 2021","format":false,"excerpt":"UriRetriever.java Accepts a host, port, and file from the command line and retrieves the implied URL from the HTTP server by issuing a GET request. Uses the following classes: import java.net.*; import java.io.*; \/** Retrieve a URL given the host, port, and file as three * separate command-line arguments. A\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/10200","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/users\/130"}],"replies":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10200"}],"version-history":[{"count":1,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/10200\/revisions"}],"predecessor-version":[{"id":10201,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/10200\/revisions\/10201"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10200"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10200"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10200"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}