Actual source code: closeport.c

  1: /* This was part of the MatlabSockettool package. 
  2:  
  3:         Written by Barry Smith, bsmith@mcs.anl.gov 4/14/92
  4:          Updated by Ridhard Katz, katz@ldeo.columbia.edu 9/28/03
  5: */

 7:  #include petscsys.h
 8:  #include src/sys/viewer/impls/socket/socket.h

 10: #include <errno.h>
 11: #if defined(PETSC_HAVE_STDLIB_H)
 12: #include <stdlib.h>
 13: #endif
 14: #include <sys/types.h>
 15: #include <ctype.h>
 16: #if defined(PETSC_HAVE_MACHINE_ENDIAN_H)
 17: #include <machine/endian.h>
 18: #endif
 19: #if defined(PETSC_HAVE_UNISTD_H)
 20: #include <unistd.h>
 21: #endif
 22: #if defined(PETSC_HAVE_SYS_SOCKET_H)
 23: #include <sys/socket.h>
 24: #endif
 25: #if defined(PETSC_HAVE_SYS_WAIT_H)
 26: #include <sys/wait.h>
 27: #endif
 28: #if defined(PETSC_HAVE_NETINET_IN_H)
 29: #include <netinet/in.h>
 30: #endif
 31: #if defined(PETSC_HAVE_NETDB_H)
 32: #include <netdb.h>
 33: #endif
 34: #if defined(PETSC_HAVE_FCNTL_H)
 35: #include <fcntl.h>
 36: #endif
 37: #if defined(PETSC_HAVE_STROPTS_H)
 38: #include <stropts.h>
 39: #endif
 40: #if defined(PETSC_HAVE_IO_H)
 41: #include <io.h>
 42: #endif

 45: #if defined(PETSC_NEED_CLOSE_PROTO)
 47: #endif

 50: #include "mex.h"
 51: #define PETSC_MEX_ERROR(a) {fprintf(stdout,"CLOSEPORT: %s \n",a); return ;}
 52: typedef struct { int onoff; int time; } Linger;
 53: /*-----------------------------------------------------------------*/
 54: /*                                                                 */
 55: /*-----------------------------------------------------------------*/
 58: void mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[])
 59: {
 60:   int    t = 0;
 61:   Linger linger;

 63:   linger.onoff = 1;
 64:   linger.time  = 0;

 66:   if (!nrhs) PETSC_MEX_ERROR("Needs one argument, the port");
 67:   t = (int)*mxGetPr(prhs[0]);

 69:   if (setsockopt(t,SOL_SOCKET,SO_LINGER,(char*)&linger,sizeof(Linger)))
 70:     PETSC_MEX_ERROR("Setting linger");
 71:   if (close(t)) PETSC_MEX_ERROR("closing socket");
 72:   return;
 73: }