Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2005

Tip: Looking for answers? Try searching our database.

socket problem

Thread view: 
vindhya - 29 Jul 2005 13:59 GMT
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.
Andrew Thompson - 29 Jul 2005 14:15 GMT
> 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

Signature

Andrew Thompson
physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
Or Is It?

jan V - 29 Jul 2005 15:23 GMT
> 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? ;-)
Thomas Hawtin - 29 Jul 2005 15:30 GMT
> 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
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Andrew Thompson - 29 Jul 2005 17:38 GMT
>> 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.)

Signature

Andrew Thompson
physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
Where No Fan Has Gone Before

jan V - 29 Jul 2005 17:42 GMT
> >> I am a newbie to java and trying to compile this piece of code. But I
> >> am getting an error on line 15...
[quoted text clipped - 6 lines]
> 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..)
frankgerlach22@gmx.de - 29 Jul 2005 20:13 GMT
The sample compiles under JDK1.5. Apparently, javac does the
Interger->int typecast automagically.. (What is the name of this
feature ?)
Andrew Thompson - 29 Jul 2005 20:52 GMT
> 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>

Signature

Andrew Thompson
physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
Beats A Hard Kick In The Face

Thomas Hawtin - 29 Jul 2005 21:02 GMT
>>..Apparently, javac does the
>>Interger->int typecast automagically.. (What is the name of this
[quoted text clipped - 3 lines]
> ..'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
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Andrew Thompson - 29 Jul 2005 21:18 GMT
>>>..Apparently, javac does the
>>>Interger->int typecast automagically.. (What is the name of this
[quoted text clipped - 5 lines]
>
> Autounboxing.
...
Whoever thought that term up, has a twisted sense of humor.
..Should be more of it.   :-)

Signature

Andrew Thompson
physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
LOADING....

Andrew Thompson - 29 Jul 2005 20:56 GMT
> 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.

Signature

Andrew Thompson
physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
Controlling You Through A Chip In Your Butt Since 1999



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.