SAS 9.1 Language Reference Dictionary, Volumes 1, 2 and 3

Enables you to read from or write to a TCP/IP socket

Valid: anywhere

Category: Data Access

Syntax

[ 1]

FILENAME fileref SOCKET hostname : portno < tcpip-options >;

[ 2]

FILENAME fileref SOCKET : portno SERVER < tcpip-options >;

Arguments

fileref

SOCKET

hostname : portno

: portno

SERVER

TCPIP-Options

BLOCKSIZE= blocksize

ENCODING= encoding-value

LRECL= lrecl

RECFM= recfm

RECONN= conn-limit

TERMSTR= eol-char

Details

The Basics A TCP/IP socket is a communication link between two applications. The server application creates the socket and waits for a connection. The client application connects to the socket. With the SOCKET access method, you can use SAS to communicate with another application over a socket in either client or server mode. The client and server applications can reside on the same machine or on different machines that are connected by a network.

As an example, you can develop an application using Microsoft Visual Basic that communicates with a SAS session that uses the TCP/IP sockets. Note that Visual Basic does not provide inherent TCP/IP support. You can obtain a custom control (VBX) from SAS Technical Support (free of charge) that allows a Visual Basic application to communicate through the sockets.

[ 1]  

Using the SOCKET Access Method in Client Mode

In client mode, a local SAS application can use the SOCKET access method to communicate with a remote application that acts as a server (and waits for a connection). Before you can connect to a server, you must know:

The remote application can be another SAS application, but it doesn t need to be. When the local SAS application connects to the remote application through the TCP/IP socket, the two applications can communicate by reading from and writing to the socket as if it were an external file. If at any time the remote side of the socket is disconnected, the local side will also automatically terminate.

[ 2]  

Using the SOCKET Access Method in Server Mode When the local SAS application is in server mode, it remains in a wait state until a remote application connects to it. To use the SOCKET access method in server mode, you need to know only the port number that you want the server to listen to for a connection. Typically, servers use well-known ports to listen for connections. These port numbers are reserved by the system for specific server applications. For more information about how well-known ports are defined on your system, refer to the documentation for your TCP/IP software or ask your system administrator.

If the server application does not use a well-known port, then the system assigns a port number when it establishes the socket from the local application. However, because any client application that waits to connect to the server must know the port number, you should try to use a well-known port.

While a local SAS server application is waiting for a connection, SAS is in a wait state. Each time a new connection is established, the EOV= variable in the DATA step is set to 1. Because the server accepts only one connection at a time, no new connections can be established until the current connection is closed. The connection closes automatically when the remote client application disconnects. The SOCKET access method continues to accept new connections until it reaches the limit set in the RECONN option.

Examples

Example 1: Communicating between Two SAS Applications Over a TCP/IP Socket

This example shows how two SAS applications can talk over a TCP/IP socket. The local application is in server mode; the remote application is the client that connects to the server. This example assumes that the server host name is hp720.unx.sas.com , that the well-known port number is 5000, and that the server allows a maximum of three connections before closing the socket.

Here is the program for the server application:

filename local socket ':5000' server reconn=3; /*The server is using a reserved */ /*port number of 5000. */ data tcpip; infile local eov=v; input x ; if v=1 then do; /* new connection when v=1 */ put 'new connection received'; end; output; run;

Here is the program for the remote client application:

filename remote socket 'hp720.unx.sas.com:5000'; data _null_; file remote; do i=1 to 10; put i; end; run;

See Also

Statements:

Категории