{"id":10183,"date":"2015-08-21T07:15:48","date_gmt":"2015-08-21T11:15:48","guid":{"rendered":"http:\/\/bangla.salearningschool.com\/?p=10183"},"modified":"2015-08-01T09:16:15","modified_gmt":"2015-08-01T13:16:15","slug":"networkclienttest-java-makes-a-simple-connection-to-the-host-and-port-specified-on-the-command-line-uses-the-following-classes","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=10183","title":{"rendered":"NetworkClientTest.java Makes a simple connection to the host and port specified on the command line. Uses the following classes"},"content":{"rendered":"<pre>NetworkClientTest.java\u00a0 Makes a simple connection to the host and port specified on the command line. Uses the following classes:\r\n\r\n\/** Make simple connection to host and port specified.\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 NetworkClientTest {\r\n\u00a0 public static void main(String[] args) {\r\n\u00a0\u00a0\u00a0 String host = \"localhost\";\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 host = args[0];\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0 if (args.length &gt; 1) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 port = Integer.parseInt(args[1]);\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0 NetworkClient nwClient = new NetworkClient(host, port);\r\n\u00a0\u00a0\u00a0 nwClient.connect();\r\n\u00a0 }\r\n}\r\n\r\n\r\n\r\nNetworkClient.java\u00a0 Starting point for a network client to communicate with a remote computer.\r\n\r\nimport java.net.*;\r\nimport java.io.*;\r\n\r\n\/** A starting point for network clients. You'll need to\r\n\u00a0*\u00a0 override handleConnection, but in many cases connect can\r\n\u00a0*\u00a0 remain unchanged. It uses SocketUtil to simplify the\r\n\u00a0*\u00a0 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 NetworkClient {\r\n\u00a0 protected String host;\r\n\u00a0 protected int port;\r\n\r\n\u00a0 \/** Register host and port. The connection won't\r\n\u00a0\u00a0 *\u00a0 actually be established until you call\r\n\u00a0\u00a0 *\u00a0 connect.\r\n\u00a0\u00a0 *\/\r\n\r\n\u00a0 public NetworkClient(String host, int port) {\r\n\u00a0\u00a0\u00a0 this.host = host;\r\n\u00a0\u00a0\u00a0 this.port = port;\r\n\u00a0 }\r\n\r\n\u00a0 \/** Establishes the connection, then passes the socket\r\n\u00a0\u00a0 *\u00a0 to handleConnection.\r\n\u00a0\u00a0 *\/\r\n\r\n\u00a0 public void connect() {\r\n\u00a0\u00a0\u00a0 try {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 Socket client = new Socket(host, port);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 handleConnection(client);\r\n\u00a0\u00a0\u00a0 } catch(UnknownHostException uhe) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 System.out.println(\"Unknown host: \" + host);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 uhe.printStackTrace();\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 you will override when\r\n\u00a0\u00a0 *\u00a0 making a network client for your task.\r\n\u00a0\u00a0 *\u00a0 The default version sends a single line\r\n\u00a0\u00a0 *\u00a0 (\"Generic Network Client\") to the server,\r\n\u00a0\u00a0 *\u00a0 reads one line of response, prints it, then exits.\r\n\u00a0\u00a0 *\/\r\n\r\n\u00a0 protected void handleConnection(Socket client)\r\n\u00a0\u00a0\u00a0 throws IOException {\r\n\u00a0\u00a0\u00a0 PrintWriter out = SocketUtil.getWriter(client);\r\n\u00a0\u00a0\u00a0 BufferedReader in = SocketUtil.getReader(client);\r\n\u00a0\u00a0\u00a0 out.println(\"Generic Network Client\");\r\n\u00a0\u00a0\u00a0 System.out.println\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 (\"Generic Network Client:\\n\" +\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \"Made connection to \" + host +\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \" and got '\" + in.readLine() + \"' in response\");\r\n\u00a0\u00a0\u00a0 client.close();\r\n\u00a0 }\r\n\r\n\u00a0 \/** The hostname of the server we're contacting. *\/\r\n\r\n\u00a0 public String getHost() {\r\n\u00a0\u00a0\u00a0 return(host);\r\n\u00a0 }\r\n\r\n\u00a0 \/** The port connection will be made on. *\/\r\n\r\n\u00a0 public int getPort() {\r\n\u00a0\u00a0\u00a0 return(port);\r\n\u00a0 }\r\n}\r\n\r\n\r\nSocketUtil.java\u00a0 Provides utilities for wrapping a BufferedReader around the Socket's input stream and a PrintWriter\u00a0 around the Socket's output stream\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 .\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>NetworkClientTest.java\u00a0 Makes a simple connection to the host and port specified on the command line. Uses the following classes: \/** Make simple connection to host and port specified. \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 &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"http:\/\/bangla.sitestree.com\/?p=10183\">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":[840,706,308,418,285],"class_list":["post-10183","post","type-post","status-publish","format-standard","hentry","category-code-programming-samples--","category-javaj2eej2me","tag-client","tag-code","tag-java","tag-network","tag-285","item-wrap"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":26583,"url":"http:\/\/bangla.sitestree.com\/?p=26583","url_meta":{"origin":10183,"position":0},"title":"NetworkClientTest.java  Makes a simple connection to the host and port specified on the command line. Uses the following classes: #Programming Code Examples #Java\/J2EE\/J2ME #Network Programming","author":"Author-Check- Article-or-Video","date":"April 29, 2021","format":false,"excerpt":"NetworkClientTest.java Makes a simple connection to the host and port specified on the command line. Uses the following classes: \/** Make simple connection to host and port specified. * * Taken from Core Web Programming from * Prentice Hall and Sun Microsystems Press, * . * \u00a9 2001 Marty Hall\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":10192,"url":"http:\/\/bangla.sitestree.com\/?p=10192","url_meta":{"origin":10183,"position":1},"title":"UrlRetriever.java Accepts an URL from the command line, parses the host, port, and URI components from the URL and then retrieves the document. Requires the following classes","author":"","date":"August 24, 2015","format":false,"excerpt":"UrlRetriever.java\u00a0 Accepts an URL from the command line, parses the host, port, and URI components from the URL and then retrieves the document. Requires the following classes: import java.util.*; \/** This parses the input to get a host, port, and file, then \u00a0*\u00a0 passes these three values to the UriRetriever\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":26591,"url":"http:\/\/bangla.sitestree.com\/?p=26591","url_meta":{"origin":10183,"position":2},"title":"UrlRetriever.java  Accepts an URL from the command line, parses the host, port, and URI components from the URL and then retrieves the document. Requires the following classes: #Programming Code Examples #Java\/J2EE\/J2ME #Network Programming","author":"Author-Check- Article-or-Video","date":"April 29, 2021","format":false,"excerpt":"UrlRetriever.java Accepts an URL from the command line, parses the host, port, and URI components from the URL and then retrieves the document. Requires the following classes: import java.util.*; \/** This parses the input to get a host, port, and file, then * passes these three values to the UriRetriever\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":26489,"url":"http:\/\/bangla.sitestree.com\/?p=26489","url_meta":{"origin":10183,"position":3},"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":[]},{"id":10189,"url":"http:\/\/bangla.sitestree.com\/?p=10189","url_meta":{"origin":10183,"position":4},"title":"AddressVerifier.java Connects to an SMTP server and issues a expn request to display details about a mailbox on the server. Uses the following classes","author":"","date":"August 24, 2015","format":false,"excerpt":"import java.net.*; import java.io.*; \/** Given an e-mail address of the form user@host, \u00a0*\u00a0 connect to port 25 of the host and issue an \u00a0*\u00a0 'expn' request for the user. Print the results. \u00a0* \u00a0*\u00a0 Taken from Core Web Programming from \u00a0*\u00a0 Prentice Hall and Sun Microsystems Press, \u00a0*\u00a0 .\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":26589,"url":"http:\/\/bangla.sitestree.com\/?p=26589","url_meta":{"origin":10183,"position":5},"title":"AddressVerifier.java  Connects to an SMTP server and issues a expn request to display details about a mailbox on the server. Uses the following classes: #Programming Code Examples #Java\/J2EE\/J2ME #Network Programming","author":"Author-Check- Article-or-Video","date":"April 29, 2021","format":false,"excerpt":"AddressVerifier.java Connects to an SMTP server and issues a expn request to display details about a mailbox on the server. Uses the following classes: import java.net.*; import java.io.*; \/** Given an e-mail address of the form user@host, * connect to port 25 of the host and issue an * 'expn'\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\/10183","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=10183"}],"version-history":[{"count":1,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/10183\/revisions"}],"predecessor-version":[{"id":10184,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/10183\/revisions\/10184"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10183"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10183"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10183"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}