Agilent Technologies 89400 Series Command Reference Manual page 487

Table of Contents

Advertisement

/****************************************************************************
*
> $Function: openSocket$
*
* $Description:
open a TCP/IP socket connection to the instrument $
*
* $Parameters:
$
*
(const char *) hostname . . . . Network name of instrument.
*
*
(int) portNumber
*
* $Return:
(int)
*
* $Errors:
returns -1 if anything goes wrong $
*
****************************************************************************/
int openSocket(const char *hostname, int portNumber)
{
struct hostent *hostPtr;
struct sockaddr_in peeraddr_in;
int s;
memset(&peeraddr_in, 0, sizeof(struct sockaddr_in));
/***********************************************/
/* map the desired host name to internal form. */
/***********************************************/
hostPtr = gethostbyname(hostname);
if (hostPtr == NULL)
{
fprintf(stderr,"unable to resolve hostname '%s'\n", hostname);
return -1;
}
/*******************/
/* create a socket */
/*******************/
s = socket(AF_INET, SOCK_STREAM, 0);
if (s == -1)
{
fprintf(stderr,"unable to create socket to '%s': %s\n",
hostname, strerror(errno));
return -1;
}
memcpy(&peeraddr_in.sin_addr.s_addr, hostPtr->h_addr, hostPtr->h_length);
peeraddr_in.sin_family = AF_INET;
peeraddr_in.sin_port = htons(portNumber);
if (connect(s, (char*) &peeraddr_in, sizeof(struct sockaddr_in)) == -1)
{
fprintf(stderr,"unable to create socket to '%s': %s\n",
hostname, strerror(errno));
return -1;
}
return s;
}
This can be in dotted decimal notation.
. . . . . . . The TCP/IP port to talk to.
. . . . . . . . . A file descriptor similar to open(1).$
Appendix B
Sockets Example Program
477

Advertisement

Table of Contents
loading

This manual is also suitable for:

89400-90039

Table of Contents