Help on sockets needed!
John Ioannidis
ji at garfield.columbia.edu
Fri Feb 28 12:50:42 AEST 1986
In article <507 at asuvax.UUCP>, system at asuvax.UUCP (Marc Lesure) writes:
> Here at Arizona State University we have a class being taught which requires
> the students to use sockets. The problem we have is that when these users
> start running their software, our VAX hangs and the only way to recover is
> to hard crash the VAX. I've looked at the routines the students have
> written and they seem to follow what the documentation says (I'm not that
> familiar with the socket software).
>
> Is there an error(s) in the documentation?
Are you kidding? errors or omissions in the Unix documentation?
> Is there a limit on the number of sockets that can used at any on time?
There is only that limit of the #of processes trying to connect
to a specific socket. (As specified in the listen(2) call)
> Do sockets prevent dead-locking situations?
Most probably they create dlock's inside the kernel :-)
>
> We are running a VAX 11/780 with 4.2bsd.
>
>From the description, I understand that the problem is with Unix domain
(AF_UNIX) sockets.
Two programs that demonstrate the use of unix-domain sockets are the
following. Compile the as follows:
$ cc und_send.c -o us
$ cc und_recv.c -o ur
and execute them :
$ ur&
$ us
Then, you'll (hopefully!) get the following output, which comes from ur:
Accepted!
Lenght:110
Family: 1
Path: yourself
Got peer!
Lenght:110
Family: 0
Path:
Read: Hello, yourself!
Of course, like it says in the manual, the peer's family is always 0 and
the path doesn't make sense.
Sorry that the programs are not very well commented (the code is obvious :-) )
but I just don't have the time to add them right now.
So, enjoy:
----------CUT HERE: und_send.c ------------
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
/*
* I included <sys/un.h> here and changed the path length from 109 to 108.
* Don't ask me why -- it works this way, but didn't with 109!!!
*/
/* un.h 6.1 83/07/29 */
/*
* Definitions for UNIX IPC domain.
*/
struct sockaddr_un {
short sun_family; /* AF_UNIX */
char sun_path[108]; /* path name (gag) [mod by ji] */
};
#ifdef KERNEL
int unp_discard();
#endif
main()
{
int sm;
struct sockaddr_un myself;
struct sockaddr_un yourself;
bzero( &myself, sizeof( myself ) );
bzero( &yourself, sizeof( yourself ) );
myself.sun_family = AF_UNIX;
yourself.sun_family = AF_UNIX;
strcpy( myself.sun_path, "myself" );
strcpy( yourself.sun_path, "yourself" );
sm = socket( AF_UNIX, SOCK_STREAM, 0, 0 );
if( sm < 0 )
{
perror( "und_send: socket" );
exit(1);
}
unlink( "myself" );
if( bind( sm, &myself, sizeof( myself ) ) < 0 )
{
perror( "und_send: bind" );
exit( 1 );
}
if( connect( sm, &yourself, sizeof( yourself ) ) < 0 )
{
perror( "und_send: connect" );
exit( 1 );
}
if( write( sm, "Hello, yourself!", 17 )<0 )
perror( "und_send: write" );
}
-------------CUT HERE: und_recv.c -----------
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
/* un.h 6.1 83/07/29 */
/*
* Definitions for UNIX IPC domain.
*/
struct sockaddr_un {
short sun_family; /* AF_UNIX */
char sun_path[108]; /* path name (gag) [mod by ji] */
};
#ifdef KERNEL
int unp_discard();
#endif
main()
{
/* Now, I am YOURSELF!!! */
int i, sm, as, rfds, mylen, hislen;
char buf[80];
struct sockaddr_un myself;
struct sockaddr_un yourself;
struct sockaddr_un himself;
bzero( &myself, sizeof( myself ) );
bzero( &yourself, sizeof( yourself ) );
bzero( &himself, sizeof( himself ) );
yourself.sun_family = AF_UNIX;
strcpy( yourself.sun_path, "yourself" );
sm = socket( AF_UNIX, SOCK_STREAM, 0, 0 );
if( sm < 0 )
{
perror( "und_recv: socket" );
exit(1);
}
unlink( "yourself" );
if( bind( sm, &yourself, sizeof( yourself ) ) < 0 )
{
perror( "und_recv: bind" );
exit( 1 );
}
if( listen( sm, 5 ) < 0 )
{
perror( "und_recv: listen" );
exit( 1 );
}
/*
* SCREAM OF JOY !!!!!!!!!!
* The key to success is the following select statement
* I saw that in a program our system administrator had
* given me quite some time ago, but which did not otherwise
* work like I wanted it to. Also note that the select(2)
* call is somewhat changed in 4.3, so beware.
*/
rfds = 1 << sm;
select( 20, &rfds, 0, 0, 0 );
mylen = sizeof( myself );
if( ( as=accept( sm, &myself, &mylen )) < 0 )
{
perror( "und_recv: accept" );
exit( 1 );
}
printf( "Accepted!\n\tLenght:%d\n\tFamily: %d\n\tPath: %s\n\n",
mylen, myself.sun_family, myself.sun_path );
hislen = sizeof( himself );
getpeername( as, &himself, &hislen );
printf( "Got peer!\n\tLenght:%d\n\tFamily: %d\n\tPath: ",
hislen, himself.sun_family );
for( i=0; i<hislen; i++ )
putchar( himself.sun_path[i]);
printf( "\n\n" );
if( read( as, buf, 80 ) < 0 )
{
perror( "und_recv: read" );
exit( 1 );
}
printf( "Read: %s\n", buf );
}
----------------CUT HERE-------------
So, that's all, folks. I'm posting this instead of directly mailing
it because I'm pretty sure that other people have also had problems
with unix-domain sockets.
____________
#include <appropriate_disclaimers>
VOICE: +1 212 280 5510 ARPA: ioannidis at cs.columbia.EDU
USnail: John Ioannidis ji at garfield.columbia.EDU
450 Computer Science
Columbia University, USENET: ...{seismo|topaz}!
New York, NY 10027 columbia!garfield!ji
... It's all Greek to me!
More information about the Comp.unix.wizards
mailing list