basic.%connect
Establishes a connection with a host through sockets.
Syntax
code=%connect( socket, addr.family, host, port )
Description
requests a connection between two sockets.
To compile successfully, the statement 'cfunction socket.builtin' must be included in the source code.
'socket' Is the file descriptor of the local socket returned by a previous call to the Pick/BASIC C function '%socket'.
'addr.family' Specifies the addressing scheme used by the protocol. This field must match the address family used when creating the socket. Valid values are defined in the include: 'dm,bp,unix.h socket.h'.
'host' Destination host name. This string must be known to the local network manager (normally defined in the '/etc/hosts/ Unix file). Internally, this string calls the BSD function 'gethostbyname' to read the '/etc/hosts' file.
'port' Port number on the distant host. Legal value for this field depends on the protocol. On TCP/IP, for example, valid port numbers are from 1024 to 32767.
Upon successful completion, a value of 0 is returned in 'code'. In case of error, a value of -1 is returned and the function 'system(0)' is set to the value of 'errno'.
The connection is closed when the socket is closed.
Example
cfunction socket.builtin
include dm,bp,unix.h socket.h
* Create a socket
fd=%socket(AF$INET,SOCKET$STREAM,0)
* Connect to the distant host
if %connect(fd,AF$INET,'prod',1024)<0 then
crt 'Connect failed'; stop
end
* Write data to it
msg='CONNECTED'
%write(fd,msg,len(msg))
* Terminate connection
%close(fd)
See Also
User Comments
What do you think?
Share your experience or ask a question by using the form below.
Login to leave your comments.
