How to associate a local port with a socket channel

Discussion in 'Java' started by P. Ajay Prakash, Jun 15, 2004.

  1. Hi,
    I am currently facing a problem programming with the
    SocketChannel class of java.nio.channels package. Actually, I am
    interested in non-blocking I/O for which i have to register socket
    channels with a selector object. Now, in my application, i have to
    associate each socket channel with a port number.
    (this has to be done at channel creation time). How do i embed LOCAL
    port information with my socket channel?

    Could anyone please help me out with this one?

    regards,
    Ajay
    P. Ajay Prakash, Jun 15, 2004
    #1
    1. Advertising

  2. P. Ajay Prakash

    Sudsy Guest

    P. Ajay Prakash wrote:
    > Hi,
    > I am currently facing a problem programming with the
    > SocketChannel class of java.nio.channels package. Actually, I am
    > interested in non-blocking I/O for which i have to register socket
    > channels with a selector object. Now, in my application, i have to
    > associate each socket channel with a port number.
    > (this has to be done at channel creation time). How do i embed LOCAL
    > port information with my socket channel?


    It's important to understand the socket semantics. If I create an
    outgoing TCP port then I don't specify the port number. Only with
    an incoming TCP port can I "bind" to a specific port. So look at
    the ServerSocketChannel.
    Here's some code I dug out of one of my projects:

    ServerSocketChannel serverSockChannel = null;
    ServerSocket serverSock = null;
    SocketChannel acceptSock = null;
    Selector selector = null;
    ArrayList sockets = new ArrayList();
    int portNum = 11111;
    try {
    serverSockChannel = ServerSocketChannel.open();
    serverSock = serverSockChannel.socket();
    serverSock.bind( new InetSocketAddress( "localhost",
    portNum ), 5 );
    selector = selector.open();
    serverSockChannel.configureBlocking( false );
    registerSelections( selector, serverSockChannel,
    sockets );
    }
    catch( IOException e ) {
    ...
    Sudsy, Jun 15, 2004
    #2
    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. =?Utf-8?B?WVNVVA==?=

    Access local port or Running local exe file

    =?Utf-8?B?WVNVVA==?=, Jan 14, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    499
    =?Utf-8?B?WVNVVA==?=
    Jan 14, 2006
  2. Kiran Kumar

    reading from socket channel

    Kiran Kumar, May 22, 2004, in forum: Java
    Replies:
    1
    Views:
    1,517
    Steve Horsley
    May 22, 2004
  3. farseer
    Replies:
    2
    Views:
    460
    farseer
    Apr 11, 2005
  4. Roedy Green

    faking HTTP with a socket channel

    Roedy Green, Aug 11, 2008, in forum: Java
    Replies:
    14
    Views:
    1,020
    Roedy Green
    Aug 14, 2008
  5. Replies:
    1
    Views:
    555
    Toni Erdmann
    Mar 2, 2005
Loading...

Share This Page