prefect
Home Engineering Publications
DACS: TCP/IP Client Library Code, client.c
 
  next up previous contents
Next: TCP/IP Server Library Header, Up: MIDI Controller Previous: TCP/IP Client Library Header,   Contents

TCP/IP Client Library Code, client.c

The following code provides easy access to Berkeley TCP/IP sockets in a Unix (Linux) environment.
/*****************************************************************************
 * DACS : Distributed Audio Control System
 *============================================================================
 *         File: client.c
 *  Description: TCP/IP client routines
 *       Author: Stephen S. Richardson
 * Date Created: 04.23.95
 *  Environment: GNU C Compiler (GCC) v2.7.1, Linux i486 v2.0.28
 *        Build: library
 *============================================================================
 * The code, executables, documentation, firmware images, and all related
 * material of DACS are  
 * Copyright (C) 1997 Stephen S. Richardson - ALL RIGHTS RESERVED
 *****************************************************************************
 * Source code control:
 *
 * $Id: client.c,v 1.1 1997/07/25 12:15:44 prefect Exp prefect $
 *
 *****************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <unistd.h>
#include <netdb.h>
#include <time.h>
#include <sys/time.h>
#include "client.h"

/*****************************************************************************
 attempts to connect to an ip and tcp port, returning an fd of the socket

 returns the file descriptor of the socket or

 ERROR_BADHOST - hostname or ip specified is bogus
 ERROR_SOCKET - the 'socket' command failed
 ERROR_CONNECT - the 'connect' command failed
******************************************************************************/

int tcpEstablishConn (char *hostname, int port)
{
  struct sockaddr_in bba;
  struct hostent *hp;
  int s;

  memset (&bba, 0 , sizeof (bba));
  bba.sin_family = AF_INET;
  hp = gethostbyname (hostname);
  if (hp == NULL) return (ERROR_BADHOST);
  memcpy (&bba.sin_addr, hp->h_addr, hp->h_length);
  bba.sin_port = htons ((unsigned short int) port);

  s = socket (AF_INET,SOCK_STREAM,0);
  if (s == -1) return (ERROR_SOCKET);
  if (connect (s,(struct sockaddr *) &bba, sizeof (bba)) !=0)
    return (ERROR_CONNECT);

  return s;
}



/*****************************************************************************
 kills (disconnects) a socket

 returns SUCCESS if it worked or

 ERROR_KILL if it couldn't be disconnected
******************************************************************************/

int tcpKillConn (int fd)
{
  int retcode;

  retcode = close (fd);
  if (retcode==-1) return (ERROR_KILL);
  else return (SUCCESS);
}



/*****************************************************************************
 writes out buffer of size bufsize to an open file descriptor (a socket)

 returns number of bytes written or

 ERROR_WRITEFAILED - write failed
******************************************************************************/

int tcpWriteBuffer (int fd, char *buffer, size_t bufsize)
{
  int retcode;
  
  retcode=write (fd, buffer, bufsize);

  if (retcode==-1) {
    return (ERROR_WRITEFAILED);
  }
  else return (retcode);
}


/*****************************************************************************
 synchronous I/O multiplexer - detects if there's stuff waiting using select
 doesn't disturb the file descriptor set you send

 returns DATA if there's data, or NODATA if there's none

******************************************************************************/

int tcpDataWaiting (int sfd)
{
  fd_set fds;
  struct timeval tv;

  bzero (&tv, sizeof (struct timeval));
  tv.tv_usec = 1;

  FD_ZERO (&fds);
  FD_SET (sfd, &fds);
  while ((select (sfd+1, &fds, NULL, NULL, &tv))==-1); /* make sure select */
                                                       /* works            */

  if (FD_ISSET (sfd, &fds)) return (DATA);
  else return (NODATA);
}



/******************************************************************************
 reads bytes from a socket file descriptor, putting them into the buffer

 returns number of bytes read, or

 ERROR_HANGUP if the socket has been disconnected

******************************************************************************/

int tcpReadBuffer (int fd, char *buffer, int bufsize)
{
  int count;

  count=read (fd, buffer, bufsize);
  if (count==0) {
    close (fd);
    return (ERROR_HANGUP);
  } else if (count==-1) {
    return (ERROR_READ);
  }
  else return (count);
}


Steve Richardson 2000-07-06
Table of Contents

[PDF] [Whole document in PDF 1.9MB]

[more photos and information]

 
Page last modified:
Copyright © 1993-2000 prefect - All Rights Reserved.