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

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

Advertisement

Using Internet Datagram Sockets
Example Using Datagram Sockets
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <netdb.h>
int s;
#define BUFFERSIZE
int cc;
char buffer[BUFFERSIZE];
struct hostent *hp;
struct servent *sp;
struct sockaddr_in myaddr_in;
struct sockaddr_in clientaddr_in;/* for client's socket address */
struct in_addr reqaddr;
#define ADDRNOTFOUND 0xffffffff /* return address for unfound host */
/*
*
*
* 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 socket, and for each incoming
* request, it returns an answer.
* host name for which the requester desires to know the
* internet address.
* /etc/hosts file, and return the internet address to the
* client. An internet address value of all ones will be returned
* if the host name is not found. NOTE: This example is valid
* only if the /etc/hosts file is being used to lookup host names.
*
*/
main(argc, argv)
int argc;
char *argv[];
{
int addrlen;
/* clear out address structures */
memset ((char *)&myaddr_in, 0, sizeof(struct sockaddr_in));
memset ((char *)&clientaddr_in, 0, sizeof(struct sockaddr_in));
/* Set up address structure for the socket. */
myaddr_in.sin_family = AF_INET;
/* The server should receive 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
* network at once will be able to have one server
* listening on all networks at once.
* host is connected to only one network, this is good
* practice, because it makes the server program more
* portable.
102
/* socket descriptor */
1024
/* max size of packets to be received */
/* contains the number of bytes read */
/* buffer for packets to be read into */
/* pointer to info of requested host */
/* pointer to service information */
/* for local socket address */
/* for requested host's address */
M A I N
It forks, leaving the child
Each request consists of a
The server will look up the name in its
This is
Even when the
Chapter 4

Advertisement

Table of Contents
loading

Table of Contents