Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   SockeChannel connect problem (http://www.velocityreviews.com/forums/t622712-sockechannel-connect-problem.html)

jimgardener 06-28-2008 08:17 AM

SockeChannel connect problem
 
hi
i am trying out connections using SocketChannel with different ports
on my machine.
I wrote the application as below

public class SChannel{

public static void main(String args[]){
try{
int port=Integer.parseInt(args[0]);
System.out.println("connecting to:"+port);
SocketChannel sc=SocketChannel.open();
InetSocketAddress isa=new
InetSocketAddress(InetAddress.getLocalHost(),port) ;
sc.connect(isa);
System.out.println("connected:"+sc.isConnected());


}
catch(Exception e){
e.printStackTrace();
}
}


i tried diff ports as argument..like 8080,8086,4444,9090..but all
gives 'net.ConnectException :connection refused:connect'
Is there something wrong with the way i am trying to connect
can somebody help
jim



Andrew Thompson 06-28-2008 09:47 AM

Re: SockeChannel connect problem
 
On Jun 28, 6:17*pm, jimgardener <jimgarde...@gmail.com> wrote:
...
> I wrote the application as below
>
> public class SChannel{


Please consider posting SSCCEs in future.
It only took a couple of extra lines of code
to make that compilable and runnable.

E.G. the code I used..

<sscce>
import java.net.*;
import java.nio.channels.SocketChannel;

public class SChannel{


public static void main(String args[]){
try{
int port=Integer.parseInt(args[0]);
System.out.println("connecting to:"+port);
SocketChannel sc=SocketChannel.open();
InetSocketAddress isa=new
InetSocketAddress(InetAddress.getLocalHost(),port) ;
sc.connect(isa);
System.out.println("connected:"+sc.isConnected());


}
catch(Exception e){
e.printStackTrace();
}


}
}
</sscce>

> i tried diff ports as argument..like 8080,...


The output I got..

<output>
C:\SChannel>java SChannel 8080
connecting to:8080
java.net.ConnectException: Connection refused: connect
at sun.nio.ch.Net.connect(Native Method)
at sun.nio.ch.SocketChannelImpl.connect(Unknown Source)
at SChannel.main(SChannel.java:14)

C:\SChannel>pause
Press any key to continue . . .
</output>

Then I started tomcat, and got..

<output>
C:\SChannel>java SChannel 8080
connecting to:8080
connected:true

C:\SChannel>pause
Press any key to continue . . .
</output>

> Is there something wrong with the way i am trying to connect


Is that a question? Questions are best ended
with a question mark (?).

In any case, I suspect the problem is what
the code is trying to connect *to*.

> can somebody help


Is /that/ a question?

--
Andrew Thompson
http://pscode.org/

Arved Sandstrom 06-28-2008 10:45 AM

Re: SockeChannel connect problem
 
"jimgardener" <jimgardener@gmail.com> wrote in message
news:757e2cd8-d3f1-44bb-8a2b-caa87ec32e48@p25g2000pri.googlegroups.com...
> hi
> i am trying out connections using SocketChannel with different ports
> on my machine.
> I wrote the application as below
>

[ SNIP ]
> i tried diff ports as argument..like 8080,8086,4444,9090..but all
> gives 'net.ConnectException :connection refused:connect'
> Is there something wrong with the way i am trying to connect
> can somebody help
> jim


Lke Andrew said, do you have any reason to believe that anything is
listening on those ports?

A "netstat -an" on Windows will show you open ports.

AHS




All times are GMT. The time now is 08:05 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.