[comp.unix.wizards] question about using sockets on sysVr3

pspinler@att1.mankato.msus.edu (Patrick J. Spinler) (07/11/90)

I am attempting to learn to use sockets, however, I am evidently
creating a protocal error of some sort, and have no real idea where to
start looking.  If some kind soul would please tell me why I'm getting
an EINVAL error on my call to bind(), I would be very grateful.  I'm
running sysVr3.1.1 on an at&t 3b2/500 with the WINS tcp/ip and
wollengang socket emulation library installed.

Please email replys, and thanks very much !

-- Pat

--- cut here ---

#include <errno.h>       /* obligatory includes */
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/inet.h>
#include <sys/in.h>
#include <sys/time.h>
#include <netdb.h>
#include <string.h>

#define MAXHOSTNAME	32
#define PORTNUM 	4594

main ()
{
    int    sock_fds, pid, fds, size;
    struct sockaddr_in	sock_add;
    struct sockaddr_in	connect_add;
    struct hostent	*host;
    short  portnum;
    char   hostname[MAXHOSTNAME+1], buffer[80];

    bzero (&sock_add, sizeof (sock_add));

    gethostname (hostname, MAXHOSTNAME);
    host = gethostbyname (hostname);
    if (!host) {
	perror ("getbyhostname");
	exit (1);
    }

    if (host->h_addrtype != AF_INET) {
	perror ("host in wrong address family");
	exit (1);
    }

    /*
     * create the socket !
     */

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

    pid = fork ();

    if (pid < 0) {
	perror ("fork");
	close (sock_fds);
	exit (1);
    }

    if (pid) {	/* Parent process -- server */

        /* 
         * Name the socket
         */

	bzero (&sock_add, sizeof (sock_add));
	sock_add.sin_family = AF_INET;
	bcopy (host->h_addr, &sock_add.sin_addr, host->h_length);
	sock_add.sin_port = htons ((unsigned short) PORTNUM);	

	if (bind (sock_fds, &sock_add, sizeof (sock_add))) {
	    perror ("bind");
	    close (sock_fds);
	    exit (1);
	}

	size = sizeof (sock_add);
	if (getsockname (sock_fds, &sock_add, &size)) {
	    perror ("getsockname");
	    close (sock_fds);
	    exit (1);
	}
	sprintf (buffer, "Port #%d\n", ntohs (sock_add.sin_port));
	write (1, buffer, strlen (buffer));

	listen (sock_fds, 1);
	size = sizeof (connect_add);
	fds = accept (sock_fds, &connect_add, &size);
	if (fds < 0) {
	    perror ("accept");
	    close (sock_fds);
	    exit (1);
	}

	while ((size = read (fds, buffer, 79))) {
	    write (1, buffer, size);
	}
	close (fds);
	close (sock_fds);
	exit (0);
    }
    else {	/* child */

        /* 
         * Name the socket
         */

	bzero (&sock_add, sizeof (sock_add));
	sock_add.sin_family = AF_INET;
	bcopy (host->h_addr, &sock_add.sin_addr, host->h_length);
	sock_add.sin_port = htons ((unsigned short) PORTNUM);	

        if (connect (sock_fds, &sock_add, sizeof (sock_add)) < 0) {
	    perror ("connect");
	    close (sock_fds);
	    exit (1);
	}
	sprintf (buffer, "connected to port #%d\n", ntohs (sock_add.sin_port));
	write (1, buffer, strlen (buffer));

	strcpy (buffer, "Hi, this is a test !");
	size = strlen (buffer);
	write (sock_fds, buffer, size);
	close (sock_fds);
	exit (0);
    }
}

----
Patrick J. Spinler             		    PSPinler@att1.Mankato.MSUS.EDU
Student Programmer			    PSPinler@vax1.Mankato.MSUS.EDU
Mankato State University		    PSPinler@msus1.MSUS.EDU

gt0178a@prism.gatech.EDU (BURNS,JIM) (07/21/90)

A while back, Patrick Spinler posted a sockets program that he was having
trouble with and asked for email replies. I sent him the mail below asking
for his results and it bounced. Would appreciate email OR a post
(especially if YOU bounce).

From MAILER-DAEMON@prism.gatech.edu Fri Jul 20 22:00:12 1990
Return-Path: <MAILER-DAEMON@prism.gatech.edu>
Received: by hydra.gatech.edu (5.61/3.1)
	id AA06120; Fri, 20 Jul 90 21:50:48 -0400
Date: Fri, 20 Jul 90 21:50:48 -0400
From: MAILER-DAEMON@prism.gatech.edu (Mail Delivery Subsystem)
Subject: Returned mail: Cannot send message for 5 days
Message-Id: <9007210150.AA06120@prism.gatech.edu>
To: gt0178a@prism.gatech.edu
Status: RO

   ----- Transcript of session follows -----
421 att1.mankato.msus.edu.cether... Deferred: Connection timed out during user open with att1.mankato.msus.edu

   ----- Unsent message follows -----
Return-Path: <gt0178a@prism.gatech.edu>
Received: by hydra.gatech.edu (5.61/3.1)
	id AA15925; Sun, 15 Jul 90 21:55:38 -0400
Date: Sun, 15 Jul 90 21:55:38 -0400
From: gt0178a@prism.gatech.edu (BURNS,JIM)
Message-Id: <9007160155.AA15925@prism.gatech.edu>
To: pspinler@att1.mankato.msus.edu
Subject: Re: question about using sockets on sysVr3

pspinler@att1.mankato.msus.edu (Patrick J. Spinler):
> I am attempting to learn to use sockets, however, I am evidently
> creating a protocal error of some sort, and have no real idea where to
> start looking.  If some kind soul would please tell me why I'm getting
> an EINVAL error on my call to bind(), I would be very grateful.  I'm

Did you ever get an answer to your question, and can you forward same to
me? I would prefer working code and an explanation of the changes.

I got past your bind error by commenting out the bcopy just before, but
then the connection never established, and timed-out.

				Thanx, Jim Burns
-- 
BURNS,JIM
Georgia Institute of Technology, Box 30178, Atlanta Georgia, 30332
uucp:	  ...!{decvax,hplabs,ncar,purdue,rutgers}!gatech!prism!gt0178a
Internet: gt0178a@prism.gatech.edu