SockeChannel connect problem

J

jimgardener

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
 
A

Andrew Thompson

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();
}


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

Arved Sandstrom

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

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top