[comp.lang.c] Inter Process Communication, how does it work?

ephrem@oakhill.UUCP (Ephrem Chemaly) (07/27/90)

I have two X applications, I would like to send some data from one to the
other.  The two applications do not necessarily reside on the same CPU.
Here is the code I wrote to try this, and it does not work.  I have included
the two files here.  The first "ipc.c" is the server program, that listens
to inputs, and the second "client.c" is the client program that will send the
data.  I start the server program, then I start the client program and nothing
happens.  In the file /etc/servers I have added the line:

tsteph  tcp     /usr/users/ephrem/source/misc/ipc/ipc

so when I start the client program, If the server program is not already
running, the client will start it.  Even though the server gets started,
it still does not get the input from the client.  I need to send data both
ways also.
I am trying this on Sun and on A/UX.

/******************************************************************************
*
*								ipc.c
*
*     This file contains the server communication routines.
*
******************************************************************************/

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

/******************************************************************************
********** doit(): .  *******
******************************************************************************/

int	doit(g, from)
int		g;
struct sockaddr_in *from;
{
char	buf[256];

printf("Doing it\n");
read(g, buf, sizeof(buf));
}
/******************************************************************************
********** main(): .  *******
******************************************************************************/

main(argc, argv, envp)
int argc;
char **argv, **envp;
{
struct	servent *sp;
int		s, ret;

if ( (sp=getservbyname("tsteph", "tcp")) == 0 )
	{
	perror("getservbyname");
	exit(1);
	}
sp->s_aliases = 0;

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

if ( (ret=bind(s, (caddr_t)sp, sizeof(struct servent) )) < 0 )
	{
	perror("bind");
	exit(3);
	}

if ( (ret=listen(s, 5)) < 0 )
	{
	perror("listen");
	exit(4);
	}

for ( ; ; )
	{
	struct sockaddr_in	from;
	int	g, len = sizeof(from);
	printf("Waiting for connection:\n");
	if ( (g=accept(s, &from, &len)) < 0 )
		{
		perror("accept");
		continue;
		}
	if ( fork()==0 )
		{
		close(s);
		doit(g, &from);
		}
	close(g);
	}
}


/******************************************************************************
*
*								client.c
*
*     This file contains the (inter process communication) client
* communication routines.
*
******************************************************************************/

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>		/* For sockaddr_in. */
#include <netdb.h>
#include <sys/errno.h>

/******************************************************************************
********** main(): .  *******
******************************************************************************/

main(argc, argv, envp)
int argc;
char **argv, **envp;
{
struct	sockaddr_in sin;
struct	servent *sp;
struct	hostent *hp;
int		s;
char	*hostname, *TheMessage = "A message from the client";
int		ret;

if ( (sp=getservbyname("tsteph", "tcp")) == 0 )
	{
	perror("getservbyname");
	exit(1);
	}

#ifdef macII
	hostname = "unimac";
#else
	hostname = "soleil";
#endif

if ( (hp=gethostbyname(hostname)) == 0 )
	{
	perror("gethostbyname");
	exit(2);
	}

bzero((char*)&sin, sizeof(sin));
bcopy(hp->h_addr, (char*)&sin.sin_addr, hp->h_length);
sin.sin_family = hp->h_addrtype;
sin.sin_port = sp->s_port;

printf("sin_family = %d\n", sin.sin_family);
printf("sin_port   = %d\n", sin.sin_port);
#ifdef macII
printf("sin_addr   = %u\n", sin.sin_addr);
#else
printf("sin_addr   = %d.%d.%d.%d\n", sin.sin_addr.S_un.S_un_b.s_b1, sin.sin_addr.S_un.S_un_b.s_b2, sin.sin_addr.S_un.S_un_b.s_b3, sin.sin_addr.S_un.S_un_b.s_b4);
#endif
printf("sin_zero   = %d,%d,%d,%d,%d,%d,%d,%d\n", sin.sin_zero[0], sin.sin_zero[1], sin.sin_zero[2], sin.sin_zero[3], sin.sin_zero[4], sin.sin_zero[5], sin.sin_zero[6], sin.sin_zero[7]);

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

if ( (ret=connect(s, (char*)&sin, sizeof(sin))) < 0)
	{
	perror("connect");
	exit(5);
	}

if ( ret = write(s, TheMessage, strlen(TheMessage)) < 0 )
	{
	perror("write");
	exit(4);
	}

if ( close(s) )
	perror("close");
}



Thanks in advance,
Ephrem A. Chemaly

+=========================================================================+
| Motorola Inc.                            Ephrem A. Chemaly              |
|                                          6501 William Cannon Drive West |
|                                          Austin, Texas 78735            |
|                                          Maildrop: OE-28                |
|                                          (512) 891-2760                 |
|                                                                         |
| Internal Addr: ephrem@soleil-tx                                         |
| Internet Addr: ...!soleil-tx.sps.mot.com!ephrem                         |
| UUCP Address:  oakhill!soleil-tx!ephrem@cs.utexas.edu                   |
+=========================================================================+