Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2011
    Posts
    16
    Rep Power
    0

    Default Problem! Help Apreciated

    Any idea on how I can modify this code so the IP address can be supplied on the command line instead of being hard-wired.

    Java Code:
    //
    // DateClient.java
    // This attempts to connect to the daytime port of a server.
    // e.g. java DateClient 127.0.0.1
    //
    import java.io.*;
    import java.net.*;
    
    public class DateClient {
    
    public final static int DEFAULT_PORT = 13; // a constant!!
    // declare variables ready for use by all the methods.
    private static Socket dateSocket = null;
    private static InputStreamReader isr = null;
    private static BufferedReader in = null;
    
    public void DateClient() {
    } // empty constructor
    public static void main(String[] args) throws IOException {
    String host = "127.0.0.1";
    InputStreamReader keyIn = null;
    int port = DEFAULT_PORT;
    getDateSocket(host, port);
    // Use BufferedReader to read a whole line
    String dateTime = in.readLine();
    System.out.println("BufferedReader DateTime: " + dateTime);
    // the server will close down its connection so close ours
    in.close();
    isr.close();
    dateSocket.close();
    // now open it again to try using InputStreamReader instead
    } // main()
    //
    // Method to open a socket, connect to the server.
    // It also creates a BufferedReader and an InputStreamReader!
    //
    private static void getDateSocket(String host, int portNum) {
    try {
    dateSocket = new Socket(host, portNum);
    isr = new InputStreamReader(dateSocket.getInputStr…
    // might need , "US-ASCII"); or , "UTF-8" to end the previous line!!
    in = new BufferedReader(isr);
    } catch (IOException e) {
    System.err.print("Couldn't get I/O for ");
    System.out.println("the connection to: " + host);
    System.exit(1);
    } // IOException
    } //getDateSocket()
    } // class DateClient[
    Last edited by Fubarable; 02-20-2011 at 02:56 PM. Reason: Moderator Edit: Code tags added -- but code posted was unformatted

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    The args parameters will hold any Strings passed in via the command line.

    Unrelated comments:

    Java Code:
    public void DateClient() {
    } // empty constructor
    The comment is incorrect since constructors don't return anything, not void, not anything. So this in fact is a method not a constructor.

    Finally, I added code tags to your post to help make the code more readable, but the code wasn't formatted to begin with so it's hard to read regardless. I hope to see your questions (and answers!) here in the future, and if so, you'll help us by posting formatted code with code tags.

    To do this, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [code] above your pasted code and the tag [/code] below your pasted code like so:

    Java Code:
    [code]
      // your code goes here
      // notice how the top and bottom tags are different
    [/code]
    Best of luck, and welcome!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •