abmg@cathedral.cerc.wvu.wvnet.edu (Aliasghar Babadi) (06/01/91)
Hi,
I am having problem with the foloowing code. This code is
to connect to the X server. it works fine on sun and sgi but it
kills the system on DEC5000/200 Ultrix. The command line arguments are
machine_name and number of connections requested. Can somebody see what
the problem is. Thanks in advance.
/*
* this program is to connect to the X server running on a
* given machine. We can specify to which machine to connect and how
* many times to connect.
*/
*
/*
* 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]);
n = atoi(argv[2]);
/*
* 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);
}
}