Results 1 to 7 of 7
  1. #1
    captainpirate is offline Member
    Join Date
    May 2011
    Posts
    10
    Rep Power
    0

    Default Accessing DataOutputStream

    I have a class named Class1 and it has a boolean method isClientConnection(InetAddress address, int serverPort) and code is
    boolean isClientConnection(InetAddress address, int serverPort) {
    //Here address is localhost and serverPort is 4093
    InetAddress serverName = address;
    int port = serverPort;
    try {
    System.out.println("Connecting to " + serverName
    + " on port " + port);
    client = new Socket(serverName, port);
    System.out.println("Just connected to "+ client.getRemoteSocketAddress());
    outToServer = client.getOutputStream();
    out = new DataOutputStream(outToServer);
    out.writeUTF("OIE Raju " + client.getLocalSocketAddress());
    out.flush();
    return true;
    } catch (IOException e) {
    return false;
    }
    }

    what i want is that i want to access out object from another class named Class2.
    I tried this under Class2 but didnt work:

    Class1 c1=new Class1();
    c1.out.writeBytes("hellowwww");

    How can i solve it ???? Help !!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Accessing DataOutputStream

    didnt work:
    Please explain. You need to show the definition of Class1 where out is defined and given a value.

  3. #3
    captainpirate is offline Member
    Join Date
    May 2011
    Posts
    10
    Rep Power
    0

    Default Re: Accessing DataOutputStream

    Oh sorry for not providing it. I have made them global variable as :
    Socket client;
    OutputStream outToServer;
    DataOutputStream out;

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Accessing DataOutputStream

    Are the variables local to a method or are they class variables? Are they public or private?

    Please post the full text of any error messages.

  5. #5
    captainpirate is offline Member
    Join Date
    May 2011
    Posts
    10
    Rep Power
    0

    Default Re: Accessing DataOutputStream

    under class2.java, i write :

    private void sendButtonActionPerformed(java.awt.event.ActionEve nt evt) {
    Class1 c1=new Class1();
    try {
    mf.out.writeUTF("Hellooooooooo");
    } catch (IOException ex) {
    System.out.println("unable to send message");
    }

    but it didnt work.
    Helllooooooooo is not written to the serverPort (4093) that i defined in Class1.
    Sorry for my weak English.

    Is mf.out.writeUTF("Helloooooooo") wrong ??


    }

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Accessing DataOutputStream

    but it didnt work.
    Helllooooooooo is not written
    What is written?

    You will have to provide a small, complete program that compiles and executes to test with. A SSCCE

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,856
    Rep Power
    19

    Default Re: Accessing DataOutputStream

    That code there instantiates c1 and then doesn't use it.
    How does the call to writeUTF() have anything to do with that Class1 object?

Similar Threads

  1. DataOutputStream
    By blaqkout in forum New To Java
    Replies: 0
    Last Post: 10-02-2011, 07:27 PM
  2. weird character in DataOutputStream file
    By digitalmalayan in forum New To Java
    Replies: 9
    Last Post: 06-25-2011, 04:50 PM
  3. Question about DataOutputStream
    By Pojahn_M in forum New To Java
    Replies: 10
    Last Post: 06-22-2011, 09:52 AM
  4. Dataoutputstream
    By saranyabaskaran in forum New To Java
    Replies: 6
    Last Post: 02-12-2011, 02:26 AM
  5. File I/O with DataOutputStream
    By Tzaphiel in forum New To Java
    Replies: 0
    Last Post: 12-16-2007, 09:39 PM

Posting Permissions

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