This is the mail archive of the libc-alpha@sources.redhat.com mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

A strange behaviour


Hi

I'have just written these 2 programs (client/server) that comunicate by
socket in the local domain (AF_LOCAL).

/*---server...................*/
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <sys/socket.h>
#include <net/if.h>
#include <sys/un.h>
#include <unistd.h>

#define LINE "---------------\n"

int main(void){
	int lsock,tsock;
	char buf[10]={0};
	//char uno;	/* first */
	struct sockaddr_un arr, lsock_addr;
	socklen_t arrlen;
	lsock_addr.sun_family = AF_LOCAL;
	strcpy(lsock_addr.sun_path,"m_sock");
	lsock = socket(PF_LOCAL, SOCK_STREAM, 0);
	if(lsock == -1){
		perror("socket");
		exit(0);
	}
	if(bind(lsock, (struct sockaddr *)&lsock_addr, 1+SUN_LEN(&lsock_addr))
== -1){
		perror("bind");
		exit(0);
	}
	if(listen(lsock, 10) == -1){
		perror("listen");
		exit(0);
	}
	tsock = accept(lsock, (struct sockaddr *)&arr, &arrlen);
	if(tsock == -1){
		perror("accept");
		exit(0);
	}
	char tmp[6] = {'e','x','i','t','\n','\0'};
	char uno; /* second */
	while(1){
		uno = recv(tsock, buf, 10, 0);
		if(uno == -1){
			perror("recv");
			break;
		}
		if(uno == 0)
			break;
		if(strcmp(buf,tmp) == 0)
			break;
		printf("%s", buf);
		memset(buf, 0, 10);
	}
	close(lsock);
	return 0;
}

/*...client...................*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>

int main(int argc, char **argv){
	int sock;
	char buf[11];
	struct sockaddr_un saddr;
	sock = socket(PF_LOCAL, SOCK_STREAM, 0);
	saddr.sun_family = AF_LOCAL;
	strcpy(saddr.sun_path,(*++argv));
	if ((connect(sock, (struct sockaddr*)&saddr, SUN_LEN(&saddr)+1))<0){
		perror(NULL);
		exit(0);
	}
	while(1){
		fgets(buf, 10, stdin);
		if(buf[0] == '\n')
			break;
		send(sock, buf, 10,0);
		memset(buf,0,11);
	}
	close(sock);
	return 0;
}

Once the server startup, it create a socket named 'm_sock' and the
client should be started with 'm_sock' as first argument 
(client m_sock).
The problem is within the server.
Look at the UNO variable. If it is declared after the declaration of
"struct sockaddr_un arr, lsock_addr;" all works fine.
But if it is declared before, the call 'accept' fail.
Try to believe.

How can this happen ??

Thanks in advance.

______________________________________________________________________
Yahoo! Cellulari: loghi, suonerie, picture message per il tuo telefonino
http://it.yahoo.com/mail_it/foot/?http://it.mobile.yahoo.com/index2002.html


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]