HP 3000 Manuals

More on Berkeley Sockets/iX [ COMMUNICATOR 3000 MPE MPE/iX RELEASE 4.0 ] MPE/iX Communicators


COMMUNICATOR 3000 MPE MPE/iX RELEASE 4.0

More on Berkeley Sockets/iX 

by Majid Mohazzab 
Commercial Systems Division 

USING BERKELEY SOCKETS/iX 

An overview of Berkeley (BSD)tm Sockets is discussed in the article
related article in the "Data Communications" section in this
Communicator.  Here more technical details of BSD sockets are described.
The client-server model is used in the following sections to better
explain Berkeley Sockets usage.  In the client-server model, a server is
a process that is waiting to be connected by a client process in order to
service the clients requests.

To have communications, a server and a client must first decide what
communication type they want to use.  They can choose either a
connection-based (stream) or a connectionless (datagram) communication
type.  The communication is initiated when the server and the client each
create a socket.  The server makes its socket accessible to the client by
binding it to a well known address.  The previous two steps, creating
sockets and binding, are common for stream and datagram communication
types.

In stream communications, a connection must be established between the
server and the client sockets.  The client issues a connect request, and
the server listens and then accepts the connection.  Once the connection
is accepted, the server and the client can send or receive data through
this connection.

In datagram communication, no connection is necessary.  But when sending
or receiving data, the address of the target socket must also be
specified.

socket() 

To use socket, a server and its client must call the ^fssocket system
calls to create sockets.

     s = socket(domain, type, protocol);
     int s, domain, type, protocol;

The socket descriptor s, is used in later socket-related system calls.

The domain is one of the following constants:

     AF_UNIX  for local domain
     AF_INET  for Internet domain

The type is one of the following constants:

     SOCK_STREAM  for connected communications
     SOCK_DGRAM   for connectionless communications

These constants are defined in the header file SOCKET.H.SYS.

The last parameter, the protocol, specifies a particular communication
protocol to be used with the socket.  Protocol can be set to zero, in
which case the system chooses a protocol type to use.

If the call succeeds, a socket descriptor is returned.  If the call
fails, a -1 is returned, and the global variable ^fserrno is set to an
error code.

bind() 

A created socket has no name.  The client process can not reach the
server, until the server binds its socket to a name.

The bind system call is used to bind a socket to a name.

     int bind(s, addr, addrlen)
     int s;
     struct sockaddr *addr;
     int addrlen;

The addr parameter is a pointer to the name or address structure of the
socket.  A name in Local domain contains a path name and a family,
which is always AF_UNIX. The Local path name in MPE/iX is any
arbitrary array of characters of up to 256 bytes (for example,
"SERVERSOCK.MYAPPLICATION").  A name in Internet domain contains a node
Internet address, a host port number, and a family, which is always
AF_INET. The addrlen parameter is the size of the name structure.

If the call succeeds, a value of 0 is returned.  If the call fails, a
value of -1 is returned, and the global variable errno is set to an error
code.

listen() 

To have a stream (connection-based) communication type, the server and
the client must create a connection, before any data can be exchanged
between them.  To establish a connection, the server must first indicate
it is willing to accept connections by using the listen system call.

     int listen(s, backlog)
     int s, backlog;

The backlog parameter specifies the number of outstanding connections
that can be queued waiting for acceptance.

If the call succeeds, a value of 0 is returned.  If the call fails, a
value of -1 is returned, and the global variable errno is set to an error
code.

accept() 

To wait for an incoming connection request, the server uses the accept 
system call.

     ns = accept(s, addr, addrlen)
     int s, ns;
     struct sockaddr *addr;
     int addrlen;

Accept creates a new socket with the same properties as s, and assigns a
new file descriptor to the new socket.  The new file descriptor is used
for sending or receiving data.  Accept also returns the address of the
connecting socket that is pointed to by the addr parameter.  Accept is
used only with stream type sockets.  In local domain sockets, addr and
addrlen are ignored.

If the call succeeds, accept returns a new socket descriptor.  If the
call fails, a value of -1 is returned and the global variable errno is
set to an error code.

connect() 

To establish a connection with the server, the client uses the connect 
system call.

     connect(s, addr, addrlen)
     int s;
     struct sockaddr *addr;
     int addrlen;

The addr parameter is a pointer to the address structure of the socket
created by the other end (the socket a listen was done on).  Addrlen is
the size of the socket address structure.  Connect is used only with
stream type sockets.

If the call succeeds, a value of 0 is returned.  If the call fails, a
value of -1 is returned, and the global variable errno is set to an error
code.

send() 

After a connection is established, the client can send data to the server
by using the send system call.

     int send(s, msg, len, flags)
     int s;
     char *msg;
     int len, flags;

The parameter msg is a pointer to the buffer that contains the message.
The parameter len is the buffer len.  Currently the flags parameter can
only be set to zero.

If the call succeeds, it returns the number of characters that it sends.
If an error occurs, A -1 is returned.

sendto() 

In datagram communication type, the client uses the sendto system call to
send data.  With this communication type there is no connection between
the sever and the client, so the address of the destination socket must
also be passed with the data in every ^fssendto call.

        int sendto(s, msg, len, flags, to, tolen)
        int s;
        char *msg;
        int len, flags;
        struct sockaddr *to;
        int tolen;

The to and tolen parameters specify a pointer to the destination socket
address structure and size of the address structure of the receiving end.

If the call succeeds, it returns the number of characters that it sends.
If an error occurs, A -1 is returned.

recv() 

The server uses the recv system call in the stream communication type to
receive data sent by the client.

        int recv(s, buf, len, flags)
        int s;
        char *buf;
        int len, flags;

Buf is a pointer to the buffer where messages are placed.  Len is the
maximum number of bytes that will fit into the buffer pointed to by buf.
Currently the parameter flags can be only set to zero.

If the call succeeds, it returns the number of characters that have been
received.  If an error occurs, A -1 is returned.

recvfrom() 

To receive messages from an unconnected datagram type socket, the server
uses the recvfrom system call.

       int recvfrom(s, buf, len, flags, from, fromlen);
       int s;
       char *buf;
       int len, flags;
       struct sockaddr *from;
       int *fromlen;

The parameters from and fromlen returned by recvfrom specify a pointer to
the source socket address and size of the address structure of the
sending socket.  Fromlen and from parameters are ignored in Local domain
sockets.

shutdown() 

To shutdown a socket, the shutdown system call is used.

       int shutdown(s, how)
       int s, how;

Depending on the value of the how parameter, once the socket has been
shutdown it can not receive, send, or send and receive data.

getsockname() 

Getsockname returns the address of the socket specified by s.

        int getsockname(s, addr, addrlen);
        int s;
        struct sockaddr *addr;
        int *addrlen;

Addr is a pointer to the socket address structure.  Addrlen is the size
of the socket address structure pointed to by addr.  Getsockname is only
supported for the Internet domain.

getpeername() 

Getpeername returns the address of the peer socket that is connected to
the socket specified by s.

        int getpeername(s, addr, addrlen)
        int s;
        struct sockaddr *addr;
        int *addrlen;

Addr is a pointer to the peer socket address structure.  Addrlen is the
size of the peer socket address structure pointed to by addr.
Getpeername is only supported for the Internet domain.

socketpair() 

Socketpair is called to create a pair of connected sockets.

        int socketpair(d, type, protocol, sv)
        int d, type, protocol;
        int sv[2];

The parameter d specifies the socket domain.  The only supported socket
domain is the Local domain.  The definitions for type, and protocol are
the same as for the socket system call.  The descriptor for the created
sockets are returned in the ssv parameter.

NAMING SERVICE ROUTINES 

The following Naming Service Routines are the ones offered as part of the
MPE/iX operating system.

   *   gethostbyname

   *   gethostbyaddr

gethostbyname() 

        struct hostent *gethostbyname(name)
        char *name;

The gethostbyname routine returns a pointer to the hostent data structure
that matches the parameter name.

gethostbyaddr() 

        struct hostent *gethostbyaddr(addr, len, type)
        char *addr;
        int len, type;

The gethostbyaddr routine returns a pointer to the hostent data structure
that matches the input Internet address addr.

     -------------------------------------------------------------------

1 Copyright 1979, 1980, 1983, 1985-1990 The Regents of the University of
California.  This software and documentation is based in part on
materials licensed from the Regents of the University of California.  We
acknowledge the role of the Computer Systems Research Group and the
Electrical Engineering and Computer Sciences Department of the University
of California at Berkeley and the other named contributors in their
development.



MPE/iX Communicators