Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How to associate a local port with a socket channel

Reply
Thread Tools

How to associate a local port with a socket channel

 
 
P. Ajay Prakash
Guest
Posts: n/a
 
      06-15-2004
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
 
Reply With Quote
 
 
 
 
Sudsy
Guest
Posts: n/a
 
      06-15-2004
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 ) {
...


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Dual channel or triple channel? VanguardLH Computer Support 1 06-06-2010 01:36 AM
2924-XL Port Aggregation (port-channel) Steven Carr Cisco 3 10-17-2007 04:52 PM
Six channel or two channel sound on DVD? cydeweys@gmail.com DVD Video 1 10-10-2005 04:51 AM
Socket Send and Receive, Same local port, Different Remote Port jeffburgoon@hotmail.com Perl Misc 1 03-02-2005 08:54 AM
Have two ATA100 Seagate drives in a vp6, one IDE channel comes up as mode 5 in winxp but the other channel is stuck in mode 4 Tim Computer Support 3 02-23-2004 03:35 AM



Advertisments