Home| base |src.lib|tcpip|cnx| ConnexRead Index

ConnexRead

Syntax
int ConnexRead(int num,int *sock,unsigned char **buffer,int *size,int *flag, struct timeval *tmout);
Header
base/connex.h
Library
cnx
Description

The ConnexRead function reads formatted data packets from one or more TCP/IP socket stream connections.

The number of open streams to process is given by the argument num. The socket file descriptors are given by the array pointed to by the argument sock.

If a data packet can be read from a socket then memory is allocated to store the decoded data. The pointer to the memory buffer is stored in the appropriate element of the array pointed to by the argument buffer. The number of bytes of data read is stored in the appropriate element of the array pointed to by the argument size. The argument flag points to an array that stores a status flag for each stream; if data was received then the flag is set to (1), if no data was received then the flag is set to zero, if an error occurred then the flag is set to (-1).

The final argument tmout is a pointer to a timeout structure that indicates how long the function should block until control is returned. If this argument is set to NULL then the function will block indefinitely until data is received on one of the connections.

The memory buffers allocated by the function should be freed when they are no longer required.

Returns
Returns the number of sockets that data has been read from on success. On error, (-1) is returned.
Errors
On error, (-1) is returned.
Example

Source Code: ConnexRead.c

/* ConnexRead.c
   ============
   Author: R.J.Barnes
 Copyright (c) 2012 The Johns Hopkins University/Applied Physics Laboratory

This file is part of the Radar Software Toolkit (RST).

RST is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.

Modifications:



#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include "connex.h"

int main(int argc,char *argv[]) {
  int n,num;
  int sock[10];
  int port[10];
  char *host[10];
  int flag[10],status,size[10];
  unsigned char *buffer[10];
  struct timeval tout;
 
  
  if (argc<3) { 
    fprintf(stderr,"host and port must be specified.'n");
    exit(-1);
  }

  num=0;
  for (n=1;n<argc;n+=2) {
    host[num]=argv[n];
    port[num]=atoi(argv[n+1]);
    flag[num]=0;
    size[num]=0;
    buffer[num]=0;
    num++;
  } 

  for (n=0;n<num;n++) {
    sock[n]=ConnexOpen(host[n],port[n]); 
    if (sock[n]<0) fprintf(stderr,"Could not connect to host.'n");
  }

  do {
   tout.tv_sec = 5;
   tout.tv_usec = 0;
   status=ConnexRead(num,sock,buffer,size,flag,&tout);
   if (status==-1) break;
   for (n=0;n<num;n++) 
     if (flag[n]==1) fprintf(stderr,"Server %s:%d Size:%d'n",
                            host[n],port[n],size[n]);     
  } while(1);
  fprintf(stderr,"Connection failed.'n");
  return 0;

}