HP Rp7410 BSD Sockets Interface Programmer's Guide

HP Rp7410 - Server - 0 MB RAM Manual

HP Rp7410 manual content summary:

  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 1
    BSD Sockets Interface Programmer's Guide Edition 6 B2355-90136 HP 9000 Networking E0497 Printed in: United States © Copyright 1997 Hewlett-Packard Company.
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 2
    product and replacement parts can be obtained from your local Sales and Service Office. Restricted Rights Legend. Use, duplication or disclosure by the COMPANY 3000 Hanover Street Palo Alto, California 94304 U.S.A. Use of this manual and flexible disk(s) or tape cartridge(s) supplied for this pack is
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 3
    ©copyright 1980, 1984, 1986 Novell, Inc. ©copyright 1986-1992 Sun Microsystems, Inc. ©copyright 1985-86, 1988 Massachusetts Institute of Technology. ©copyright 1989-93 The Open Software Foundation, Inc. ©copyright 1986 Digital Equipment Corporation. ©copyright 1990 Motorola, Inc. ©copyright 1990,
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 4
    4
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 5
    Address 32 Getting the Port Address for the Desired Service 33 Using a Wildcard Local Address 34 Writing the Server Process 35 Creating a Socket 35 Binding a Socket Address to the Server Process's Socket 36 Setting Up the Server to Wait for Connection Requests 37 Accepting a Connection 38
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 6
    Contents Example Using Internet Stream Sockets 48 3. Advanced Topics for Stream Sockets Socket Options 61 Getting and Setting Socket Options 62 SO_REUSEADDR 65 SO_KEEPALIVE 66 SO_DONTROUTE 67 SO_SNDBUF 67 SO_RCVBUF 67 SO_LINGER 68 SO_USELOOPBACK 69 SO_OOBINLINE 69 SO_SNDLOWAT 69
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 7
    Address Variables 88 Getting the Remote Host's Network Address 89 Getting the Port Address for the Desired Service 90 Using a Wildcard Local Address 91 Writing the Server and Client Processes 92 Creating Sockets 92 Binding Socket Addresses to Datagram Sockets 93 Sending and Receiving Messages
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 8
    Using UNIX Domain Stream Sockets 140 7. Using UNIX Domain Datagram Sockets Overview 148 Preparing Address Variables 150 Declaring Socket Address Variables 150 Writing the Server and Client Processes 152 Creating Sockets 152 Binding Socket Addresses to UNIX Domain Datagram Sockets . . . 153 8
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 9
    Messages 156 Closing a Socket 159 Example Using UNIX Domain Datagram Sockets 160 8. Programming Hints Troubleshooting 167 Using Diagnostic Utilities as Troubleshooting Tools 168 Adding a Server Process to the Internet Daemon 169 Summary Tables for System and Library Calls 173 Portability
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 10
    Contents 10
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 11
    when extensive changes are made. Manual updates may be issued between editions to correct errors or document product changes. To ensure that you receive the updated or new editions, you should subscribe to the appropriate product support service. See your HP sales representative for details. First
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 12
    12
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 13
    a list of other hosts or nodes your HP 9000 product can communicate with (this list may also be obtained from your node manager). This guide is organized as follows: Chapter 1 BSD Sockets Concepts provides an overview of the Client-Server Model. Chapter 2 Using Internet Stream Sockets describes
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 14
    exchange data between AF_UNIX datagram sockets on the same node without establishing a connection. Programming Hints contains information about troubleshooting, port addresses, diagnostic utilities, internet daemon, inetd, portability issues, and system and library calls. BSD Sockets Quick Reference
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 15
    1 BSD Sockets Concepts The BSD Sockets facility allows you to create distributed applications that pass data between programs (on the same computer or on separate computers on the network) without requiring an understanding of the 15
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 16
    is a program development tool. Before you attempt to use BSD Sockets, you may need to familiarize yourself with the C programming language and the HP-UX operating system. You could implement a BSD Sockets application using FORTRAN or Pascal, but all library calls and include files are implemented in
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 17
    BSD Sockets Concepts Introduction Introduction This guide describes the steps involved in establishing and . The internet address family (AF_INET) and the Berkeley UNIX Domain address family (AF_UNIX) are supported. A means of labeling a socket so that it is distinguishable from other sockets on a
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 18
    The internet (AF_INET) address family domain is supported. The UNIX Domain (AF_UNIX) address family domain is also supported, for local communication only. A four-byte connected sockets provides an interface similar to that of HP-UX pipes. A socket is identified by a socket descriptor. 18 Chapter 1
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 19
    address is the directory pathname bound to the socket. A socket descriptor is an HP-UX file descriptor that references a socket instead of an ordinary file. man page for more information on TCP. Provides the underlying communication support for datagram sockets. The User Datagram Protocol (UDP) is an
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 20
    You Can Use BSD Sockets How You Can Use BSD Sockets The best example of how BSD Sockets can be used is the Internet Services. These services use BSD Sockets to communicate between remote hosts. Using the BSD Sockets facility, you can write your own distributed application programs to do a variety
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 21
    and client process. An example of an asymmetrical application is the ftp service. Creating a Connection: the Client-Server Model The following figures illustrate conceptual views of the client-server model at three different stages of establishing a connection. The completed steps are included
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 22
    Figure 1-1 BSD Sockets Concepts The Client-Server Model Client-Server in a Pre-Connection State 22 Chapter 1
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 23
    Figure 1-2 BSD Sockets Concepts The Client-Server Model Client-Server at Time of Connection Request Chapter 1 23
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 24
    Figure 1-3 BSD Sockets Concepts The Client-Server Model Client-Server When Connection is Established 24 Chapter 1
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 25
    Routines BSD Sockets Library Routines The library routines and system calls that you need to implement a BSD Sockets application are described throughout the guide. In addition, a complete list of all these routines and system calls is provided in the "Summary Tables for Library and System Calls
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 26
    BSD Sockets Concepts BSD Sockets Library Routines 26 Chapter 1
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 27
    2 Using Internet Stream Sockets 27
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 28
    releases support two variations of sockets behavior: classic HP-UX sockets and X/Open sockets. By default, users receive classic HP-UX HP-UX sockets, see the remainder of this manual as well as the man pages. For detailed information about X/Open sockets, see "CAE Specification, Networking Services
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 29
    connection using stream sockets. Creating/Terminating BSD Sockets Connections Using Internet Stream Sockets Client Process Activity System Call Used Server Process Activity System Call Used create a socket bind a socket address (optional) request a connection send data receive data disconnect
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 30
    the call interacts with other BSD Sockets system calls. • Where to find details on the system call. The stream socket program examples are at the end of these descriptive sections. You can refer to the example code as you work through the descriptions. 30 Chapter 2
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 31
    connection. Your server process needs to: • Declare socket address variables. • Assign a wildcard address. • Get the port address of the service that you port address for the service that you want to use. These activities are described next. Refer to the program example at the end of this chapter to
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 32
    a specific service. Specifies the internet address. Assign this field when you get the internet address for the remote host. The server process only needs size of struct in_addr) from /etc/hosts or from NIS name server. gethostbyname and its parameters are described in the following table. Include
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 33
    .s_addr = ((struct in_addr *)(hp->h_addr))->s_addr; The argv[1] parameter is the host name specified in the client program command line. Refer to the gethostent(3n) man page for more information on gethostbyname. Getting the Port Address for the Desired Service When a server process is preparing to
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 34
    Sockets Preparing Address Variables #include struct servent *sp; /* pointer to service info */ ... sp = getservbyname ("example", "tcp"); peeraddr.sin_port = sp->s_port; When to Get the Server's Socket Address The server process needs to get the socket address before binding the listen
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 35
    with and serve a client process. Creating a Socket The server process must call socket to create a communication endpoint. socket and returned is the socket descriptor for the newly created socket. This number is an HP-UX file descriptor and can be used for reading, writing or any standard file
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 36
    . Otherwise, a client process would not know what port to connect to for the desired service. Set up the address structure with a local address before you make a bind call. Use a wildcard address so your server process does not have to look up its own internet address. bind and its parameters are
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 37
    and before any other BSD Sockets system calls. Refer to the bind(2) man page for more information on bind. Setting Up the Server to Wait for Connection Requests Once your server process has an address bound to it, it must call listen to set up a queue that accepts incoming connection requests. The
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 38
    can respond to connection requests. Refer to the listen(2) man page for more information on listen. Accepting a Connection The server process can accept any connection requests that enter its queue after it executes listen. accept creates a new socket for the connection and returns the socket
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 39
    structure where address will be put pointer to the size of struct sockaddr_in OUTPUT Value unchanged pointer to socket address of client socket that server's new socket is connected to pointer to the actual length of address returned in addr Function result: socket descriptor of new socket if
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 40
    client process must make to connect with and be served by a server process. Creating a Socket The client process must call socket to create returned is the socket descriptor for the newly created socket. This number is an HP-UX file descriptor and can be used for reading, writing or any standard fi
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 41
    Using Internet Stream Sockets Writing the Client Process Requesting a Connection Once the server process is listening for connection requests, the client process can request a connection with the connect call. connect and its parameters are described in the following
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 42
    the Client Process The client process does not get feedback that the server process has completed the accept call. As soon as the connect client process should request a connection after socket is created and after server socket has a listening socket. Refer to the connect(2) man page for more information
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 43
    executed, the connection is established and data can be sent and received between the two socket endpoints. Because the stream socket descriptors correspond to HP-UX file descriptors, you can use the read and write calls (in addition to recv and send) to pass data through a socket-terminated channel
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 44
    0); send blocks until the specified number of bytes have been queued to be sent, unless you are using nonblocking I/O. When to Send Data The server or client process should send data after the connection is established. Refer to the send(2) man page for more information on send. Receiving Data recv
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 45
    Using Internet Stream Sockets Sending and Receiving Data Parameter Description of Contents INPUT Value s buf len flags socket descriptor of local socket descriptor of socket socket receiving data pointer to data buffer pointer to buffer that is to receive data maximum number of bytes that
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 46
    remains in the socket buffer as though it had not been read yet. The next recv returns the same data. When to Receive Data The server or client process should receive data after connection is established. Refer to the recv(2) man page for more information on recv. 46 Chapter 2
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 47
    socket descriptor. The daemon process should do a close of the socket descriptor to avoid keeping the socket open once the server is through with it. Because the server performs the work, the daemon does not use the socket after the fork. close decrements the file descriptor reference count. Once
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 48
    as examples only of stream socket usage and are not Hewlett-Packard supported products. These program examples demonstrate how to set up and use in conjunction with the server program. The client program requests a service called example from the server program. The server process receives requests
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 49
    socket descriptor */ struct hostent *hp; /* pointer to host info for remote host */ struct servent *sp; /* pointer to service information */ long timevar; char peer socket address */ /* * M A I N * * This routine starts the server. It forks, leaving the child * to do all the work, so it does
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 50
    to 5. 20 */ if (listen(ls, 5) == -1) { perror(argv[0]); fprintf(stderr, "%s: unable to listen on socket\n",argv[0]); exit(1); } /* Now, all the initialization of the server is * complete, and any user errors will have already * been detected. Now we can fork the daemon and * return to the user. We
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 51
    From now on, the daemon will not report any * error messages. This daemon will loop forever, * waiting for connections and forking a child * server to handle each one. */ fclose(stdin); fclose(stderr); /* Set SIGCLD to SIG_IGN, in order to prevent * the accumulation of zombies as each child
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 52
    client. * It will also write some logging information to stdout. * */ server() { int reqcnt = 0; /* keeps count of number of requests */ char dot format. */ hostname = inet_ntoa(peeraddr_in.sin_addr); } else { hostname = hp->h_name; /* point to host's name */ } /* Log a startup message.
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 53
    recv call will return zero bytes, signalling an * end-of-file condition. This is is how the server * will know that no more requests will follow * } /* The loop has terminated, because there are no * more requests to be serviced. As above, this * close will block until all of the sent replies * have
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 54
    hp; struct servent *sp; /* pointer to host info for remote host */ /* pointer to service routine is the client that requests service from the * remote example server. It creates a connection, sends connection in one direction to * signal the server about the end of data, and then receives * all
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 55
    .sin_addr.s_addr= ((struct in_addr*)(hp->h_addr)->s_addr; /* Find the information for the "example" server * in order to get the needed port number. */ sp = getservbyname ("example", "tcp"); if (sp == NULL) { fprintf(stderr, "%s: example not found in /etc/services
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 56
    on error ", argv[0]); fprintf(stderr, "on send number %d\n", i); exit(1); } } /* Now, shutdown the connection for further sends. * This causes the server to receive an end-of-file * condition after receiving all the requests that * have just been sent, indicating that no further * requests will be
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 57
    . * This loop will terminate when the recv returns * zero, which is an end-of-file condition. This * will happen after the server has sent all of its * replies, and closed its end of the connection. */ while (i = recv(s, buf, 10, 0)) { if (i == -1) { errout: perror(argv[0]); fprintf(stderr
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 58
    Using Internet Stream Sockets Example Using Internet Stream Sockets 58 Chapter 2
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 59
    3 Advanced Topics for Stream Sockets 59
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 60
    Advanced Topics for Stream Sockets This chapter explains the following: • Socket options. • Synchronous I/O multiplexing with select. • Sending and receiving data asynchronously. • Nonblocking I/O. • Using shutdown. • Using read and write to make stream sockets transparent. • Sending and receiving
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 61
    Socket Options The operation of sockets is controlled by socket level options. The following options are supported for internet stream sockets: • SO_REUSEADDR • SO_KEEPALIVE • SO_DONTROUTE • SO_SNDBUF • SO_RCVBUF • SO_LINGER • SO_USELOOPBACK • SO_OOBINLINE • SO_SNDLOWAT • SO_RCVLOWAT • SO_SNDTIMEO
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 62
    Socket Options • SO_SNDTIMEO • SO_RCVTIMEO • SO_BROADCAST • SO_REUSEPORT In addition, the SO_DEBUG option is supported for compatibility only; it has no functionality. Options for protocol levels are described in the individual protocol manual pages, such as tcp(7p), udp(7p), and ip(7p). The next
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 63
    pointer to current value of option pointer to length of optval INPUT Value socket descriptor for which option values are to be returned SOL_SOCKET supported option name pointer to buffer where option's current value is to be returned pointer to maximum number of bytes to be returned by optval
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 64
    The SO_LINGER option is a combination. It sets a linger value, and also toggles linger behavior on and off. In previous releases SO_DONTLINGER was supported. For SO_LINGER, optval points to a struct linger, defined in /usr/include/sys/socket.h. The structure contains an integer boolean flag to
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 65
    option pointer to option input value optlen length of optval INPUT Value supported option name Must be at least size of (int). Holds either uniqueness requirement is violated. Example of the SO_REUSEADDR Option A network daemon server is listening on a specific port: port 2000. If you executed
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 66
    250.100). If you executed netstat an, the output would resemble: Active connections (including servers) Proto Recv-Q Send-Q Local Address Foreign Address (state) tcp 0 0 192.6.250. are trying to send, or an end-of-file condition if you are trying to receive) that the connection is broken. 66
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 67
    NOTE Advanced Topics for Stream Sockets Socket Options SO_DONTROUTE This option is AF_INET socket-specific. SO_DONTROUTE indicates that outgoing messages should bypass the standard routing facilities. Instead, messages are directed to the appropriate network interface according to the network
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 68
    Table 3-1 Advanced Topics for Stream Sockets Socket Options Summary Information for Changing Socket Buffer Size SocketType (Protocol) When Buffer Size Increase Allowed When Buffer Size Decrease Allowed Maximum Buffer Size stream (TCP) at any time only prior to establishing a connection 262144
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 69
    the user to set or fetch the low water mark for the socket's send socket buffer. At present, this option is not used. It is supported in anticipation of future use. Chapter 3 69
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 70
    allows the user to set or fetch the timeout value for the socket's receive socket buffer. At present, this option is not used. It is supported in anticipation of future use. SO_TYPE This option is used to return the socket type (e.g., stream, datagram, etc.). Use this option only with the getsockopt
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 71
    Advanced Topics for Stream Sockets Socket Options are bound to the port. All processes that share the port must specify this option. For more information on using this option, see "Sending and Receiving IP Multicast Datagrams," in Chapter 5. Chapter 3 71
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 72
    Advanced Topics for Stream Sockets Synchronous I/O Multiplexing with Select Synchronous I/O Multiplexing with Select The select system call can be used with sockets to provide a synchronous multiplexing mechanism. The system call has several parameters which govern its behavior. If you specify a
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 73
    Advanced Topics for Stream Sockets Synchronous I/O Multiplexing with Select The following example illustrates the select system call. Since it is possible for a process to have more than 32 open file descriptors, the bit masks used by select are interpreted as arrays of integers. The header file sys/
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 74
    Advanced Topics for Stream Sockets Synchronous I/O Multiplexing with Select exit(1); } if (FD_ISSET(s, &read_mask)) do_read(s); /* something to read on socket s */ /* fall through as maybe more to do */ if (FD_ISSET(s, &write_mask)) do_write(s); /* space to write on socket s */ } } 74
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 75
    Advanced Topics for Stream Sockets Sending and Receiving Data Asynchronously Sending and Receiving Data Asynchronously Asynchronous sockets allow a user program to receive an SIGIO signal when the state of the socket changes. This state change can occur, for example, when new data arrives.
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 76
    is not illustrated here. For example, the socket could be of type SOCK_DGRAM; a signal here can be interpreted as the arrival of a service-request packet. Multiple identical servers could be set up, and the first available one could receive and process the packet. int flag = 1; int iohndlr(); signal
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 77
    error. This is also true for write. The O_NDELAY flag for fcntl(2) is also supported. If you use this flag and there are no data available to be received on value of -1 and the EAGAIN error. This is the same as returning an end-of-file condition. This is also true for send, sendto, sendmsg, and write
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 78
    or a close. • If how = 1, shutdown starts a graceful disconnect by attempting to send any unsent data before preventing further sending. shutdown sends an end-of-file condition to the peer, indicating that there are no more data to be sent. Once both shutdown(s, 0) and shutdown(s, 1) have been
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 79
    (s, 1); When to Shut Down a Socket Optionally, after the process has sent all messages and wants to indicate that it is done sending, shut down the server or client process. Refer to the shutdown(2) man page for more information on shutdown. Chapter 3 79
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 80
    Advanced Topics for Stream Sockets Using Read and Write to Make Stream Sockets Transparent Using Read and Write to Make Stream Sockets Transparent An example application of read and write with stream sockets is to fork a command with a socket descriptor as stdout. The peer process can read input
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 81
    includes an out-of-band data facility. Out-of-band data uses a logically independent transmission channel associated with a pair of connected stream sockets. TCP supports the reliable delivery of only one out-of-band message at a time. The message can be a maximum of one byte long. Out-of-band
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 82
    &pid) < 0) { perror("ioctl(SIOCSPGRP)"); } /* ** If a process needs to be notified, it should be ** pid = -getpgrp(); */ If the server process is sending data to the client process, and a problem occurs, the server can send an out-of-band data byte by executing a send with the MSG_OOB flag set. This
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 83
    out-of-band data. ioctl returns a 1 when the stream pointer reaches the out-of-band byte pointer. The next recv provides data sent by the server after the out-of-band message. The following shows how the SIOCATMARK request can be used in a SIGURG interrupt handler. /* s is the socket with urgent
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 84
    Advanced Topics for Stream Sockets Sending and Receiving Out-of-band Data printf("received %c OOB\n", mark); return; } 84 Chapter 3
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 85
    4 Using Internet Datagram Sockets This chapter describes communication between processes using internet datagram sockets. 85
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 86
    sources with one socket. There is no two-process model, although a two-process model is the simplest case of a more general multiprocess model. The terms server and client are used in this chapter only in the application sense. There is no difference in the calls that must be made by the
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 87
    bind a socket address send message receive message System Call Used Server Process Activity socket() bind() sendto() or sendmsg() create a call. The datagram socket program examples are at the end of these descriptive sections. You can refer to the example code as you work through the
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 88
    client process needs to: • Declare socket address variables. • Get the remote server's internet address. • Get the port address for the service that you want to use. These activities are described next. Refer to the program example at the end of this chapter to see how these activities work together
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 89
    . Specifies the internet address. Assign this field when you get the internet address for the remote host. The server process must bind the port address of the service to its own socket and establish an address structure to store the clients' addresses when they are received with recvfrom. The client
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 90
    hp; /* point to host info for name server host */ ... servaddr.sin_family = AF_INET; hp = gethostbyname (argv[1]); servaddr.sin_addr.s_addr = ((struct in_addr *)(hp Service When a client process needs to use a service that is offered by some server process, it must send a message to the server's
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 91
    Internet Datagram Sockets Preparing Address Variables When to Get Server's Socket Address The server process should get the server's socket address before binding. The client process should get the server's socket address before client requests the service from the host. Refer to the getservent(3N
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 92
    Writing the Server and Client Processes Writing the Server and Client Processes This section explains the calls your server and number returned is the socket descriptor for the newly created socket. This number is an HP-UX file descriptor and can be used for reading, writing or any standard file
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 93
    Until an address is bound, other processes have no way to reference it. The server process must bind a specific port address to its socket. Otherwise, a client process would not know what port to send requests to for the desired service. The client process can let the local host bind its local port
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 94
    is successful, -1 if failure occurs. Example: struct sockaddr_in myaddr; ... bind (s, &myaddr, sizeof(struct sockaddr_in)); When to Bind Socket Addresses The client and server process should bind socket addresses after the socket is created and before any other BSD Sockets system calls. Refer to the
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 95
    or an error will occur. send is described in the "Sending Data" section in the "BSD Sockets: Using Internet Stream Sockets" chapter of this guide and in the send(2) man page. sendto and its parameters are described in the following table. Include files: System call: #include #include
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 96
    address of recipient socket size of to 0 (no options are currently supported) pointer to the socket address that message should be sent to length the expected response does not occur. When to Send Data The client or server process should send data after sockets are bound. Refer to the send(2) man
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 97
    the three calls are identical. recv is described in the "Receiving Data" section of the "BSD Sockets: Using Internet Stream Sockets" chapter in this guide and in the recv(2) man page. recvfrom and its parameters are described in the following table. Include files: System call: #include
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 98
    Using Internet Datagram Sockets Sending and Receiving Messages Parameter flags from fromlen Contents settings for optional flags address of socket that sent message pointer to the size of from INPUT Value 0 or MSG_PEEK pointer to address structure, not used for input pointer to size of from OUTPUT
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 99
    data buffer as though it had not been read yet. The next recvfrom will return the same message. When to Receive Data The client or server process should receive data after sockets are bound. Refer to the recv(2) man page for more information on recvfrom and recvmsg. Chapter 4 99
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 100
    program and your process terminates, the sockets are closed for you. If you need to close a socket while your program is still running, use the HP-UX file system call close. You may have more than one process with the same socket descriptor if the process with the socket descriptor executes
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 101
    only of datagram socket usage and are not Hewlett-Packard supported products. These program examples demonstrate how to set up to lookup * host names. * * This program provides a service called "example". It is an * example of a simple name server. In order for * it to function, an entry for it
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 102
    */ struct hostent *hp; struct servent *sp; /* pointer to info of requested host */ /* pointer to service information */ struct * host name for which the requester desires to know the * internet address. The server will look up the name in its * /etc/hosts file, and return the internet
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 103
    bind(s, &myaddr_in, sizeof(struct sockaddr_in)) == -1) { perror(argv[0]); printf("%s: unable to bind address\n", argv[0]); exit(1); } /* Now, all the initialization of the server is * complete, and any user errors will have already * been detected. Now we can fork the daemon and * return to the
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 104
    host has been configured to use the NIS * server or name server (BIND), it is desirable * not to 1 bytes are read so that * room is left at the end of the buffer * for a null character. */ cc = return buffer. */ reqaddr.s_addr = ((struct in_addr *)(hp->h_addr))->s_addr; } /* Send the response back to
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 105
    errno; int s; /* socket descriptor */ struct hostent *hp; struct servent *sp; /* pointer to info for nameserver host */ /* pointer to service information */ struct sockaddr_in myaddr_in; /* for local socket address */ struct sockaddr_in servaddr_in;/* for server socket addres */ Chapter 4 105
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 106
    * * This routine is the client which requests service from * the remote "example server". It will send a message to the * remote server address. */ servaddr_in.sin_family = AF_INET; /* Get the host info for the server's hostname that the * user passed in. */ hp = gethostbyname (argv[1]); if (hp
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 107
    sin_addr.s_addr = ((struct in_addr *) (hp->h_addr))->s_addr; /* Find the information for the "example" server * in order to get the needed port number. */ sp = getservbyname ("example", "udp"); if (sp == NULL) { fprintf(stderr, "%s: example not found in /etc/services
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 108
    Using Internet Datagram Sockets Example Using Datagram Sockets if (recv (s, &reqaddr, sizeof(struct in_addr), 0) == -1) { if (errno == EINTR) { /* Alarm went off & aborted the receive. * Need to retry the request if we have * not already exceeded the retry limit. */ if (-retry) { goto again; } else
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 109
    5 Advanced Topics for Internet Datagram Sockets This chapter explains the following: 109
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 110
    Advanced Topics for Internet Datagram Sockets • SO_BROADCAST socket option. • Specifying a default socket address. • Synchronous I/O multiplexing with select. • Sending and receiving data asynchronously. • Sending and receiving IP multicast datagrams. • Nonblocking I/O. • Using broadcast addresses.
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 111
    Advanced Topics for Internet Datagram Sockets SO_BROADCAST Socket Option SO_BROADCAST Socket Option This option is AF_INET socket-specific. SO_BROADCASTADDR establishes permission to send broadcast datagrams from the socket. Chapter 5 111
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 112
    NOTE Advanced Topics for Internet Datagram Sockets Specifying a Default Socket Address Specifying a Default Socket Address It is possible (but not required) to specify a default address for a remote datagram socket. This allows you to send messages without specifying the remote address each time.
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 113
    length of address pointed to by addr Function result: 0 if connect is successful, -1 if failure occurs. When to Specify a Default Socket Address The client or server process should specify a default socket address after sockets are bound. Chapter 5 113
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 114
    Advanced Topics for Internet Datagram Sockets Synchronous I/O Multiplexing with Select Synchronous I/O Multiplexing with Select The select system call can be used with sockets to provide a synchronous multiplexing mechanism. The system call has several parameters which govern its behavior. If you
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 115
    occur, for example, when new data arrive. More information on SIGIO can be found in the "Advanced Topics for Internet Stream Sockets" chapter of this guide. Chapter 5 115
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 116
    a member of the same group on each interface. Sending IP Multicast Datagrams IP multicasting is supported only for AF_INET sockets of type SOCK_DGRAM and only on networks for which the interface supports multicasting. To send a multicast datagram, the application specifies an IP multicast address as
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 117
    the application are sent from the default interface. Otherwise, addr.s_addr must specify the IP address of a local network interface that supports multicasting. Specifying the Scope of a Multicast IP_MULTICAST_TTL By default, multicast datagrams are sent only to systems on a local network. If the
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 118
    only a modest improvement in system performance. Receiving IP Multicast Datagrams IP multicasting is supported only for AF_INET sockets of type SOCK_DGRAM and only on networks for which the interface supports multicasting. In order to receive multicast datagrams, an application must bind to the
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 119
    Advanced Topics for Internet Datagram Sockets Sending and Receiving IP Multicast Datagrams because the system uses some link-layer multicast addresses. For example, the E/ISA interface card is limited to 16 multicast addresses, and the system uses two of those. So all applications in the system can
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 120
    Advanced Topics for Internet Datagram Sockets Sending and Receiving IP Multicast Datagrams Note that imr_interface must match the field that was used when the IP_ADD_MEMBERSHIP socket option was specified for imr_multiaddr. Sharing a Multicast Port SO_REUSEPORT If more than one application may bind to
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 121
    the value -1 and the EWOULDBLOCK error. The O_NDELAY flag for fcntl(2) is also supported. If you use this flag and there is no message available to be received the value of -1 and the EAGAIN error. This is the same as returning an end-of-file condition. This is also true for send , sendto, and write if
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 122
    Advanced Topics for Internet Datagram Sockets Using Broadcast Addresses Using Broadcast Addresses In place of a unique internet address or the wildcard address, you can also specify a broadcast address. A broadcast address is an internet address with a local address portion of all 1s. If you use
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 123
    6 Using UNIX Domain Stream Sockets This chapter describes creating a UNIX Domain stream socket connection between two processes executing on the same node. 123
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 124
    using stream sockets. Creating/Terminating BSD Sockets Connections Using UNIX Domain Stream Sockets Client Process Activity System Call Used Server Process Activity System Call Used create a socket request a connection send data socket() connect() write() or send() create a socket bind
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 125
    Process Activity receive data disconnect socket (optional) System Call Used Server Process Activity System Call Used send data read() or recv() UNIX Domain stream socket program examples are at the end of these descriptive sections. You can refer to the example code as you work through
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 126
    a connection. Your server process needs to: • Declare socket address variables. • Get the pathname (character string) for the service you want to (character string) for the service you want to use. These activities are described next. Refer to the program example at the end of this chapter to see
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 127
    Using UNIX Domain Stream Sockets Preparing Address Variables The server process only needs an address for its own socket. Your client process will not need an address for its own socket. Chapter 6 127
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 128
    is the socket descriptor for the newly created socket. This number is an HP-UX file descriptor and can be used for reading, writing or any standard a file descriptor for an open file. When to Create Sockets The server process should create sockets before any other BSD Sockets system calls. Refer to
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 129
    socket, other processes have no way to reference it. The server process must bind a specific pathname to this socket, which is used for listening. Otherwise, a client process would not know what pathname to connect to for the desired service. Set up the address structure with a local address before
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 130
    The current default setting is 20, but may change in future releases. When to Set Server Up to Listen The server process should set up server to listen after socket is created and bound and before the server can respond to connection requests. Refer to the listen(2) man page for more information on
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 131
    ; int *addrlen; Parameter Contents INPUT Value OUTPUT Value s addr addrlen socket descriptor of local socket socket address length of address socket descriptor of server socket unchanged pointer to address structure where address will be put pointer to socket address of client socket that
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 132
    successful, -1 if failure occurs. Example: struct sockaddr_un peeraddr; ... addrlen = sizeof(sockaddr_un); s = accept (ls, &peeraddr, &addrlen); There is no way for the server process to indicate which requests it can accept. It must accept all requests or none. When to Accept a Connection The
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 133
    client process must make to connect with and be served by a server process. Creating a Socket The client process must call socket to create returned is the socket descriptor for the newly created socket. This number is an HP-UX file descriptor and can be used for reading, writing or any standard fi
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 134
    a connection. When the connection is ready, the client process completes its connect call and the server process can complete its accept call. The client process does not get feedback that the server process has completed the accept call. As soon as the connect call returns, the client process
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 135
    Using UNIX Domain Stream Sockets Writing the Client Process When to Request a Connection The client process should request a connection after socket is created and after server socket has a listening socket. Refer to the connect(2) man page for more information on connect. Chapter 6 135
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 136
    executed, the connection is established and data can be sent and received between the two socket endpoints. Because the stream socket descriptors correspond to HP-UX file descriptors, you can use the read and write calls (in addition to send and recv) to pass data through a socket-terminated channel
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 137
    send blocks until the specified number of bytes have been queued to be sent, unless you are using nonblocking I/O. When to Send Data The server or client process should send data after connection is established. Refer to the send(2) man page for more information on send. Receiving Data recv and
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 138
    received on the next recv. Flag Options There are no flag options for UNIX Domain (AF_UNIX) sockets. The only supported value for this field is 0. When to Receive Data The server or client process should receive data after connection is established. Refer to the recv(2) man page for more information
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 139
    same socket descriptor. The daemon process should do a close of the socket descriptor to avoid keeping the socket open once the server is through with it. Because the server performs the work, the daemon does not use the socket after the fork. close decrements the file descriptor reference count and
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 140
    UNIX Domain stream socket usage and are not Hewlett-Packard supported products. These programming examples demonstrate how to set up and af_unix directory. The client program is intended to run in conjunction with the server program. This example shows how to create UNIX Domain stream sockets and
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 141
    = atoi(*argv++); } else counter_pid = 0; /* * Set up the socket variables - address family, socket name. * They'll be used later to bind() the name to the server socket. */ sa.sun_family = AF_UNIX; strncpy(sa.sun_path, SOCKNAME, (sizeof(struct sockaddr_un) - sizeof(short))); /* * Create the
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 142
    data transfers */ if ((setsockopt(s, SOL_SOCKET, SO_RCVBUF, &bufsize, sizeof(bufsize))) == -1) { perror("catch - setsockopt failed"); exit(0); } /* * Bind the server socket to its name */ if ((bind(s, &sa, sizeof(struct sockaddr_un))) == -1) { perror("catch - bind failed"); exit(0); } /* * Call
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 143
    = bytes/msec; /* * Send back the bullet with throughput info, then close the * server socket */ if ((cc = send(ns, &bullet, sizeof(struct bullet), 0)) == -1) { perror("catch - send end bullet failed"); exit(0); } close(ns); } timeout() { printf( "alarm went off -- stopping the
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 144
    - SEND DATA TO THE CATCHER * * Pitch and catch set up a simple unix domain stream socket * client-server connection. The client (pitch) then sends * data to the server (catch), throughput is calculated, and * the result is printed to the client's stdout. */ #include #include
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 145
    0); setbuf(stderr, 0); if (argc < 2) { printf("usage: pitch Kbytes [pid]\n"); exit(0); } argv++; /* * Set up socket variables (address family; name of server socket) * (they'll be used later for the connect() call) */ sa.sun_family = AF_UNIX; strncpy(sa.sun_path, SOCKNAME, (sizeof(struct sockaddr_un
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 146
    Using UNIX Domain Stream Sockets Example Using UNIX Domain Stream Sockets if (gettimeofday(&tp1, &tzp) == -1) { perror("pitch time of day failed"); exit(0); } i = bytes; total = 0; /* * Send the data */ while (i > 0) { cc = sendsize < i ? sendsize : i; send_data(s, buffer, cc); i -= cc; total += cc;
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 147
    7 Using UNIX Domain Datagram Sockets This chapter describes communication between processes using UNIX Domain datagram sockets. 147
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 148
    involved in data transfer are not required to have a client-server relationship; the processes can be symmetrical. AF_UNIX datagram sockets allow you clients. This can all be done with one AF_UNIX datagram socket for the server. The simplest two-process model is used in this section to describe
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 149
    socket bind a socket send message receive message System Call Used Server Process Activity socket() bind() sendto() or sendmsg() create . The domain datagram sockets programming examples are at the end of these descriptive sections. You can refer to the example code as you work through the
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 150
    about the server process. Your server process needs to: • Declare socket address variables. • Get the pathname (character string) for the service you want character string) for the service you want to use. These activities are described next. Refer to the program example at the end of this chapter to
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 151
    process only needs one address for its socket. Any process that knows the address of the server process can then send messages to it. Thus, your client process needs to know the address of the server socket. The client process will not need an address for its own socket, unless other processes
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 152
    be used AF_UNIX SOCK_DGRAM 0 (default) Function result: socket number (HP-UX file descriptor) if successful, -1 if socket call fails. .h> ... s = socket(AF_UNIX, SOCK_DGRAM, 0) When to Create Sockets The server or client process should create sockets before any other BSD Sockets system calls.
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 153
    socket. Set up the address structure with a local address before the server makes a call to bind. The bind system call creates the these unused files, you can remove the files by calling unlink or remove them manually with the rm command. bind and its parameters are described in the following table.
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 154
    and Client Processes strcpy(servaddr.sun_path, SOCKET_PATH); unlink(SOCKET_PATH); bind(s, &servaddr, sizeof(struct sockaddr_un)); When to Bind Socket Addresses The server process should bind socket addresses after socket is created and before any other BSD Sockets system calls. Refer to the bind
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 155
    pointer to data buffer pointer to data to be sent size of data buffer size of msg settings for optional flags 0 (no options are currently supported) address of recipient socket pointer to the socket address that message should be sent to size of to length of address structure that to points
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 156
    struct sockaddr_un servaddr; ... count = sendto(s, argv[2], strlen(argv[2]), 0, &servaddr, sizeof(struct sockaddr_un); When to Send Data The server or client process should send data after server has bound to an address. Refer to the send(2) man page for more information on sendto and sendmsg
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 157
    to received data maximum number of bytes that should be received size of data buffer unchanged settings for 0 (no options are optional flags supported unchanged address of socket that sent message pointer to address structure, not used for input pointer to socket address of socket that sent
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 158
    Using UNIX Domain Datagram Sockets Sending and Receiving Messages message is in the queue, it is not affected. Therefore, the best technique is to receive as much as possible on each call. Refer to the recv(2) man page for more information on recvfrom and recvmsg. 158 Chapter 7
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 159
    Using UNIX Domain Datagram Sockets Closing a Socket Closing a Socket In most applications, you do not have to close the sockets. When you exit your program and your process terminates, the sockets are closed for you. If you need to close a socket while your program is still running, use the close
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 160
    of UNIX Domain datagram socket usage and are not Hewlett-Packard supported products. These programming examples demonstrate how to set up and this example the client process sends 2000 bytes of data to the server (five times). The server process can receive data from any other process and will echo
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 161
    sizeof(servaddr)); servaddr.sun_family = AF_UNIX; strcpy(servaddr.sun_path, SOCKET_PATH); if (bind(sock, &servaddr, sizeof(servaddr)) < 0) { close(sock); perror("server: bind"); exit(2); } /* Receive data from anyone, echo back data to the sender * Note that fromlen is passed as pointer so
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 162
    datagram socket for client */ if ((sock = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0) { perror("client: socket"); exit(2); } /* Client will bind to an address so the server will * get an address in its recvfrom call and use it to * send data back to the client. */ bzero(&clntaddr, sizeof(clntaddr
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 163
    Sockets Example Using UNIX Domain Datagram Sockets perror("client: bind"); exit(3); } /* Set up address structure for server socket */ bzero(&servaddr, sizeof(servaddr)); servaddr.sun_family = AF_UNIX; strcpy(servaddr.sun_path, SOCKET_PATH); for (j = 0; j < 5; j++) { sleep(1); slen = sendto
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 164
    Using UNIX Domain Datagram Sockets Example Using UNIX Domain Datagram Sockets 164 Chapter 7
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 165
    8 Programming Hints This chapter contains information for: • Troubleshooting. • Using diagnostic utilities as troubleshooting tools. 165
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 166
    Programming Hints • Adding a server process to the internet daemon. • Summary tables for system and library calls. • Portability issues. 166 Chapter 8
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 167
    Programming Hints Troubleshooting Troubleshooting The first step to take is to avoid many problems by using good programming the programs. It is possible that you could assign a reserved port address and cause a service to fail. For example, if the nftdaemon is not running, and you assign its port,
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 168
    destination node. netstat Use netstat to display sockets and associations to help you troubleshoot problems in your application programs. Use netstat to determine if your program has successfully created are described in detail in the Installing and Administering LAN/9000 manual. 168 Chapter 8
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 169
    .tcp example dgram udp wait root /server.udp server.udp where is the path to the files on your host. • Add the following lines to the /etc/services file: example 22375/tcp example 22375/udp • If inetd is already running, execute the following command so that inetd recognizes the changes
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 170
    server cause * an end-of-file condition to appear on this end of the * * bytes, signaling an end-of-file condition. This is * how the server will know that no more /* This sleep simulates the processing of * the request that a real server may do. */ sleep(1); /* Send a response back to the client
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 171
    ]; /* buffer for packets to be read into */ struct hostent *hp; /* pointer to info for requested host */ struct sockaddr_in clientaddr_in;/* for a host name for which the requester desires to know the internet address. The server will look up the name in its /etc/hosts file, and return the
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 172
    Programming Hints Adding a Server Process to the Internet Daemon * BUFFERSIZE - 1 bytes are read so that * room is left at the end of the buffer * for host into the * return buffer. /* reqaddr.s_addr = ((struct in_addr *)(hp->h_addr))->s_addr; } /* send the response back to the requesting client.
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 173
    Table 8-1 Programming Hints Summary Tables for System and Library Calls Summary Tables for System and Library Calls The following table contains a summary of the BSD Sockets system calls. BSD Sockets System Calls System Call Description socket bind listen connect accept send, sendto, sendmsg
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 174
    Table 8-2 Programming Hints Summary Tables for System and Library Calls System Call Description getsockname Gets the socket address of the specified socket. getsockopt, Gets, or sets, the options associated with a socket. setsockopt getpeername Gets the name of the peer socket connected to the
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 175
    Programming Hints Summary Tables for System and Library Calls System Call Description select ioctl fcntl Can be used to improve efficiency for a process that accesses multiple sockets or other I/O devices simultaneously. Refer to the sections on "Synchronous I/O Multiplexing with Select." Can be
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 176
    inet_makeaddr inet_netof inet_network internet address manipulation routines setservent endservent getservbyname getservbyport getservent get or set service entry setprotoent endprotoent getprotobyname getprotobynumber getprotoent get or set protocol entry setnetent endnetent getnetbyaddr
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 177
    IPC functions and library calls to watch out for if you want to port your IPC applications between HP-UX and 4.3 BSD systems. Shutdown When shutdown has been used on a datagram socket on an HP-UX system, the local port number bound to that socket remains unavailable for use until that socket
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 178
    be smaller than the backlog value. Pending Connections There is no guarantee which pending connection on a listening socket is returned by accept. HP-UX systems return the newest pending connection. Applications should be written such that they do not depend upon connections being returned by accept
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 179
    the utmp(4) man page for details. Library Equivalencies Certain commonly used library calls in 4.3 BSD are not present in HPUX systems, but they do have HP-UX equivalents. To make code porting easier, use the following equivalent library calls. You can do this by putting them in an include file, or
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 180
    Programming Hints Portability Issues 180 Chapter 8
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 181
    A BSD Sockets Quick Reference Table This appendix compares HP-UX BSD Sockets and X/Open Sockets. 181
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 182
    Quick Reference Table Release 10.10 of the HP-UX operating system supports two variations of sockets--HP-UX BSD Sockets and X/Open Sockets. To about HP- UX BSD sockets, see this manual as well as the man pages. For detailed information about X/Open sockets, see "CAE Specification, Networking Services,
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 183
    BSD Sockets Quick Reference Table Quick Reference Table HP-UX X/Open getpeername int getpeername (int socket, void *address, int *address_len); getsockname int getsockname (int socket, void *address, int *address_len); getsockopt int getsockopt (int socket,
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 184
    BSD Sockets Quick Reference Table Quick Reference Table HP-UX recvmsg int recvmsg (int socket, struct msghdr msg[], int flags); select int select (int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds, struct timeval *timeout);
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 185
    BSD Sockets Quick Reference Table Quick Reference Table HP-UX socketpair int socketpair (int af, int type, int protocol, int sv[2]); write(2) ssize_t write (int fildes, const void *buf, size_t nbyte); writev ssize_t writev (
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 186
    BSD Sockets Quick Reference Table Quick Reference Table 186 Appendix A
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 187
    fied in socket operations. The internet address family (AF_INET) is supported. Address: An Interprocess Communication term that refers to the means of research agency that was instrumental in developing and using the original ARPA Services on the ARPANET. Alias: A term used to refer to alternate
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 188
    at either end of the connection. See also, "Association." Daemon: A software process that runs continuously and provides services on request. to and received from many other datagram sockets. Datagram sockets do not support the concept of a connection. Messages could be lost or duplicated and
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 189
    Glossary services. Domain: A set of allowable names or values. See also, "Communication domain." Organization: Called "ISO," this organization created a network model that identifies the seven commonly-
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 190
    for managing the networking services on a specific node or host. Peer: An Interprocess Communication socket at the other end of a connection. A port number between 1 and 1023 that is only for super-user use. Server: A process or host that performs operations that local or remote client hosts request
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 191
    entities that are at either end of an Interprocess Communication the network to locate a socket. Socket descriptor: An HP-UX file descriptor accessed for reading, writing or any Protocol: A protocol that provides the underlying communication support for AF_INET stream sockets. TCP is used to
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 192
    address family. User Datagram Protocol: A protocol that provides the underlying communication support for datagram sockets. UDP is an unreliable protocol. A process receiving messages A protocol that provides terminal access to interactive services on remote hosts (e.g., telnet(1)). 192 Glossary
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 193
    48 address family, 17, 32, 89 addressing, 17 addressing domain, 31, 88, 126 AF_INET, 32 association, 17 binding, 18 binding a socket address to server processs", 36, 129 binding a socket to UNIX domain datagram sockets, 153 binding socket addresses to datagram sockets, 93 BSD IPC, 177, 178, 179 193
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 194
    of- band data, 81 sending data, 43, 136 sending messages, 95 server, 21 setting the server up to wait for connection, 37, 130 signal calls, 179 SIOCATMARK, 83 , 18 summary tables for system and library calls, 173 TCP, 19 troubleshooting, 167 UDP, 19 using a wildcard local address, 34, 91 using
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 195
    Index preparing address variables, 150 sockaddr_un, 150 ioctl, 77, 121, 175 IP multicasting, 116 IP_ADD_MEMBERSHIP, 119 IP_DROP_MEMBERSHIP, 119 IP_MAX_MEMBERSHIPS, 118 IP_MULTICAST_IF, 117 IP_MULTICAST_LOOP, 118 IP_MULTICAST_TTL, 117 IPC connections, 17, 29, 35, 124, 128 L library calls, 177, 179
  • HP Rp7410 | BSD Sockets Interface Programmer's Guide - Page 196
    Index SO_KEEPALIVE, 61, 66 SO_LINGER, 61, 68 SO_OOBINLINE, 61 SO_RCVBUF, 61, 67 SO_RCVLOWAT, 61 SO_RCVTIMEO, 61, 62 SO_REUSEADDR, 61, 66 SO_REUSEPORT, 61, 62 SO_SNDBUF, 61, 67 SO_SNDLOWAT, 61 SO_SNDTIMEO, 61, 62 SO_TYPE, 61 SO_USELOOPBACK, 61 socket, 29, 87, 124, 149 domain datagram sockets, 149
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196

BSD Sockets Interface Programmer’s
Guide
Edition 6
B2355-90136
HP 9000 Networking
E0497
Printed in: United States
© Copyright 1997 Hewlett-Packard Company.