{"id":26599,"date":"2021-04-29T23:10:08","date_gmt":"2021-04-30T03:10:08","guid":{"rendered":"http:\/\/bangla.salearningschool.com\/recent-posts\/implementing-a-server-network-server-programming-code-examples-java-j2ee-j2me-network-programming\/"},"modified":"2021-04-29T23:10:08","modified_gmt":"2021-04-30T03:10:08","slug":"implementing-a-server-network-server-programming-code-examples-java-j2ee-j2me-network-programming","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=26599","title":{"rendered":"Implementing a Server : Network Server #Programming Code Examples #Java\/J2EE\/J2ME #Network Programming"},"content":{"rendered":"<pre>\nNetworkServerTest.java  Establishes a network Server that listens for client requests on the port specified (command-line argument). Uses the following classes: \n\n\n\/** Taken from Core Web Programming from\n *  Prentice Hall and Sun Microsystems Press,\n *  .\n *  &copy; 2001 Marty Hall and Larry Brown;\n *  may be freely used or adapted.\n *\/\n\n\npublic class NetworkServerTest {\n  public static void main(String[] args) {\n    int port = 8088;\n    if (args.length &gt; 0) {\n      port = Integer.parseInt(args[0]);\n    }\n    NetworkServer nwServer = new NetworkServer(port, 1);\n    nwServer.listen();\n  }\n}\n\n\n# NetworkServer.java  A starting point for network servers. \n\nimport java.net.*;\nimport java.io.*;\n\n\/** A starting point for network servers. You'll need to\n *  override handleConnection, but in many cases listen can\n *  remain unchanged. NetworkServer uses SocketUtil to simplify\n *  the creation of the PrintWriter and BufferedReader.\n *\n *  Taken from Core Web Programming from\n *  Prentice Hall and Sun Microsystems Press,\n *  .\n *  &copy; 2001 Marty Hall and Larry Brown;\n *  may be freely used or adapted.\n *\/\n\npublic class NetworkServer {\n  private int port, maxConnections;\n\n  \/** Build a server on specified port. It will continue to\n   *  accept connections, passing each to handleConnection until\n   *  an explicit exit command is sent (e.g., System.exit) or\n   *  the maximum number of connections is reached. Specify\n   *  0 for maxConnections if you want the server to run\n   *  indefinitely.\n   *\/\n\n  public NetworkServer(int port, int maxConnections) {\n    setPort(port);\n    setMaxConnections(maxConnections);\n  }\n\n  \/** Monitor a port for connections. Each time one is\n   *  established, pass resulting Socket to handleConnection.\n   *\/\n\n  public void listen() {\n    int i=0;\n    try {\n      ServerSocket listener = new ServerSocket(port);\n      Socket server;\n      while((i++ &lt; maxConnections) || (maxConnections == 0)) {\n        server = listener.accept();\n        handleConnection(server);\n      }\n    } catch (IOException ioe) {\n      System.out.println(&quot;IOException: &quot; + ioe);\n      ioe.printStackTrace();\n    }\n  }\n\n  \/** This is the method that provides the behavior to the\n   *  server, since it determines what is done with the\n   *  resulting socket. <b>Override this method in servers\n   *  you write.\n   *  <p>\n   *  This generic version simply reports the host that made\n   *  the connection, shows the first line the client sent,\n   *  and sends a single line in response.\n   *\/\n\n  protected void handleConnection(Socket server)\n      throws IOException{\n    BufferedReader in = SocketUtil.getReader(server);\n    PrintWriter out = SocketUtil.getWriter(server);\n    System.out.println\n      (&quot;Generic Network Server: got connection from &quot; +\n       server.getInetAddress().getHostName() + &quot;n&quot; +\n       &quot;with first line '&quot; + in.readLine() + &quot;'&quot;);\n    out.println(&quot;Generic Network Server&quot;);\n    server.close();\n  }\n\n  \/** Gets the max connections server will handle before\n   *  exiting. A value of 0 indicates that server should run\n   *  until explicitly killed.\n   *\/\n\n  public int getMaxConnections() {\n    return(maxConnections);\n  }\n\n  \/** Sets max connections. A value of 0 indicates that server\n   *  should run indefinitely (until explicitly killed).\n   *\/\n\n  public void setMaxConnections(int maxConnections) {\n    this.maxConnections = maxConnections;\n  }\n\n  \/** Gets port on which server is listening. *\/\n\n  public int getPort() {\n    return(port);\n  }\n\n  \/** Sets port. <b>You can only do before &quot;connect&quot; is\n   *  called.<\/b> That usually happens in the constructor.\n   *\/\n\n  protected void setPort(int port) {\n    this.port = port;\n  }\n}\n\nSocketUtil.java  Simplifies the creation of a PrintWriter and BufferedReader.\n\n\nimport java.net.*;\nimport java.io.*;\n\n\/** A shorthand way to create BufferedReaders and\n *  PrintWriters associated with a Socket.\n *\n *  Taken from Core Web Programming from \n *  Prentice Hall and Sun Microsystems Press,\n *  &copy; 2001 Marty Hall and Larry Brown;\n *  may be freely used or adapted. \n *\/\n\npublic class SocketUtil {\n  \/** Make a BufferedReader to get incoming data. *\/\n\n  public static BufferedReader getReader(Socket s)\n      throws IOException {\n    return(new BufferedReader(\n       new InputStreamReader(s.getInputStream())));\n  }\n\n  \/** Make a PrintWriter to send outgoing data.\n   *  This PrintWriter will automatically flush stream\n   *  when println is called.\n   *\/\n\n  public static PrintWriter getWriter(Socket s)\n      throws IOException {\n    \/\/ Second argument of true means autoflush.\n    return(new PrintWriter(s.getOutputStream(), true));\n  }\n}\n\n<\/p><\/b><\/pre>\n<p>Note: Brought from our old site: http:\/\/www.salearningschool.com\/example_codes\/ on Jan 2nd, 2017 From: http:\/\/sitestree.com\/?p=10235<br \/> Categories:Programming Code Examples, Java\/J2EE\/J2ME, Network Programming<br \/>Tags:Java\/J2EE\/J2MENetwork Programming<br \/> Post Data:2017-01-02 16:04:28<\/p>\n<p>\t\tShop Online: <a href='https:\/\/www.ShopForSoul.com\/' target='new' rel=\"noopener\">https:\/\/www.ShopForSoul.com\/<\/a><br \/>\n\t\t(Big Data, Cloud, Security, Machine Learning): Courses: <a href='http:\/\/Training.SitesTree.com' target='new' rel=\"noopener\"> http:\/\/Training.SitesTree.com<\/a><br \/>\n\t\tIn Bengali: <a href='http:\/\/Bangla.SaLearningSchool.com' target='new' rel=\"noopener\">http:\/\/Bangla.SaLearningSchool.com<\/a><br \/>\n\t\t<a href='http:\/\/SitesTree.com' target='new' rel=\"noopener\">http:\/\/SitesTree.com<\/a><br \/>\n\t\t8112223 Canada Inc.\/JustEtc: <a href='http:\/\/JustEtc.net' target='new' rel=\"noopener\">http:\/\/JustEtc.net (Software\/Web\/Mobile\/Big-Data\/Machine Learning) <\/a><br \/>\n\t\tShop Online: <a href='https:\/\/www.ShopForSoul.com'> https:\/\/www.ShopForSoul.com\/<\/a><br \/>\n\t\tMedium: <a href='https:\/\/medium.com\/@SayedAhmedCanada' target='new' rel=\"noopener\"> https:\/\/medium.com\/@SayedAhmedCanada <\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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, * . * &copy; 2001 Marty Hall and Larry Brown; * may be freely used or adapted. *\/ public class NetworkServerTest &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"http:\/\/bangla.sitestree.com\/?p=26599\">Continue reading<\/a><\/p>\n","protected":false},"author":8,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1917],"tags":[],"class_list":["post-26599","post","type-post","status-publish","format-standard","hentry","category-fromsitestree-com","item-wrap"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":10200,"url":"http:\/\/bangla.sitestree.com\/?p=10200","url_meta":{"origin":26599,"position":0},"title":"Implementing a Server : Network Server","author":"","date":"August 25, 2015","format":false,"excerpt":"NetworkServerTest.java\u00a0 Establishes a network Server that listens for client requests on the port specified (command-line argument). Uses the following classes: \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\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":26743,"url":"http:\/\/bangla.sitestree.com\/?p=26743","url_meta":{"origin":26599,"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":26599,"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":26599,"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":26599,"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":26599,"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\/26599","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\/8"}],"replies":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=26599"}],"version-history":[{"count":0,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/26599\/revisions"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=26599"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=26599"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=26599"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}