Socket programming examples wanted

Bradley Smith bes at holin.ATT.COM
Sat Oct 21 03:51:25 AEST 1989


In article <430 at cscnj.csc.COM> paul at 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 at holin.ATT.COM



More information about the Comp.unix.questions mailing list