*BSD News Article 30762


Return to BSD News archive

Xref: sserve comp.unix.bsd:14015 comp.lang.c:70378
Newsgroups: comp.unix.bsd,comp.lang.c
Path: sserve!newshost.anu.edu.au!harbinger.cc.monash.edu.au!yeshua.marcam.com!news.kei.com!world!clintp
From: clintp@world.std.com (Clinton A Pierce)
Subject: Problem coding Sockets
Message-ID: <Cq9op0.3E2@world.std.com>
Followup-To: poster
Summary: Sorry about the crosspost--still no answers
Organization: The World Public Access UNIX, Brookline, MA
Date: Mon, 23 May 1994 18:00:35 GMT
Lines: 143

Ok, I'm tinkering with sockets on serveral AIX RS/600's and I have a short demo
program (enclosed).  I have one machine that will NOT run this code.  When
the client tries to connect(), he gets "Permission denied".   What permission?

Code edited for brevity:
The server's code

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <string.h>
#include <netdb.h>
#include <stdio.h>

extern int errno;

main(argc, argv)
int argc;
char **argv;
{
	char c;
	int sock;
	FILE *fp, *tempfile;
	int fromlen;
	char hostname[64], xms[123];
	struct hostent *hp;
	int i, s, ns;
	struct sockaddr_in sin, fsin;

	if (argc!=3) {
		fprintf(stderr, "Usage server socket filename");
	}
	tempfile=fopen(argv[2], "r");
	if (tempfile==NULL) exit(2);
	fclose (tempfile);
	sock=atoi(argv[1]);
	if (sock==0) exit(2);

	gethostname(hostname, sizeof(hostname));
	
	if ((hp=gethostbyname(hostname))==NULL) {
		fprintf(stderr, "%s: host unknown.\n", hostname);
		exit(1);
	}

	if ((s=socket(AF_INET, SOCK_STREAM, 0))<0) {
		perror("server:socket");
		exit(1);
	}

	sin.sin_family=AF_INET;
	sin.sin_port=htons(sock);
	bcopy(hp->h_addr, &sin.sin_addr, hp->h_length);

	if (bind(s, &sin, sizeof(sin)) < 0) {
		perror("server:bind");
		exit(1);
	}

	if (listen(s, 5)<0) {
		perror("server:listen");
		exit(1);
	}

	if ((ns=accept(s, &fsin, &fromlen))<0) {
		perror("server:accept");
		exit(1);
	}
	
	fp=fdopen(ns, "r");

[Useful portion of code removed]

	fclose(tempfile);
	close(s);
	exit(0);
}
	

And the client:

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>

extern int errno;

main(argc, argv)
int argc;
char **argv;
{
	char c;
	int sock;
	FILE *fp;
	int fromlen;
	char hostname[64];
	struct hostent *hp;
	int i, s;
	struct sockaddr_in sin;

	sock=atoi(argv[1]);
	if (sock==0) exit(2);

	gethostname(hostname, sizeof(hostname));
	
	if ((hp=gethostbyname(hostname))==NULL) {
		fprintf(stderr, "%s: host unknown.\n", hostname);
		exit(1);
	}

	if ((s=socket(AF_INET, SOCK_STREAM, 0))<0) {
		perror("client:socket");
		exit(1);
	}

	sin.sin_family=AF_INET;
	sin.sin_port=htons(sock);
	bcopy(hp->h_addr, &sin.sin_addr, hp->h_length);

	if (connect(s, &sin, sizeof(sin)) < 0) {
		perror("client:connect");
		exit(1);
	}

[useful section of code deleted]

	
	close(s);
	exit(0);
}
	

Can anyone help with this?   This should be so straightforward!  And why only 
one system does this?  (I'm NOT running this over a network--yet.  It's still
only local.)

Help!