Results 1 to 2 of 2
  1. #1
    sixxvirus's Avatar
    sixxvirus is offline Member
    Join Date
    Oct 2011
    Location
    Solana Beach, CA
    Posts
    23
    Blog Entries
    3
    Rep Power
    0

    Default initializing InetAddress object in constructor

    I am having trouble getting this source code to compile with out any errors. The only error is in the main constructor
    UDP_Poke main_obj=new UDP_Poke(host, port);

    It keeps saying "host may not be initialized", this code is out of my text book and I cannot get it to run so I can move on to the next chapter. The InetAddress object looks to be in good form, but the compiler still does not like it. Any suggests??

    import java.net.*;
    import java.io.*;
    public class UDP_Poke{
    private int bufferSize;
    private DatagramSocket socket;
    private DatagramPacket outgoing;

    // UDP_Poke() constructor
    public UDP_Poke(InetAddress host, int port, int bufferSize, int timeout)
    throws SocketException{
    outgoing=new DatagramPacket(new byte[1], 1, host, port);
    this.bufferSize=bufferSize;
    socket=new DatagramSocket(0);
    socket.connect(host, port);
    socket.setSoTimeout(timeout);
    } // end constructor

    // UDP_Poke() constructor
    public UDP_Poke(InetAddress host, int port, int bufferSize)
    throws SocketException{
    this(host, port, bufferSize, 30000);
    } // end constructor

    // UDP_Poke() constructor
    public UDP_Poke(InetAddress host, int port) throws SocketException{
    this(host, 8192, 30000);
    } // end constructor

    // poke() method
    public byte[] poke() throws IOException{
    byte[] response=null;
    try{
    socket.send(outgoing);
    DatagramPacket incoming=new DatagramPacket(new byte[bufferSize],
    bufferSize);
    socket.receive(incoming);
    int numBytes=incoming.getLength();
    response=new byte[numBytes];
    System.arraycopy(incoming.getData(), 0, response, 0, numBytes);
    }catch(IOException e){
    // response will be null
    } // end exception
    return response;
    } // end method

    public static void main(String[] args){
    InetAddress host;
    int port=0;
    try{
    host=InetAddress.getByName(args[0]);
    port=Integer.parseInt(args[1]);
    if(port<1 || port>65535){
    throw new Exception();
    } // end condition
    }catch(Exception e){
    System.err.println("Usage: java UDP_Poke host port");
    System.exit(0);
    } // end exception
    try{
    UDP_Poke main_obj=new UDP_Poke(host, port);
    byte[] response=main_obj.poke();
    if(response==null){
    System.out.println("No response within the allotted time");
    System.exit(0);
    } // end condition
    String result="";
    try{
    result=new String(response, "ASCII");
    }catch(UnsupportedEncodingException e){
    // try a different encoding
    result=new String(response, "8859_1");
    } // end exception
    System.out.println(result);
    }catch(Exception e){
    System.err.println(e);
    e.printStackTrace();
    } // end exception
    } // end main
    } // end class

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: initializing InetAddress object in constructor

    You've been advised about code tags.
    Java Port Scanner

    Is it really so difficult to understand how to use them?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Error Passing Object through a constructor
    By mpankhurst in forum New To Java
    Replies: 9
    Last Post: 12-15-2011, 10:12 AM
  2. Getting the calling object in a constructor?
    By Koala in forum New To Java
    Replies: 8
    Last Post: 10-31-2011, 03:27 AM
  3. Create an object without calling the constructor?
    By sublixt in forum New To Java
    Replies: 7
    Last Post: 10-28-2011, 12:49 PM
  4. Replies: 2
    Last Post: 08-12-2011, 08:48 PM
  5. Initializing property object from a file
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-29-2007, 04:55 PM

Tags for this Thread

Posting Permissions

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