SockeChannel connect problem

Discussion in 'Java' started by jimgardener, Jun 28, 2008.

  1. jimgardener

    jimgardener Guest

    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
     
    jimgardener, Jun 28, 2008
    #1
    1. Advertising

  2. On Jun 28, 6:17 pm, jimgardener <> 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/
     
    Andrew Thompson, Jun 28, 2008
    #2
    1. Advertising

  3. "jimgardener" <> wrote in message
    news:...
    > 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
     
    Arved Sandstrom, Jun 28, 2008
    #3
    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. Otis Mukinfus
    Replies:
    3
    Views:
    961
    Juan T. Llibre
    Jul 8, 2005
  2. Replies:
    3
    Views:
    40,681
    Roedy Green
    Nov 16, 2005
  3. Replies:
    2
    Views:
    331
  4. Mohsen Pahlevanzadeh

    Qt connect and first connect or unicode

    Mohsen Pahlevanzadeh, Sep 17, 2013, in forum: Python
    Replies:
    3
    Views:
    178
    Mohsen Pahlevanzadeh
    Sep 18, 2013
  5. Mohsen Pahlevanzadeh

    Re: Qt connect and first connect or unicode

    Mohsen Pahlevanzadeh, Sep 17, 2013, in forum: Python
    Replies:
    0
    Views:
    140
    Mohsen Pahlevanzadeh
    Sep 17, 2013
Loading...

Share This Page