socket problem

V

vindhya

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.
 
A

Andrew Thompson

I am a newbie to java ..

A good group for those learning Java is..
..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.
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
 
J

jan V

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? ;-)
 
T

Thomas Hawtin

vindhya said:
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
 
A

Andrew Thompson

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.)
 
J

jan V

Andrew Thompson said:
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..)
 
F

frankgerlach22

The sample compiles under JDK1.5. Apparently, javac does the
Interger->int typecast automagically.. (What is the name of this
feature ?)
 
A

Andrew Thompson

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>
 
A

Andrew Thompson

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.
 
T

Thomas Hawtin

Andrew said:
<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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top