AF_INET sockets, arghh/

Jordan K. Hubbard jordan at greipa.UUCP
Tue Jun 18 10:49:45 AEST 1985


Um, I'm going to apologize in advance for posting hideous test code to the
net, but I very curious why it doesn't work.  I'm trying to test sockets
between machines (in preparation for writing an application) and having a bit
of trouble. For one, it seems strange that I can only bind the socket if I'm
the super user. Maybe not so weird since most services would be root/daemon
privileged and you wouldn't want just anyone binding to their addresses
(though if they'd already bound to it at system startup, what would it matter?)

The second REAL problem is that when my server accepts a connection, a socket
with the value of 0 is returned!

A third problem is that when I install the server on another machine (listening
at the same address) the connection is 'refused' outright.

Here are the salient parts of my client/server code.

Any suggestions, flames of 'That's Gross you idiot!', fixes
or general comments are appreciated (well, maybe not ALL of them).. I
would also appreciate any code fragments using internet sockets as I have
very few examples to go on. Anyway, on to the gross stuff.

Most declarations have been stripped, assume missing variables are ints.

-- client code --

#include <sys/types.h>
#include <sys/socket.h>

#include <netinet/in.h>      
#include <netdb.h>

#include <stdio.h>

SendToHost(host, cmd)
char *host, *cmd;
{
	struct	hostent *him;	/* host table entry */
	struct	servent *sir;	/* sevice file entry */
	struct	sockaddr_in sin;/* socket address */
	int	fd;					/* network file descriptor */
	char buff[81];			/* server buffer */

	if ((him = gethostbyname(host)) == NULL) {
		fprintf(stderr, "client: Unknown host %s\n", host);
		return(-1);
	}

	if ((sir = getservbyname("sir","tcp")) == NULL) {
		fprintf(stderr, "client: sir/tcp: unknown service\n");
		return(-1);
	}

	if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
		perror("client");
		return(-1);
	}

	sin.sin_family = him->h_addrtype;      
	bcopy(him->h_addr, (caddr_t)&sin.sin_addr, him->h_length);
	sin.sin_port = sir->s_port;

	if (connect(fd, (caddr_t)&sin, sizeof(sin), 0) < 0) {
		perror("client");
		close(fd);
		return(-1);
	}

	/* Send the remote command */
	write(fd, cmd, strlen(cmd)); 
	printf("[%s: %s]\t", him->h_name, cmd);

	/* read in the response */
		while (read(fd, buff, 80) > 0)
			printf("Server returns: %s\n", buff);
		close(fd);
}


-- server code --


#include <sys/types.h>
#include <sys/socket.h>

#include <netinet/in.h>      
#include <netdb.h>

#include <stdio.h>

HostServer()
{
	struct	hostent *him;	/* host table entry */
	struct	servent *sir;	/* sevice file entry */
	struct	sockaddr_in sin;/* socket address */
	int	fd, grinding = 1;	/* network file descriptor */
	int nf,	sz;				/* new fd and size of sin */
	char buff[36], buff2[132];			/* server buffer */

	if ((sir = getservbyname("sir","tcp")) == NULL) {
		fprintf(stderr, "server: sir/tcp: unknown service\n");
		return(-1);
	}

	if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
		perror("client/sock");
		return(-1);
	}

    sin.sin_family = AF_INET;
    sin.sin_port = sir->s_port;
	sz = sizeof(sin);
	if (bind(fd, (caddr_t)&sin, sz) < 0) {
		perror("server/bind");
		return(-1);
	}

	if (listen(fd, 5) < 0) {
		perror("server/listen");
		return(-1);
	}
	/* deal with requests */
	while (grinding) {
		/*  *** HERE'S WHERE THE BUGGER FLOPS *** */
		if (nf = accept(fd, (caddr_t)&sin, &sz) < 0) {
			perror("server/accept");
			return(-1);
		}

printf("Server: got connect, fd = %d, nf = %d\n", fd, nf);
		while (read(nf, buff, 36) != 1) {
			if (!strcmp(buff, "stop")) {
				grinding = 0;
				break;
			}
			sprintf(buff2, "I got a %s command\n", buff);
			write(nf, buff2, 80);
		}
		close(nf);
	}
}
-- 
			Jordan K. Hubbard
			@ Genstar Rental Electronics.
			Palo Alto, CA.
			{pesnta, decwrl, dual, pyramid}!greipa!jordan

			"ack pfffft. gag. retch. barf.. ack"

				- Bill again.



More information about the Comp.unix.wizards mailing list