TCP/IP Programming Manual
5 Sample Programs
This section provides TCP/IP program examples for AF_INET sockets and AF_INET6 sockets.
Programs Using AF_INET Sockets
This subsection contains a client and server program that use AF_INET sockets.
AF_INET Client Stub Routine
The first example shows a sample client program that you can build, compile, and run on your
system. The program sends a request to and receives a response from the system specified on the
command line.
/*
 * AF_INET Client Stub Routine
 * *****************************************************************
 * * *
 * * Copyright (c) Hewlett-Packard Company, 2003 *
 * * *
 * * The software contained on this media is proprietary to *
 * * and embodies the confidential technology of Hewlett *
 * * Packard Corporation. Possession, use, duplication or *
 * * dissemination of the software and media is authorized only *
 * * pursuant to a valid written license from Hewlett Packard *
 * * Corporation. *
 * * *
 * * RESTRICTED RIGHTS LEGEND Use, duplication, or disclosure *
 * * by the U.S. Government is subject to restrictions as set *
 * * forth in Subparagraph (c)(1)(ii) of DFARS 252.227-7013, *
 * * or in FAR 52.227-19, as applicable. *
 * * *
 * *****************************************************************
 */
#include <systype.h>
#include <socket.h>
#include <errno.h>
#include <in.h>
#include <netdb.h>
#include <string.h>
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <inet.h>
#include <cextdecs(FILE_CLOSE_)>
#define SERVER_PORT 7639
#define CLIENT_PORT 7739
#define MAXBUFSIZE 4096
int main (int argc, char **argv )
{
 int s;
 int error;
 char databuf[MAXBUFSIZE];
 int dcount;
 const char *ap;
 struct hostent *hp;
 char *server;
 /* Declare sockaddr_in structures for IPv4 use.*/
 struct sockaddr_in serveraddr;
208 Sample Programs










