[comp.unix.questions] Socket programming examples wanted

johnl@tw-rnd.SanDiego.NCR.COM (johnl) (10/17/89)

	I would like to get some sample code illustrating the use of sockets
in a client/server model.  I am using Internet domain, stream sockets and am
having some trouble.

In my example code:
------------------
1. The server creates/binds/listens on a socket with a well-known port number
2. The client creates a socket and connects to the server's socket.
3. The client sends a message.
4. The server (using select) detects the incoming message,
                          calls accept to get a new file descriptor,
                          forks a child which calls recv to read the message,
                          child closes it's (new) socket and dies.
5. Goto 3.

	The first message is transferred correctly, but subsequent messages
are "lost".  Any help or sample code appreciated!  I can email the code to
anyone interested in helping me debug.  Thanks!!

-- 
----------------------------------------------------------------------
John Lindwall                            johnl@tw-rnd.SanDiego.NCR.COM
           "Above opinions are my own, not my employer's"
PenPal V1.1 for sale: $90.  All original disks, manual, box.  Registration.

paul@cscnj.csc.COM (Paul Moody) (10/20/89)

In article <101@tw-rnd.SanDiego.NCR.COM>, johnl@tw-rnd.SanDiego.NCR.COM (johnl) writes:
> 
> 	I would like to get some sample code illustrating the use of sockets
> in a client/server model.  I am using Internet domain, stream sockets and am
> In my example code:
> 4. The server (using select) detects the incoming message,
>                           calls accept to get a new file descriptor,
>                           forks a child which calls recv to read the message,
>                           child closes it's (new) socket and dies.

I dont know what flavor of tcp/ip you are using, but all three that
we work with say 'DONT USE SELECT'. Instead use a blocking accept
and set an alarm. 
eg:

		/* accept new connections */
			timeout = FALSE;
			signal(SIGALRM,clockwatch);
			alarm(1);
			if ((news=accept(s,&newaddr,&newaddrlen)) == -1)
			{	if (timeout == FALSE)
					abort("accept failed");
			}
			else
			{
				makechild(news);
			}
			alarm(0);
-- 
Paul Moody			UUCP: rutgers!cscnj!paul 
Computer Sciences Corporation
# the opinions expressed are entirely imaginary			#

chris@mimsy.umd.edu (Chris Torek) (10/20/89)

In article <430@cscnj.csc.COM> paul@cscnj.csc.COM (Paul Moody) writes:
>I dont know what flavor of tcp/ip you are using, but all three that
>we work with say 'DONT USE SELECT'. Instead use a blocking accept
>and set an alarm.

All three that you work with are broken, then.

Select for reading returns true whenever an accept() will not block.
-- 
`They were supposed to be green.'
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@cs.umd.edu	Path:	uunet!mimsy!chris

bes@holin.ATT.COM (Bradley Smith) (10/21/89)

In article <430@cscnj.csc.COM> paul@cscnj.csc.COM (Paul Moody) writes:
>I dont know what flavor of tcp/ip you are using, but all three that
>we work with say 'DONT USE SELECT'. Instead use a blocking accept
>and set an alarm. 
I don't know about this, I have used select to detect when to do an
accept, below is some dumb code to show how one might do it. This way
you could/can (if you want) have the server do it all (provided that
the clients don't keep it that busy).  But it is a way to show how to
do it. FYI - the below code is for UNIX domain sockets, but INET work
the same way.
================
here:

	do {
		a2 = 1 << s1;	 /* get first one */
		for(i=0;i<nscnt;i++) 
			if(ns[i])
				a2 |= 1 << ns[i];
		time(&t);
		printf("select bitmask = 0x%x @ %s",a2, ctime(&t));
		t = 3;
		ret = select(32,&a2,0,0,&timeout);
		time(&t);
		printf("select ret=%d, bitmask = 0x%x, - %s", ret, a2,ctime(&t));
	} while(ret == 0);
	if( (1 << s1) & a2) {
		for(j = 0; j<nscnt;j++) {
			if(!ns[j])
				break;
		}
		if(j == nscnt) {
			ns[nscnt] = accept(s1, (char *) &sinsink, &addrlen);
			printf("new connection nscnt = %d\n",nscnt);
			nscnt++;
		}else {
			ns[j] = accept(s1, (char *) &sinsink, &addrlen);
			printf("new connection j = %d\n",j);
		}
	}
	for(i=0;i<nscnt;i++) {
	    if( (1 << ns[i]) & a2) {
		printf("ns[%d]:", i); fflush(stdout);
		hismsglen = read(ns[i], sinkbuf, TESTSIZE);
		if(hismsglen > 0) {
			printf("tcpsink: %d characters where received\n"
				,hismsglen);
		}else {
			sret = errno;
			perror("Recv failed!");
			printf("errno = %d\n", sret);
			close(ns[i]);
			ns[i] = 0;
		}
	   }
	}
-- 
Bradley Smith
Telecommunications Solutions Development
AT&T Bell Labs, Holmdel, NJ 
201-949-0090 att!holin!bes or bes@holin.ATT.COM

Pabbisetty.henr@xerox.com (Nagesh Pabbisetty) (10/25/89)

See the following articles for example using the client-server model:
1. Networking with BSD-style sockets by John Romkey. Unix World. July 1989.
2. Programming with BSD-style sockets by John Romkey. Unix World. August 1989.

These papers have been written in the tutorial style...

Nagesh
716-427-5458 / 5257

madd@world.std.com (jim frost) (10/26/89)

In article <21263@adm.BRL.MIL> Pabbisetty.henr@xerox.com (Nagesh Pabbisetty) writes:
|See the following articles for example using the client-server model:
|1. Networking with BSD-style sockets by John Romkey. Unix World. July 1989.
|2. Programming with BSD-style sockets by John Romkey. Unix World. August 1989.
|
|These papers have been written in the tutorial style...

I also have a simple tutorial which several people found very useful.
It's short and tries very hard to explain socket usage in human terms
as well as giving short, simple examples.

I'll send it to any who would like it, or post it if there's enough
demand.

jim frost
software tool & die
madd@std.com