The UTL_TCP (Transfer Control Protocol) helps the PL/SQL programs to
communicate with the external servers using the TCP/IP as it is the
common communication protocol of the internet. This package provides
the client side access functionality of the TCP/IP protocol through
PL/SQL by opening up a socket to read/ write data from/ to a server
using any protocol like HTTP, SMTP, or FTP.
The different objects available in this package are described below.
CONNECTION Record Type
This is a PL/SQL record type for establishing a TCP/IP connection
through PL/SQL subprograms. The fields in the CONNECTION record type
are used to provide salient information about the TCP/IP connection
being established.
The prototype of the CONNECTION record type is shown below,
TYPE connection IS RECORD (
remote_host
VARCHAR2(255),
remote_port
PLS_INTEGER,
local_host
VARCHAR2(255),
local_port
PLS_INTEGER,
charset
VARCHAR2(30),
newline
VARCHAR2(2),
tx_timeout
PLS_INTEGER,
private_sd
PLS_INTEGER);
·
REMOTE_HOST
parameter contains the name of the remote host during a connection.
Null if no connection is established.
·
REMOTE_PORT
parameter contains the port of the remote host during a connection.
Null if no connection is established.
·
LOCAL_HOST
parameter contains the name of the local host used for the connection.
Null if no connection is established. This parameter is currently
unsupported.
·
LOCAL_PORT
parameter contains the port of the local host used for the connection.
Null if no connection is established. This parameter is currently
unsupported.
·
CHARSET
parameter contains the on-the-wire character set for converting the
character set of the database data to and from the connected internet
site’s character set.
·
NEWLINE
parameter contains the sequence of the newline character.
·
TX_TIMEOUT
parameter contains the timeout time in seconds before giving up on a
connection while trying.
·
PRIVATE_SD
parameter is used for the package’s internal use and must not be
manipulated.
OPEN_CONNECTION Function
This function opens up a TCP/IP protocol based connection to the
specified web service. The connections opened up by this function
remains open and can be transferred between the databases using the
shared server configuration. These connections must be closed
explicitly and failing in closing the unwanted connections may result
in holding up of the system resources.
The prototype of the OPEN_CONNECTION function is shown below,
UTL_TCP.OPEN_CONNECTION (
remote_host
IN VARCHAR2,
remote_port
IN PLS_INTEGER,
local_host
IN VARCHAR2 DEFAULT NULL,
local_port
IN PLS_INTEGER DEFAULT NULL,
in_buffer_size
IN PLS_INTEGER DEFAULT NULL,
out_buffer_size
IN PLS_INTEGER DEFAULT NULL,
charset
IN VARCHAR2 DEFAULT NULL,
newline
IN VARCHAR2 DEFAULT CRLF,
tx_timeout
IN PLS_INTEGER DEFAULT NULL,
wallet_path
IN VARCHAR2 DEFAULT NULL,
wallet_password
IN VARCHAR2 DEFAULT NULL,
RETURN connection;
·
IN_BUFFER_SIZE
parameter accepts the size of the input buffer for speeding up the
execution performance of the receiving data from the server.
·
OUT_BUFFER_SIZE
parameter accepts the size of the output buffer for speeding up the
execution performance of the sending data to the server.
·
WALLET_PATH
parameter accepts the wallet path for the certificates stored which
are required while requesting data from a secure connection. The
format is file: <Directory_path>.
·
WALLET_PASSWORD
parameter accepts the password to open the wallet. When AUTO_LOGIN
parameter is enabled, this parameter may set to
null.
SECURE_CONNECTION Procedure
This procedure secures the TCP/IP connection made using SSL/TLS
configuration. This process needs an Oracle wallet with a valid
certificate to be specified during the connection call using the
OPEN_CONNECTION function.
The prototype of the SECURE_CONNECTION procedure is shown below,
UTL_TCP.SECURE_CONNECTION (c IN OUT NOCOPY connection);
·
C
parameter accepts and returns the connection details of the server
from which the data is to be received.
UTL_TCP AVAILABLE Function
This function returns the number of bytes available for reading from
the connected server without blocking after making a connection. On
some platforms, this function may return 1 if there are data to
receive from the server and 0 if not. This function is used to do a
precautionary check whether the data is available or not before
reading before allocating resources for the data read.
The prototype of the AVAILABLE function is shown below,
UTL_TCP.AVAILABLE (
c
IN OUT NOCOPY connection,
timeout IN
PLS_INTEGER DEFAULT 0)
RETURN PLS_INTEGER;
·
TIMEOUT
parameter accepts the timeout time in seconds before giving up on a
connection while trying.
FLUSH Procedure
This procedure flushes all the data into the server from the output
buffer after establishing a connection.
The prototype of the FLUSH procedure is shown below,
UTL_TCP.FLUSH(c IN OUT NOCOPY connection);
GET_LINE, and GET_LINE_NCHAR Functions
The functions GET_LINE and GET_LINE_NCHAR, return line of data read in
the database and national character set respectively.
The prototype of the GET_LINE and the GET_LINE_NCHAR functions are
shown below,
UTL_TCP.GET_LINE (
c
IN OUT NOCOPY connection,
remove_crlf IN
BOOLEAN DEFAULT FALSE,
peek
IN
BOOLEAN DEFAULT FALSE)
RETURN VARCHAR2;
UTL_TCP.GET_LINE_NCHAR (
c
IN OUT NOCOPY connection,
remove_crlf IN
BOOLEAN DEFAULT FALSE,
peek
IN
BOOLEAN DEFAULT FALSE)
RETURN NVARCHAR2;
·
REMOVE_CRLF
parameter removes the trailing CRLF characters from the received
message.
·
PEEK
parameter is to be set to a Boolean
true when we want to look
ahead at the data without removing it from the
queue so that it's available for reading in the further call. This
parameter needs an input buffer to be created before the connection is
opened, which then holds the data for peeking without interfering this
data from the next call.
GET_RAW, GET_TEXT, and GET_TEXT_NCHAR Functions
The GET_RAW, GET_TEXT, and GET_TEXT_NCHAR functions return the RAW
data, TEXT data, TEXT data in national character read from the target
server.
The prototypes of these functions are shown below,
UTL_TCP.GET_RAW (
c
IN OUT NOCOPY connection,
len
IN
PLS_INTEGER DEFAULT 1,
peek
IN
BOOLEAN
DEFAULT FALSE)
RETURN RAW;
UTL_TCP.GET_TEXT (
c
IN OUT NOCOPY connection,
len
IN
PLS_INTEGER DEFAULT 1,
peek IN
BOOLEAN
DEFAULT FALSE)
RETURN VARCHAR2;
UTL_TCP.GET_TEXT_NCHAR (
c
IN OUT NOCOPY connection,
len
IN
PLS_INTEGER DEFAULT 1,
peek IN
BOOLEAN
DEFAULT FALSE)
RETURN NVARCHAR2;
·
LEN
parameter accepts the number of bytes of data to be received.
READ_LINE
This function receives a text line from the target server upon an open
connection. A line feed, a carriage-return or a
carriage return followed by a
linefeed determines the line
separator. This function returns the number of characters of data
received through its RETURN statement.
The prototype of this function is shown below,
UTL_TCP.READ_LINE (
c
IN OUT NOCOPY connection,
data
IN OUT NOCOPY VARCHAR2 CHARACTER SET ANY_CS,
peek
IN
BOOLEAN DEFAULT FALSE)
RETURN PLS_INTEGER;
·
DATA
parameter accepts and returns the data to be received.
READ_RAW, and READ_TEXT Functions
The READ_RAW and READ_TEXT
functions receive binary, and
text data respectively from the target server with an open connection.
These functions do not return data
unless the specified number of bytes is read or the EOI (End of Input)
has been reached. The size of the VARCHAR2 buffer should be equal to
the number of characters to be read, multiplied by the maximum number
of bytes of a character of the database character set. These functions
return the number of characters of data received through its RETURN
statement.
The prototypes of these functions are shown below,
UTL_TCP.READ_RAW (
c
IN OUT NOCOPY connection,
data
IN OUT NOCOPY RAW,
len
IN
PLS_INTEGER DEFAULT 1,
peek
IN
BOOLEAN
DEFAULT FALSE)
RETURN PLS_INTEGER;
UTL_TCP.READ_TEXT (
c
IN OUT NOCOPY connection,
data IN OUT
NOCOPY VARCHAR2 CHARACTER SET ANY_CS,
len
IN
PLS_INTEGER DEFAULT 1,
peek IN
BOOLEAN
DEFAULT FALSE)
RETURN PLS_INTEGER;
WRITE_LINE, WRITE_RAW, and WRITE_TEXT Functions
The WRITE_LINE, WRITE_RAW, and WRITE_TEXT functions
write a text line, a binary
message, and a text message respectively to the target server in an
open connection. These functions return the number of characters of
data received through its RETURN statement after writing the specified
number of bytes to the server (in the
case of WRITE_RAW and WRITE_TEXT).
The WRITE_LINE function adds a NEWLINE character message before it is
transmitted. The WRITE_TEXT function converts its data to the
on-the-wire character set before the transmission.
The prototypes of these functions are shown below,
UTL_TCP.WRITE_LINE (
c
IN OUT NOCOPY connection,
data IN
VARCHAR2 DEFAULT NULL CHARACTER SET ANY_CS)
RETURN PLS_INTEGER;
UTL_TCP.WRITE_RAW (
c
IN OUT NOCOPY connection,
data IN
RAW,
len
IN
PLS_INTEGER DEFAULT NULL)
RETURN PLS_INTEGER;
UTL_TCP.WRITE_TEXT (
c
IN OUT NOCOPY connection,
data IN
VARCHAR2 CHARACTER SET ANY_CS,
len
IN
PLS_INTEGER DEFAULT NULL)
RETURN num_chars
PLS_INTEGER;
CLOSE_CONNECTION and CLOSE_ALL_CONNECTIONS Procedures
The CLOSE_CONNECTION and CLOSE_ALL_CONNECTIONS procedures close a
single and all the connections respectively. A TCP/IP connection
remains open until explicitly said. Failing to close the unwanted
connections may result in holding up of the system resources
unconditionally.
The prototypes of these procedures are shown below,
UTL_TCP.CLOSE_CONNECTION (c IN OUT NOCOPY connection);
UTL_TCP.CLOSE_ALL_CONNECTIONS;
In the below example, a connection to GOOGLE server has been
established using the OPEN_CONNECTION function with the default port
number: 80 and character set as AL32UTF8. The HTTP request has been
sent to the server using the WRITE_LINE function and the response is
retrieved using the GET_LINE function within a loop. Finally, the
connection has been terminated using the CLOSE_CONNECTION procedure
after receiving the complete response from the server.
DECLARE
l_c_conn
utl_tcp.connection;
l_pi_ret pls_integer;
BEGIN
l_c_conn :=
utl_tcp.open_connection(remote_host => 'www.google.com',
remote_port => 80, charset => 'AL32UTF8');
l_pi_ret :=
utl_tcp.write_line(l_c_conn, 'GET / HTTP/1.0');
l_pi_ret :=
utl_tcp.write_line(l_c_conn);
BEGIN
LOOP
dbms_output.put_line(utl_tcp.get_line(l_c_conn, TRUE));
END LOOP;
EXCEPTION
WHEN
utl_tcp.end_of_input THEN
NULL;
END;
utl_tcp.close_connection(l_c_conn);
END;
/
Results:
HTTP/1.0 302 Found
Cache-Control: private
Content-Type: text/html; charset=UTF-8
Location: http://www.google.co.in/?gfe_rd=cr&ei=lRrbV5zTMcWL8QfltbigBA
Content-Length: 261
Date: Thu, 15 Sep 2016 22:03:01 GMT
<HTML File>
---------------------------------------------------------------------
Here are the utl_tcp stored procedures:
CLOSE_ALL_CONNECTIONS Procedure: |
Closes all open TCP/IP connections |
CLOSE_CONNECTION Procedure |
Closes an specific open TCP/IP connection |
FLUSH Procedure: |
Transmits all data in the output buffer, if a buffer is used, to the server immediately |
Here are the utl_tcp functions: (a function must return a value)
AVAILABLE Function: |
Determines the number of bytes available for reading from a TCP/IP connection |
GET_LINE Function |
Returns the line of data read |
GET_LINE_NCHAR Function |
Returns the line of data read in NCHAR form |
GET_RAW Function |
Return the data read instead of the amount of data read |
GET_TEXT Function |
Returns the text data read |
GET_TEXT_NCHAR Function |
Returns the text data read in NCHAR form |
OPEN_CONNECTION Function |
Opens a TCP/IP connection to a specified service |
READ_LINE Function |
Receives a text line from a service on an open connection |
READ_RAW Function |
Receives binary data from a service on an open connection |
READ_TEXT Function |
Receives text data from a service on an open connection |
WRITE_LINE Function |
Transmits a text line to a service on an open connection |
WRITE_RAW Function |
Transmits a binary message to a service on an open connection |
WRITE_TEXT Function |
Transmits a text message to a service on an open connection |
The
utl_tcp package provides PL/SQL programs the
capability to communicate via TCP/IP (Transmission Control
Protocol/Internet Protocol).
The
utl_tcp
package requires that the Oracle XMLDB component was chosen during initial database creation or
subsequent modification utilizing DBCA. Otherwise, the following
series of error messages will come up:
SQL> @send_email
DECLARE
*
ERROR at line 1:
ORA-24248: XML DB extensible security not
installed
ORA-06512: at "SYS.UTL_TCP", line 17
ORA-06512: at "SYS.UTL_TCP", line 246
ORA-06512: at "SYS.UTL_SMTP", line 115
ORA-06512: at "SYS.UTL_SMTP", line 138
ORA-06512: at line 7
Just like
utl_smtp, this is one occasion where an example is worth
more than the reference material. The
email_myself_tcp_demo.sql PL/SQL code is a snippet of
PL/SQL to email a message:
email_myself_tcp_demo.sql script
DECLARE
sender
VARCHAR2(30) := 'bert.scalzo@yahoo.com';
recipient
VARCHAR2(30) := 'bert.scalzo@yahoo.com';
message
VARCHAR2(30) := 'This is a test message';
mailhost
VARCHAR2(30) := '127.0.0.1';
mail_conn
utl_tcp.connection;
PROCEDURE smtp_command (command IN
VARCHAR2, ok IN VARCHAR2 DEFAULT '250')
IS
response varchar2(3);
len
pls_integer;
BEGIN
len := utl_tcp.write_line(mail_conn,
command);
response := substr(utl_tcp.get_line(mail_conn),
1, 3);
END;
BEGIN
mail_conn := utl_tcp.open_connection
(remote_host => mailhost, remote_port => 25, charset =>
'US7ASCII');
smtp_command ('HELO ' || mailhost);
smtp_command ('MAIL FROM: ' ||
sender);
smtp_command ('RCPT TO: ' ||
recipient);
smtp_command ('DATA', '354');
smtp_command (message);
smtp_command ('QUIT', '221');
utl_tcp.close_connection (mail_conn);
END;
/
Next, examine the procedures and functions provided by the
utl_tcp package. Key to the process is creating a TCP
connection and then using that on subsequent
utl_tcp calls. One also needs to work with the following
data type and constants:
TYPE connection IS RECORD (
remote_host
VARCHAR2(255),
-- Remote host name
remote_port
PLS_INTEGER,
-- Remote port number
local_host
VARCHAR2(255),
-- Local host name
local_port
PLS_INTEGER,
-- Local port number
charset
VARCHAR2(30),
-- Character set for on-the-wire comm.
newline
VARCHAR2(2),
-- Newline character sequence
tx_timeout
PLS_INTEGER,
-- Transfer time-out value (in seconds)
private_sd
PLS_INTEGER
-- For internal use only
);
buffer_too_small
EXCEPTION; --
Buffer is too small for I/O
end_of_input
EXCEPTION; --
End of input from connection
network_error
EXCEPTION; --
Network error
bad_argument
EXCEPTION; --
Bad argument passed in API call
partial_multibyte_char
EXCEPTION; --
A partial multi-byte char found
transfer_timeout
EXCEPTION; --
Transfer time-out occurred
network_access_denied
EXCEPTION; --
Network access denied
buffer_too_small_errcode
CONSTANT PLS_INTEGER:= -29258;
end_of_input_errcode
CONSTANT PLS_INTEGER:= -29259;
network_error_errcode
CONSTANT PLS_INTEGER:= -29260;
bad_argument_errcode
CONSTANT PLS_INTEGER:= -29261;
partial_multibyte_char_errcode CONSTANT
PLS_INTEGER:= -29275;
transfer_timeout_errcode
CONSTANT PLS_INTEGER:= -29276;
network_access_denied_errcode
CONSTANT PLS_INTEGER:= -24247;
Close_data
is a function that determines the number of bytes available for
consumption from a TCP/IP connection and returns a
pls_integer.
Argument
|
Type
|
In / Out
|
Default Value
|
C
|
UTL_TCP.CONNECTION
|
IN | OUT
|
|
TIMEOUT
|
PLS_INTEGER
|
IN
|
0
|
Table 6.157:
Close_data Parameters
Close_all_connections
is a
utl_tcp
procedure that simply closes all the open TCP/IP connections. It
takes no parameters.
Close_connection
is a
utl_tcp
procedure that closes the specified open TCP/IP connection.
Argument
|
Type
|
In / Out
|
Default Value
|
C
|
UTL_TCP.CONNECTION
|
IN | OUT
|
|
Table 6.158:
Close_connection Parameter
Flush
is a
utl_tcp
procedure that immediately transmits any and all data in the
output buffer.
Argument
|
Type
|
In / Out
|
Default Value
|
C
|
UTL_TCP.CONNECTION
|
IN | OUT
|
|
Table 6.159:
Flush Parameters
Get_line
is a
utl_tcp
function that reads a line of data and returns a VARCHAR2. The
peek parameter allows one to look ahead in the buffer
without actually consuming the message.
Argument
|
Type
|
In / Out
|
Default Value
|
C
|
UTL_TCP.CONNECTION
|
IN | OUT
|
|
REMOVE_CRLF
|
BOOLEAN
|
IN
|
FALSE
|
PEEK
|
BOOLEAN
|
IN
|
FALSE
|
Table 6.160:
Get_line Parameters
Get_line_nchar
is a
utl_tcp
function that reads a line of data, and returns a NVARCHAR2. The
peek parameter allows one to look ahead in the buffer
without actually consuming the message.
Argument
|
Type
|
In / Out
|
Default Value
|
C
|
UTL_TCP.CONNECTION
|
IN | OUT
|
|
REMOVE_CRLF
|
BOOLEAN
|
IN
|
FALSE
|
PEEK
|
BOOLEAN
|
IN
|
FALSE
|
Table 6.161:
Get_line_nchar Parameters
Get_raw
is a
utl_tcp function that reads data and returns it as a RAW. The
peek parameter allows one to look ahead in the buffer
without actually consuming the message. The
len parameter allows specifying the number of bytes to
get.
Argument
|
Type
|
In / Out
|
Default Value
|
C
|
UTL_TCP.CONNECTION
|
IN | OUT
|
|
LEN
|
BOOLEAN
|
IN
|
1
|
PEEK
|
BOOLEAN
|
IN
|
FALSE
|
Table 6.162:
Get_raw Parameters
Get_text
is a
utl_tcp
function that reads data, and returns it as a VARCHAR2.
Argument
|
Type
|
In / Out
|
Default Value
|
C
|
UTL_TCP.CONNECTION
|
IN | OUT
|
|
LEN
|
BOOLEAN
|
IN
|
1
|
PEEK
|
BOOLEAN
|
IN
|
FALSE
|
Table 6.163:
Get_text Parameters
Get_text_nchar
is a
utl_tcp
function that reads data and returns it as an NVARCHAR2.
Argument
|
Type
|
In / Out
|
Default Value
|
C
|
UTL_TCP.CONNECTION
|
IN | OUT
|
|
LEN
|
BOOLEAN
|
IN
|
1
|
PEEK
|
BOOLEAN
|
IN
|
FALSE
|
Table 6.164:
Get_text_nchar Parameters
Open_connection
is a
utl_tcp
function that simply opens a TCP/IP connection. It returns that
connection information as type
utl_tcp.connection..
Argument
|
Type
|
In / Out
|
Default Value
|
REMOTE_HOST
|
VARCHAR2
|
IN
|
|
REMOTE_PORT
|
PLS_INTEGER
|
IN
|
|
LOCAL_HOST
|
VARCHAR2
|
IN
|
NULL
|
LOCAL_PORT
|
PLS_INTEGER
|
IN
|
NULL
|
IN_BUFFER_SIZE
|
PLS_INTEGER
|
IN
|
NULL
|
OUT_BUFFER_SIZE
|
PLS_INTEGER
|
IN
|
NULL
|
CHARSET
|
VARCHAR2
|
IN
|
NULL
|
NEWLINE
|
VARCHAR2
|
IN
|
CRLF
|
TX_TIMEOUT
|
PLS_INTEGER
|
IN
|
NULL
|
Table 6.165:
Open_connection Parameters
Read_line
is a
utl_tcp
function that reads a line of data from a service and returns a
pls_integer for the number of characters read. The
peek parameter allows one to look ahead in the buffer
without actually consuming the message.
Argument
|
Type
|
In / Out
|
Default Value
|
C
|
UTL_TCP.CONNECTION
|
IN | OUT
|
|
DATA
|
VARCHAR2
|
IN | OUT
|
|
PEEK
|
BOOLEAN
|
IN
|
FALSE
|
Table 6.166:
Read_line Parameters
Read_raw
is a
utl_tcp
function that reads a line of data from a service and returns a
pls_integer for the number of bytes read.
Argument
|
Type
|
In / Out
|
Default Value
|
C
|
UTL_TCP.CONNECTION
|
IN | OUT
|
|
DATA
|
RAW
|
IN | OUT
|
|
LEN
|
PLS_INTEGER
|
IN
|
1
|
PEEK
|
BOOLEAN
|
IN
|
FALSE
|
Table 6.167:
Read_raw parameters
Read_text
is a
utl_tcp
function that reads a line of data from a service and returns a
pls_integer for the number of characters read.
Argument
|
Type
|
In / Out
|
Default Value
|
C
|
UTL_TCP.CONNECTION
|
IN | OUT
|
|
DATA
|
VARCHAR2
|
IN | OUT
|
|
LEN
|
PLS_INTEGER
|
IN
|
1
|
PEEK
|
BOOLEAN
|
IN
|
FALSE
|
Table 6.168:
Read_text Parameters
Write_line
is a
utl_tcp
function that transmits a line of data to a service and returns a
pls_integer for the number of characters sent.
Argument
|
Type
|
In / Out
|
Default Value
|
C
|
UTL_TCP.CONNECTION
|
IN | OUT
|
|
DATA
|
VARCHAR2
|
IN | OUT
|
|
Table 6.169:
Write_line Parameters
Write_raw
is a
utl_tcp
function that transmits a line of data to a service and returns a
pls_integer for the number of bytes sent.
Argument
|
Type
|
In / Out
|
Default Value
|
C
|
UTL_TCP.CONNECTION
|
IN | OUT
|
|
DATA
|
RAW
|
IN | OUT
|
|
LEN
|
PLS_INTEGER
|
IN
|
1
|
Table 6.170:
Write_raw Parameters
Write_text
is a
utl_tcp
function that transmits a line of data from a service and returns
a
pls_integer for the number of characters sent.
Argument
|
Type
|
In / Out
|
Default Value
|
C
|
UTL_TCP.CONNECTION
|
IN | OUT
|
|
DATA
|
VARCHAR2
|
IN | OUT
|
|
LEN
|
PLS_INTEGER
|
IN
|
1
|
Table 6.171:
Write_text Parameters
|
|
|
Get the Complete
Oracle Utility Information
The landmark book
"Advanced Oracle
Utilities The Definitive Reference" contains over 600 pages of
filled with valuable information on Oracle's secret utilities.
This book includes scripts and tools to hypercharge Oracle 11g
performance and you can
buy it
for 30% off directly from the publisher.
|
|
|