Get Service Name for Port
Q:
Is there any way to retrieve the TCP/IP service (local port name)
associated to a local port number ?
Just like int the command WRKSRVTBLE. Is there an API for this ?
A:
- Here's an RPG/IV example of the getservbyport() function:
H bnddir( 'QC2LE' )
**
D getservbyport pr * extproc( 'getservbyport' )
D port 10i 0 value
D protocol * value options( *string )
**
D servent ds based( p_servent )
D s_name *
D s_aliases *
D port 10i 0
D s_proto *
**
D idx s 10i 0
D name s 128a
D protocol s 128a
D p_aliases s * dim( 12 ) based( s_aliases )
D aliases s 128a dim( %elem( p_aliases ))
**
/free
p_servent = getservbyport( 443: *null );
if p_servent <> *null;
name = %str( s_name );
protocol = %str( s_proto );
for idx = 1 to %elem( p_aliases );
if p_aliases(idx) = *null;
leave;
endif;
aliases(idx) = %str( p_aliases(idx));
endfor;
endif;
p_servent = getservbyport( 443: 'udp' );
if p_servent <> *null;
name = %str( s_name );
protocol = %str( s_proto );
for idx = 1 to %elem( p_aliases );
if p_aliases(idx) = *null;
leave;
endif;
aliases(idx) = %str( p_aliases(idx));
endfor;
endif;
*inlr = *on;
return;
/end-free
The first call returns the first server registered to the specified port.
The second call returns the service name for the specified port's 'udp'
protocol entry.
If you need to call the getservbyport() function repeatedly, the
setservent() and endservent() functions allow you to control when the
service database file is opened and closed, leaving it open for the
getservbyport() calls. The getservent() function enables you to retrieve a
sequential list of the servers in the service database.
Thanks to Carsten Flensburg
阅读(715) | 评论(0) | 转发(0) |