[1186] in linux-net channel archive
No subject found in mail header
daemon@ATHENA.MIT.EDU (Paul.Poetsma@hio.hen.nl)
Mon Oct 9 22:06:52 1995
From: Paul.Poetsma@hio.hen.nl
Date: Mon, 9 Oct 1995 15:44:44 +0100
To: linux-net@vger.rutgers.edu
HELP,
I'm trying to establish a datagram connection with sockets on the UNIX domain.
Appended to this mail is the source code which runs on SunOS but wont on
Linux 1.2.
In the source code are comments where things are going wrong. I have no idea
The sockets are created correct (filesystem) and the bind call succeeds too.
The problem is with sending and receiving data.
What am I doing wrong?
Thanks,
Paul Poetsma
------------------
imp.c
------------------
#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <signal.h>
#define DEBUG 1
#define MAXPATH 109
#define MAXNEIGHBOUR 5
#define usage "usage : %s imp_name neighbour_imp ...\n"
int sd[MAXNEIGHBOUR];
struct sockaddr_un sockname[MAXNEIGHBOUR] , to, from;
int fromlen = sizeof(from), tolen = sizeof(to);
char buf[100];
int buflen = sizeof(buf);
int ready, numfound, numneighbour=0;
main(argc,argv)
int argc; char *argv[];
{
int i;
struct timeval tijd;
extern cleanup();
struct sockaddr_un neighbour[MAXNEIGHBOUR];
if (argc <3) {
fprintf(stderr,usage,argv[0]);
exit(1);
}
signal(SIGTERM, cleanup);
for (i=0;i<argc-2 && i<MAXNEIGHBOUR;i++) {
numneighbour++;
sd[i]=socket(AF_UNIX, SOCK_DGRAM,0);
#ifdef DEBUG
fprintf(stderr,"sd[%d]=%d\n",i,sd[i]);
#endif
if (sd[i] < 0) {
perror("socket");
exit(2);
}
/* install sockets for all
datalink connections */
sockname[i].sun_family = AF_UNIX;
sockname[i].sun_path[0] = argv[1][0];
sockname[i].sun_path[1] = argv[i+2][0];
sockname[i].sun_path[2] = '\0';
#ifdef DEBUG
fprintf(stderr,"socketname[%d].path = %s; sd = %d \n",i,sockname[i].sun_path,sd[i]);
#endif
if ( bind(sd[i], &sockname[i],
sizeof(struct sockaddr_un)) <0) {
perror("bind");
exit(3);
}
/* remember corresponding
neighbour socket */
neighbour[i].sun_family = AF_UNIX;
neighbour[i].sun_path[0] = argv[i+2][0];
neighbour[i].sun_path[1] = argv[1][0];
neighbour[i].sun_path[2] = '\0';
}
while (1) {
for (i=0; i<numneighbour; i++)
ready |= (1 << sd[i]);
tijd.tv_sec=(long) 10;
tijd.tv_usec= (long) 0;
numfound=select(32,&ready, (int *) 0, (int *) 0,&tijd);
sleep(5); /* let all IMP's get installed */
if (!numfound)
fprintf (stderr,
"No information received in timeout period at %s\n",
argv[1]);
for (i=0; i<numneighbour ; i++){
if (ready & (1 << sd[i])){
/************************
the 'recvfrom' call results into an error which says it can't find the endpoint
************************/
if (recvfrom (sd[i],
buf, buflen,0, &from, &fromlen)>0){
fprintf(stdout,
"%s:from %s received %s$\n",
argv[1],neighbour[i].sun_path,
buf);
}
else perror("receive"); /* ?? */
/************************
the 'sendto' call results into an error which says it can't find the endpoint
************************/
if ( sendto (sd[i],"ok",
sizeof("ok"),0,&neighbour[i],tolen)<0)
perror("sendto"); /* link or IMP down */
}
/************************
the 'sendto' call results into an error which says it can't find the endpoint
************************/
else if (sendto(sd[i],"hallo",
sizeof("hallo"),0,&neighbour[i],tolen)<0)
perror("sendto"); /* link or IMP down */
}
}
}
cleanup() {
int i;
for (i=0; i<numneighbour; i++) {
close(sd[i]);
unlink(sockname[i].sun_path);
}
exit(6);
}