HP Rp3440-4 - 9000 - 0 MB RAM Programmer's Manual page 49

Bsd sockets interface programmer’s guide
Hide thumbs Also See for Rp3440-4 - 9000 - 0 MB RAM:
Table of Contents

Advertisement

* demonstrate many of the features of sockets, as well as good
* conventions for using these features.
*
* This program provides a service called "example". In order for
* it to function, an entry for it needs to exist in the
* /etc/services file.
* any port number that is likely to be unused, such as 22375,
* for example.
The host on which the client will be running
* must also have the same entry (same port number) in its
* /etc/services file.
*
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <signal.h>
#include <stdio.h>
#include <netdb.h>
int s;
int ls;
struct hostent *hp;
struct servent *sp;
long timevar;
char *ctime();
struct linger linger = {1,1};
struct sockaddr_in myaddr_in;
struct sockaddr_in peeraddr_in;/* for peer socket address */
/*
*
*
* This routine starts the server.
* to do all the work, so it does not have to be run in the
* background.It sets up the listen socket, and for each incoming
* connection, it forks a child process to process the data.
* It will loop forever, until killed by a signal.
*/
main(argc, argv)
int argc;
char *argv[];
{
int addrlen;
/* clear out address structures */
memset ((char *)&myaddr_in, 0, sizeof(struct sockaddr_in));
memset ((char *)&peeraddr_in, 0, sizeof(struct sockaddr_in));
/* Set up address structure for the listen socket. */
myaddr_in.sin_family = AF_INET;
/* The server should listen on the wildcard address,
* rather than its own internet address.
* generally good practice for servers, because on
* systems which are connected to more than one
Chapter 2
Example Using Internet Stream Sockets
The port address for this service can be
/* connected socket descriptor */
/* listen socket descriptor */
/* pointer to host info for remote host */
/* pointer to service information */
/* contains time returned by time() */
/* declare time formatting routine */
/* allow lingering, graceful close; */
/* used when setting SO_LINGER */
/* for local socket address */
M A I N
It forks, leaving the child
Using Internet Stream Sockets
This is
49

Advertisement

Table of Contents
loading

Table of Contents