HP Rp3440-4 BSD Sockets Interface Programmer's Guide

HP Rp3440-4 - 9000 - 0 MB RAM Manual

HP Rp3440-4 manual content summary:

  • HP Rp3440-4 | 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 Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 2
    to this manual, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose. Hewlett-Packard shall not be held liable for errors contained herein or direct, indirect, special, incidental or consequential damages in connection with the furnishing
  • HP Rp3440-4 | 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 Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 4
    4
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 5
    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 Writing the
  • HP Rp3440-4 | 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 Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 7
    88 Declaring Socket 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
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 8
    Creating a Socket 128 Binding a Socket Address to the Server Process's Socket 129 Setting the Server Up to Wait for Connection Requests 130 Accepting a Connection 131 Writing the Client Process 133 Creating a Socket 133 Requesting a Connection 134 Sending and Receiving Data 136 Sending Data
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 9
    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 Issues 177 Porting Issues for IPC Functions and Library
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 10
    Contents 10
  • HP Rp3440-4 | 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 Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 12
    12
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 13
    sockets from the networking services originally developed by the University of California at Berkeley (UCB). The BSD Sockets Interface Programmer's Guide is the primary reference for programmers who write or maintain BSD Sockets applications on HP 9000 computers. This guide also provides information
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 14
    the steps required to 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
  • HP Rp3440-4 | 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 Rp3440-4 | 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 Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 17
    family (AF_UNIX) are supported. A means of labeling a socket so that it is distinguishable from other sockets on a host. A BSD Sockets connection is defined by an association. An AF_INET association contains the (protocol, local address, local port, remote address, remote port)-tuple. An AF_UNIX
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 18
    supported. The UNIX Domain (AF_UNIX) address family domain is also supported, for local communication only. A four address. You can use port address values 1024 through 65535. (Port addresses 1 through 1023 . A pair of connected sockets provides an interface similar to that of HP-UX pipes. A socket
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 19
    BSD Sockets functions use socket descriptors as arguments. Provides the underlying communication support for stream sockets. The Transmission Control Protocol (TCP) is used to implement reliable, sequenced, flow-controlled two-way communication based on byte streams similar to pipes. Refer to the tcp
  • HP Rp3440-4 | 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 Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 21
    . 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 in each figure. Chapter
  • HP Rp3440-4 | 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 Rp3440-4 | 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 Rp3440-4 | 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 Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 25
    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 to specify any library name on the cc command line to use these library calls, libc.sl is used automatically. Chapter 1 25
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 26
    BSD Sockets Concepts BSD Sockets Library Routines 26 Chapter 1
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 27
    2 Using Internet Stream Sockets 27
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 28
    information about 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, Issue 4, X/Open" and the man pages. This chapter describes creating an internet stream socket connection using the
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 29
    lists the steps involved in creating and terminating a BSD Sockets 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
  • HP Rp3440-4 | 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 Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 31
    the correct variables and collect the information that you need to request a connection. Your server process needs to: • Declare socket address variables. • Assign a wildcard address. • Get the port address of the service that you want to provide. Your client process needs to: • Declare socket
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 32
    . Assign this field when you bind the port address for the socket or when you get a port address for 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 an address for its own socket. Your client
  • HP Rp3440-4 | 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 Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 34
    fic address is used in the bind, the server can only listen to that specific connection. Thus, INADDR_ANY is useful on a system in which multiple LAN cards are available, and messages for a given socket can come in on any of them. For example, to bind a specific port address to a socket, but leave the
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 35
    the calls your server process must make to connect with and serve a client process. Creating a Socket The server process must call socket to create a communication endpoint. socket and its parameters are described in the following table. Include files: System call: #include #include
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 36
    an address is bound to the server socket, other processes have no way to reference it. The server process must bind a specific port address to this socket, which is used for listening. Otherwise, a client process would not know what port to connect to for the desired service. Set up the address
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 37
    socket is created 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
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 38
    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 descriptor for the new socket. The new socket: • Is created with the same properties as the old socket. • Has the same bound port
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 39
    gethostbyaddr to get the hostname. You can close down the connection if you do not want the server process to communicate with that particular client host or port. When to Accept a Connection The server process should accept a connection after executing the listen call. Refer to the accept(2) man
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 40
    calls your client process must make to connect with and be served by a server process. Creating a Socket The client type protocol address family socket type underlying protocol to be used AF_INET SOCK_STREAM 0 (default) or value returned by getprotobyname Function result: socket number (HP
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 41
    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 table. Include files: System call: #include #include
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 42
    Process 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 can send data. Local internet and port addresses are bound when connect is executed if you have not already bound them yourself. These
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 43
    connect and accept calls are successfully 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 . Include files: System call: #include #include count = send(s,msg
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 44
    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 and its parameters are described in the following table. Include files: System call: #include
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 45
    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 should be received size of data buffer settings for optional flags 0, MSG_OOB or MSG_PEEK Function result: number of bytes
  • HP Rp3440-4 | 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 Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 47
    system call. For example, you may have a daemon process that uses fork to create the server process. The daemon process creates the BSD sockets connection and then passes the socket descriptor to the server. You then have more than one process with the same socket descriptor. The daemon process
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 48
    usage and are not Hewlett-Packard supported products. These program examples demonstrate porting to a host that requires it. • Uses the SO_LINGER option for a graceful disconnect. The client process creates a connection, sends requests to the server process and receives the results from the server
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 49
    number) in its * /etc/services file. * */ #include #include #include #include #include #include int s; int ls; /* connected socket descriptor */ /* listen socket descriptor */ struct hostent *hp; /* pointer to host info
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 50
    . Even when the * host is connected to only one network, this is good * practice, because it makes the server program more * portable. */ myaddr_in.sin_addr.s_addr = INADDR_ANY; /* Find the information for the "example" server * in order to get the needed port number. */ sp = getservbyname ("example
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 51
    /dev/null. * 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
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 52
    E R * * This is the actual server routine that the daemon forks * to handle each individual connection. Its purpose is * to receive the request } else { hostname = hp->h_name; /* point to host's name */ } /* Log a startup message. */ time (&timevar); /* The port number must be converted first
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 53
    end of the connection. After all of the * client's requests have been received, the next * recv call will return zero bytes, signalling an * end-of-file condition. This is is how the server terminated, because there are no * more requests to be serviced. As above, this * close will block until all of
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 54
    which the server will be running must also have the * same entry (same port number) in its /etc/services file. * */ #include #include #include #include #include int s; /* connected socket descriptor */ struct hostent *hp; struct
  • HP Rp3440-4 | 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\n", argv[0]); exit(1); } peeraddr_in
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 56
    for the user. */ time(&timevar); /* The port number must be converted first to * host byte the remote server. * In this case five are sent but any random number * could be used. The first four bytes of , shutdown the connection for further sends. * This causes the server to receive an end-of-file
  • HP Rp3440-4 | 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 Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 58
    Using Internet Stream Sockets Example Using Internet Stream Sockets 58 Chapter 2
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 59
    3 Advanced Topics for Stream Sockets 59
  • HP Rp3440-4 | 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 Rp3440-4 | 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 Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 62
    option is supported for compatibility only; it has no functionality. Options for protocol levels are described in the individual protocol manual pages, in the following table: Include files: System call: #include #include getsockopt(s, level, optname, optval, optlen) int
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 63
    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 OUTPUT Value unchanged unchanged unchanged pointer to
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 64
    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 / the following table: Include files: System call: #include #include setsockopt(s, level, optname, optval, optlen)
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 65
    value optlen length of optval INPUT Value supported option name Must be at least size port do not have the same remote address and remote port. Connect fails if the uniqueness requirement is violated. Example of the SO_REUSEADDR Option A network daemon server is listening on a specific port: port
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 66
    Server Listening at Port 2000. When the network daemon accepts a connection request, the accepted socket will bind to port periodic transmission of messages on a connected socket. This occurs at the connection is considered broken. The next time one of your processes attempts to use a connection
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 67
    receive socket buffer size. You can increase a stream socket's buffer size at any time, but decrease it only prior to establishing a connection. The maximum buffer size for stream sockets is 262144 bytes. Here is an example: int result; int buffsize = 10,000; result=setsockopt(s,SOL_SOCKET,SO_RCVBUF
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 68
    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 bytes SO_LINGER SO_LINGER controls the actions taken when a close is executed on a socket that
  • HP Rp3440-4 | 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 Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 70
    , 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 option allows multiple processes to share a port. All incoming multicast or broadcast UDP datagrams that are destined for the
  • HP Rp3440-4 | 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 Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 72
    it specifies a maximum interval to wait for the selection to complete. A select of a socket descriptor for reading is useful on: • A connected socket, because it determines against them). select is used in the same way as in other applications. Refer to the select(2) man page for information on how
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 73
    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/types.h contains some useful macros to be used with the select() system call, some of which are reproduced below. /* * These macros are used with select
  • HP Rp3440-4 | 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 Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 75
    true. It is generally accepted that connectionless protocols deliver the signal whenever a new packet arrives. For connection oriented protocols, the signal is also delivered when connections are established or broken, as well as when additional outgoing buffer space becomes available. Be warned
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 76
    This is typical of an application that needs to be notified when connection requests arrive. int ls; int flag = 1; int iohndlr(); /* 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,
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 77
    connection requests are present, accept returns immediately with the EWOULDBLOCK error. connect If the connection cannot be completed immediately, connect The O_NDELAY flag for fcntl(2) is also supported. If you use this flag and This is the same as returning an end-of-file condition. This is also
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 78
    descriptor are affected. shutdown causes all or part of a full-duplex connection on the specified socket to be disabled. When shutdown is 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
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 79
    Parameter Description of Contents s socket descriptor how number that indicates the type of shutdown INPUT Value socket descriptor of socket to be shut down that it is done sending, shut down the server or client process. Refer to the shutdown(2) man page for more information on shutdown.
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 80
    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 from the command. The command can be any command and does not have to know that stdout is a socket. It might use printf, which
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 81
    . 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 data arrive at the destination node in
  • HP Rp3440-4 | 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 Rp3440-4 | 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 Rp3440-4 | 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 Rp3440-4 | 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 Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 86
    Datagram sockets allow you to send and receive messages without establishing a connection. Each message includes a destination address. Processes involved in data transfer are not required to have a client-server relationship; the processes can be symmetrical. Unlike stream sockets, datagram sockets
  • HP Rp3440-4 | 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 Rp3440-4 | 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 Rp3440-4 | 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 Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 90
    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 client process needs to use a service that is offered by some server process, it must send a message to the
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 91
    , the host interprets the address as any valid address. This means that the server process can receive on a wildcard address and does not have to look up its own internet address. For example, to bind a specific port address to a socket, but leave the local internet address unspecified, the following
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 92
    the Server and Client Processes Writing the Server and Client Processes This section explains the calls your server and type protocol address family socket type underlying protocol to be used AF_INET SOCK_DGRAM 0 (default) or value returned by getprotobyname Function result: socket number (HP
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 93
    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 table. Include files: System call: #include #include #include bind (s,
  • HP Rp3440-4 | 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 Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 95
    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 #include count = sendto(s,msg,len,
  • HP Rp3440-4 | 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 Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 97
    Sockets: Using Internet Stream Sockets" chapter in this guide and in the recv(2) man page. recvfrom and following table. Include files: System call: #include #include #include count local socket pointer to data buffer maximum number of bytes that should be
  • HP Rp3440-4 | 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 Rp3440-4 | 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 Rp3440-4 | 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 Rp3440-4 | 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 name server. In order for * it to function, an entry for it needs to exist in the * /etc/services file. The port address for this service can be * any port number that
  • HP Rp3440-4 | 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 generally good practice for servers, because on * systems which are connected to more than one * network at once will be able to have one server * listening on all
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 103
    INADDR_ANY; /* Find the information for the "example" server * in order to get the needed port number. */ sp = getservbyname ("example", "udp"); 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.
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 104
    * If the host has been configured to use the NIS * server or name server (BIND), it is desirable * 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 Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 105
    port number) in its * /etc/services file. * * The "example" service is an example of a simple name server * application. The host that is to provide this service it does not recognize the host name. * */ #include #include #include #include
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 106
    requests service from * the remote "example server". given as the first parameter to the command. The second * parameter should be 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 Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 107
    .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\n", argv[0]); exit(1); } servaddr_in
  • HP Rp3440-4 | 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 Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 109
    5 Advanced Topics for Internet Datagram Sockets This chapter explains the following: 109
  • HP Rp3440-4 | 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 Rp3440-4 | 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 Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 112
    address, any message sent from that socket is automatically addressed to the peer process and only messages from that peer are delivered to the socket. connect may be called any number of times to change the associated destination address. This call does not behave the same as
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 113
    : System call: #include #include #include connect(s,addr,addrlen) int s; struct sockaddr 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
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 114
    descriptors is ready. If timout is a non-zero pointer, it specifies a maximum interval to wait for the selection to complete. select is useful for datagram socket executed against them). select is used in the same way as in other applications. Refer to the select(2) man page for information on how
  • HP Rp3440-4 | 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 Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 116
    Multicast Datagrams IP multicasting provides a mechanism for sending a single datagram to a group of systems. Normally, only 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
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 117
    are sent only to systems directly connected to the same network that the forwarded. If a multicast datagram is sent to a port from which an application on the local system is reading IP address of a local network interface that supports multicasting. Specifying the Scope of a Multicast
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 118
    . 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 port number to which multicast datagrams will be
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 119
    on a system, each application must set the SO_REUSEPORT socket option before binding to the port. In that case, every application will receive all multicast datagrams sent to the port number. Joining a Multicast Group IP_ADD_MEMBERSHIP In order to receive multicast datagrams, a system must join
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 120
    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 the same port number on a system, each application must set the SO_REUSEPORT socket option before binding to the
  • HP Rp3440-4 | 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 Rp3440-4 | 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 Rp3440-4 | 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 Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 124
    lists the steps involved in creating and terminating a UNIX Domain BSD Sockets connection 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
  • HP Rp3440-4 | 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 Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 126
    , establish the correct variables and collect the information that you need to request a connection. Your server process needs to: • Declare socket address variables. • Get the pathname (character string) for the service you want to provide. Your client process needs to: • Declare socket address
  • HP Rp3440-4 | 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 Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 128
    the calls your server process must make to connect with and serve a client process. Creating a Socket The server process must call socket to create a communication endpoint. socket and its parameters are described in the following table. Include files: System call: #include #include
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 129
    an address is bound to the server 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
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 130
    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 listen
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 131
    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 server's new socket is connected to pointer to the size of struct sockaddr_un pointer to the
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 132
    (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 server process should accept a connection after executing the listen call. Refer to the accept
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 133
    make to connect with and be served by a server process. Creating type, protocol) int af, type, protocol; Parameter Description of Contents INPUT Value af type protocol address family socket type underlying protocol to be used AF_UNIX SOCK_STREAM 0 (default) Function result: socket number (HP
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 134
    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 table. Include files: System call: #include #include
  • HP Rp3440-4 | 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 Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 136
    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 in the following table. Include files: System call: #include #include count = send(s,msg,len,flags) int s;
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 137
    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 its parameters are described in the following table. Include files: System call: #include
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 138
    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 on recv. 138 Chapter
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 139
    system call. For example, you may have a daemon process that uses fork to create the server process. The daemon process creates the BSD Sockets connection and then passes the socket descriptor to the server. You then have more than one process with the same socket descriptor. The daemon process
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 140
    not Hewlett-Packard supported products. These connection. The client (pitch) then sends * data to server (catch), throughput is calculated, and the * result is printed to the client's stdout. */ #include #include #include #include #include
  • HP Rp3440-4 | 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 Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 142
    -1) { perror("catch - setsockopt failed"); exit(0); } /* * Bind the server socket to its name */ if ((bind(s, &sa, sizeof(struct sockaddr_un))) - bind failed"); exit(0); } /* * Call listen() to enable reception of connection requests * (listen() will silently change given backlog 0, to be 1 instead)
  • HP Rp3440-4 | 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 Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 144
    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 #include #include #include #include #include
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 145
    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) - sizeof(short))); bullet.bytes
  • HP Rp3440-4 | 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 Rp3440-4 | 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 Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 148
    datagram sockets allow you to send and receive messages without establishing a connection. Each message includes a destination address. Processes involved in data transfer are not required to have a client-server relationship; the processes can be symmetrical. AF_UNIX datagram sockets allow you
  • HP Rp3440-4 | 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 Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 150
    the server process. Your server process needs to: • Declare socket address variables. • Get the pathname (character string) for the service you the end of this chapter to see how these activities work together. Declaring Socket Address Variables You need to declare a variable of type struct
  • HP Rp3440-4 | 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 Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 152
    SOCK_DGRAM 0 (default) Function result: socket number (HP-UX file descriptor) if successful, -1 if socket call fails. Example: #include #include ... s = socket(AF_UNIX, SOCK_DGRAM, 0) When to Create Sockets The server or client process should create sockets before any
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 153
    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. Include files: System call: #include #include #include bind(s, addr, addrlen); int s; struct sockaddr_un
  • HP Rp3440-4 | 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 Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 155
    described in the following table. Include files: System call: #include #include #include count = sendto(s, msg msg settings for optional flags 0 (no options are currently supported) address of recipient socket pointer to the socket address that message
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 156
    (struct sockaddr_un); When to Send Data The server or client process should send data after server has bound to an address. Refer to are described in the following table. Include files: System call: #include #include #include count = recvfrom(s, msg, len
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 157
    pointer to buffer buffer that is to receive data pointer 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
  • HP Rp3440-4 | 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 Rp3440-4 | 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 Rp3440-4 | 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 * program contains the server and is intended to operate in * conjunction with the client program. * */ #include #include
  • HP Rp3440-4 | 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 Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 162
    Example Using UNIX Domain Datagram Sockets * */ #include #include #include #include { 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
  • HP Rp3440-4 | 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 Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 164
    Using UNIX Domain Datagram Sockets Example Using UNIX Domain Datagram Sockets 164 Chapter 7
  • HP Rp3440-4 | 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 Rp3440-4 | 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 Rp3440-4 | 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 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, when you try to start the
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 168
    display sockets and associations to help you troubleshoot problems in your application programs. Use netstat to determine if your program has successfully created a connection. If you are using stream sockets are described in detail in the Installing and Administering LAN/9000 manual. 168 Chapter 8
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 169
    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 connections on the socket. /etc/inetd does these functions. * This server
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 170
    end-of-file condition to appear on this end of the * connection . After all the client's requests have * been received, the next recv call will return zero * bytes, signaling an end-of-file condition. This is * how the server the request that a real server may do. */ sleep(1);
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 171
    be received from the socket. * */ #include #include #include #include to be read into */ struct hostent *hp; /* pointer to info for requested host the requester desires to know the internet address. The server will look up the name in its /etc/hosts
  • HP Rp3440-4 | 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 Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 173
    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 recv, recvfrom, recvmsg shutdown Creates a socket, or communication endpoint for the calling process. Assigns a socket
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 174
    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 specified socket. The following table contains a summary of the other system calls that can be used with BSD Sockets. Other BSD
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 175
    Sockets. The library calls are in the common "c" library named libc.sl. Therefore, there is no need to specify any library name on the cc command line to use these library calls, libc.sl is used automatically. Chapter 8 175
  • HP Rp3440-4 | 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 Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 177
    in 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 has
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 178
    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 on a first-come, first-served basis. Porting Issues for Other Functions and Library Calls Typically
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 179
    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 by adding the
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 180
    Programming Hints Portability Issues 180 Chapter 8
  • HP Rp3440-4 | 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 Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 182
    of the HP-UX operating system supports two variations of sockets--HP-UX BSD 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 int close (int fildes); connect int connect (int socket, const struct
  • HP Rp3440-4 | 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 Rp3440-4 | 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 option_len); shutdown int shutdown (int s, int how); socket int socket (int af, int type, int protocol); X/Open recvmsg ssize_t recvmsg (int socket, struct msghdr *msg, int flags
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 185
    Sockets Quick Reference Table Quick Reference Table HP-UX socketpair int socketpair (int af, int type, int protocol, int sv[2]); write(2) iovec *iov, int iovcnt); X/Open socketpair int socketpair (int domain, int type, int protocol, int socketvector[2]); write(2) ssize_t write (int fildes, const
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 186
    BSD Sockets Quick Reference Table Quick Reference Table 186 Appendix A
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 187
    address family (AF_INET) is supported. Address: An Interprocess Communication developing and using the original ARPA Services on the ARPANET. Alias: A connection (e.g., a socket) is defined by an association. An association contains the (protocol, local address, local port, remote address, remote port
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 188
    the Internet domain is supported. Connection: A communications path to send and receive data. A connection is uniquely identified by the pair of sockets at either end of the connection. See also, "Association." Daemon: A software process that runs continuously and provides services on request. DARPA
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 189
    services used in ARPA networks. The ftp command uses the FTP protocol. Forwarding: The the Network Information Center. Internet address: A four-byte quantity that is distinct from a link See File Transfer Protocol. Gateway: A node that connects two or more networks together and routes packets between
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 190
    the networking services on a specific node or host. Peer: An Interprocess Communication socket at the other end of a connection. Port: An the network or via a gateway. Reserved port: A port number between 1 and 1023 that is only for super-user use. Server: A process or host that performs operations
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 191
    entities that are at either end of an Interprocess Communication connection. A socket is identified by a socket descriptor. A program can write data to and read data from a socket, just as it writes and reads data to and from files. Socket address: The internet address, port address and address family
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 192
    . User Datagram Protocol: A protocol that provides the underlying communication support for datagram sockets. UDP is an unreliable protocol. A process a connection between the communicating sockets. Virtual Terminal Protocol: A protocol that provides terminal access to interactive services on
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 193
    176 inetd, 169 Internet address family, 18, 19 sockets, 28 Internet address, 18 Internet daemon, 169 Interprocess Communication accepting a connection, 38, 131 adding server process to the Internet daemon, 169 address conversion, 177 address conversion call, 48 address family, 17, 32, 89 addressing
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 194
    , 18 pathname, 126 peer, 18 pending connections, 178 port, 18 port address, 31, 88 portability issues, 177 preparing 136 sending messages, 95 server, 21 setting the server up to wait for connection, 37, 130 signal calls and library calls, 173 TCP, 19 troubleshooting, 167 UDP, 19 using a wildcard
  • HP Rp3440-4 | BSD Sockets Interface Programmer's Guide - Page 195
    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 data, 81 P packet, 18 pathname, 129, 153 peer, 18 perror, 167 ping, 168 port, 18 port address, 31, 33, 36, 88, 93 pty, 178 R read, 121 domain stream
  • HP Rp3440-4 | 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.