socket problem

Discussion in 'Java' started by vindhya, Jul 29, 2005.

  1. vindhya

    vindhya Guest

    I am a newbie to java and trying to compile this piece of code. But I
    am getting an error on line 15...

    import java.util.*;
    import java.io.*;
    import java.net.*;
    import java.net.InetAddress;

    public class Client {
    public static void main (String[] args) throws IOException {
    Socket mysocket = null;
    PrintWriter out = null;
    BufferedReader in = null;

    try {
    InetAddress ip = InetAddress.getLocalHost() ;
    Integer port = 9990;
    mysocket = new Socket(ip,port); //LINE 15
    out = new PrintWriter(mysocket.getOutputStream(),true);
    in = new BufferedReader(new
    InputStreamReader(mysocket.getInputStream()));

    }
    catch (UnknownHostException e) {
    System.out.println ("Caught exception");
    }
    BufferedReader stdIn = new BufferedReader(
    new InputStreamReader(System.in));

    String userInput;

    while ((userInput = stdIn.readLine()) != null) {
    out.println(userInput);
    System.out.println("echo: " + in.readLine());
    }

    out.close();
    in.close();
    stdIn.close();
    mysocket.close();
    }
    }

    Any suggestions.
    Thanks in advance.
     
    vindhya, Jul 29, 2005
    #1
    1. Advertising

  2. On 29 Jul 2005 05:59:28 -0700, vindhya wrote:

    > I am a newbie to java ..


    A good group for those learning Java is..
    <http://www.physci.org/codes/javafaq.jsp#cljh>

    > ..and trying to compile this piece of code. But I
    > am getting an error on line 15...


    Nice example code - but beware of line wrap, and please
    change 'tab' characters for ' ' before posting code.
    Check here for some more tips on example code.
    <http://www.physci.org/codes/sscce.jsp>
    ...
    > Any suggestions.


    1) Be specific about the error. Is it a NotEnoughPuppiesException?
    Or perhaps something else?

    2) Ask a specific question, and do not forget to add the tell-tale '?'.

    3) Research the difference between Objects and primitives.
    (you need an 'int', not an 'Integer').

    4) Note that your second error is related to the first one, the
    one you did *not* mention. Please mention all errors when asking
    questions, though more generally..

    5) Solve errors from the 'top' down. An error in line 3 might
    cause 20 other errors further down.

    HTH

    --
    Andrew Thompson
    physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
    Or Is It?
     
    Andrew Thompson, Jul 29, 2005
    #2
    1. Advertising

  3. vindhya

    jan V Guest

    > I am a newbie to java and trying to compile this piece of code. But I
    > am getting an error on line 15...


    Maybe a good idea to tell us which error, specifically? Or is this a trick
    question? ;-)
     
    jan V, Jul 29, 2005
    #3
  4. vindhya wrote:
    > I am a newbie to java and trying to compile this piece of code. But I
    > am getting an error on line 15...


    Compiles fine for me.

    (Sun Linux JDK 5.0u4 and 6.0ea b44)

    Tom Hawtin
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
     
    Thomas Hawtin, Jul 29, 2005
    #4
  5. On Fri, 29 Jul 2005 15:30:12 +0100, Thomas Hawtin wrote:

    > vindhya wrote:
    >> I am a newbie to java and trying to compile this piece of code. But I
    >> am getting an error on line 15...

    >
    > Compiles fine for me.
    >
    > (Sun Linux JDK 5.0u4 ..


    Really? Where did you find the Socket constructor that
    accepts an Integer? I don't see it in the 1.5 javadocs.
    (..and my rt.jar don't have it.)

    --
    Andrew Thompson
    physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
    Where No Fan Has Gone Before
     
    Andrew Thompson, Jul 29, 2005
    #5
  6. vindhya

    jan V Guest

    "Andrew Thompson" <> wrote in message
    news:101q8474q7h4c.lfqhvw0xxjen$...
    > On Fri, 29 Jul 2005 15:30:12 +0100, Thomas Hawtin wrote:
    >
    > > vindhya wrote:
    > >> I am a newbie to java and trying to compile this piece of code. But I
    > >> am getting an error on line 15...

    > >
    > > Compiles fine for me.
    > >
    > > (Sun Linux JDK 5.0u4 ..

    >
    > Really? Where did you find the Socket constructor that
    > accepts an Integer? I don't see it in the 1.5 javadocs.
    > (..and my rt.jar don't have it.)


    Maybe something to do with autoboxing/unboxing? (Dunno, not touched the new
    language features so far..)
     
    jan V, Jul 29, 2005
    #6
  7. vindhya

    Guest

    The sample compiles under JDK1.5. Apparently, javac does the
    Interger->int typecast automagically.. (What is the name of this
    feature ?)
     
    , Jul 29, 2005
    #7
  8. On 29 Jul 2005 12:13:57 -0700, wrote:

    > The sample compiles under JDK1.5.


    Aha! I did not realise it earlier, but I was compiling using 1.4.
    ( So, yes Thomas, now I understand where you observed it. )

    >..Apparently, javac does the
    > Interger->int typecast automagically.. (What is the name of this
    > feature ?)


    <grumbles>
    ...'inline obfuscation' perhaps?
    </grumbles>

    --
    Andrew Thompson
    physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
    Beats A Hard Kick In The Face
     
    Andrew Thompson, Jul 29, 2005
    #8
  9. On 29 Jul 2005 05:59:28 -0700, vindhya wrote:

    > I am a newbie to java and trying to compile this piece of code. But I
    > am getting an error on line 15...


    > Integer port = 9990;
    > mysocket = new Socket(ip,port); //LINE 15


    Exception in thread "main" java.net.ConnectException: Connection refused:
    connect
    ...
    at test.Client.main(Client.java:15)

    That is very different from a NotEnoughPuppiesException!
    I think this might be solved without so much as a
    single small canine coming to harm.

    --
    Andrew Thompson
    physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
    Controlling You Through A Chip In Your Butt Since 1999
     
    Andrew Thompson, Jul 29, 2005
    #9
  10. Andrew Thompson wrote:
    > On 29 Jul 2005 12:13:57 -0700, wrote:
    >
    >>..Apparently, javac does the
    >>Interger->int typecast automagically.. (What is the name of this
    >>feature ?)

    >
    >
    > <grumbles>
    > ..'inline obfuscation' perhaps?
    > </grumbles>


    Autounboxing.

    Autoboxing is actually a quite useful feature, particularly with the
    var...args feature. Autounboxing is less helpful, can give you
    non-obvious NPEs, Integer==Integer doesn't get unboxed but
    Integer<=Integer does and saves even less fluff.


    Goes to show that if you have a problem compiling something, best to
    give the relevant information about the compiler and library versions.

    Tom Hawtin
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
     
    Thomas Hawtin, Jul 29, 2005
    #10
  11. On Fri, 29 Jul 2005 21:02:34 +0100, Thomas Hawtin wrote:
    > Andrew Thompson wrote:
    >> On 29 Jul 2005 12:13:57 -0700, wrote:
    >>
    >>>..Apparently, javac does the
    >>>Interger->int typecast automagically.. (What is the name of this
    >>>feature ?)

    >>
    >> <grumbles>
    >> ..'inline obfuscation' perhaps?
    >> </grumbles>

    >
    > Autounboxing.

    ....
    Whoever thought that term up, has a twisted sense of humor.
    ...Should be more of it. :)

    --
    Andrew Thompson
    physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
    LOADING....
     
    Andrew Thompson, Jul 29, 2005
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Laszlo Nagy
    Replies:
    1
    Views:
    4,855
    Mark Wooding
    Jan 27, 2009
  2. Jean-Paul Calderone
    Replies:
    0
    Views:
    971
    Jean-Paul Calderone
    Jan 27, 2009
  3. Laszlo Nagy
    Replies:
    0
    Views:
    552
    Laszlo Nagy
    Feb 1, 2009
  4. Steve Holden
    Replies:
    0
    Views:
    666
    Steve Holden
    Feb 1, 2009
  5. Steve Holden
    Replies:
    1
    Views:
    719
Loading...

Share This Page