LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-14-2010, 07:24 AM   #1
dec10
LQ Newbie
 
Registered: May 2010
Posts: 3

Rep: Reputation: 0
Reuse socket fails "Address already in use" or "Connection refused"


Problem: can't restart programs which use socket to communicate.

Symptom: Restart Listener dies "Address already in use".
Restart Caller dies "connection refused"

System: Ubuntu 10 (and 9)

General description.
Run Listener and Caller - ok. Stop Caller. Can rerun.
Stop Listener. Now neither will run, errors as above.

Tried?:-
Much net search, but most of it old, and doesn't fix.
BOTH programs try setsockopt with SO_REUSEADDR, to no avail.

Of note:
They are using AF_UNIX. (much of the net stuff is for reuse of ports on AF_INET.

I'm sure it's too blatant to be anything but a problem with this newbie, but any help would be appreciated - I'm stuck. (I wish "connection refused" would say why!)

Thanks

Minimised code below:-

Listener:-
Code:
	struct sockaddr_un sockaddr ;
	int sd;
	int turniton;
	char wait;

	if ((sd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
		printf("Can't get socket\n");

	turniton = 1;
	if (setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &turniton, sizeof(turniton)) < 0)
		printf("can't set reuse - %s\n", strerror(errno));

	memset(&sockaddr, '\0', sizeof(sockaddr)) ;
	sockaddr.sun_family = AF_UNIX ;
	strcpy(sockaddr.sun_path, "/var/ipcf/ipcfc") ;

	if (bind(sd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)) == -1)
		printf("bind failed - %s\n", strerror(errno)) ;

	wait = getchar();

	close(sd);
----------------------

Caller

Code:
	struct sockaddr_un	sockaddr ;
	int			sd ;
	int			turniton;

	sd = socket(AF_UNIX, SOCK_STREAM, 0) ;
	if (!sd)
		printf("no sd!\n");

	turniton = 1;
	if (setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &turniton, sizeof(turniton)) < 0)
		printf( "Can't set reuse - %s\n", strerror(errno));

	memset(&sockaddr, '\0', sizeof(sockaddr)) ;
	sockaddr.sun_family = AF_UNIX ;
	strcpy(sockaddr.sun_path, "/var/ipcf/ipcfc") ;

	if (connect(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)) == -1)
		printf("connect failed - %s\n", strerror(errno));

	close(sd);
 
Old 05-15-2010, 09:39 AM   #2
ForzaItalia2006
Member
 
Registered: Dec 2009
Location: Walldorf, Germany
Distribution: (X)Ubuntu, Arch, Gentoo
Posts: 205

Rep: Reputation: 67
Hey,

first of all welcome to the linux forum :-)

You geht this 'address alrady in use' because the path name, in your case /var/ipcf/ipcfc, still exists when your program exists. When you then try to re-bind to this existing path, you get the error message, though you need to make sure that the file system path does not exist when you bind to it, e.g. by calling unlink(2)

Why the re-use doesn't work, I don't really know, yet ;-)

- Andi -
 
Old 05-15-2010, 11:34 AM   #3
dec10
LQ Newbie
 
Registered: May 2010
Posts: 3

Original Poster
Rep: Reputation: 0
Yes, thanks Andi.
For now I have got round it by sharing an incremental new name on startup.
This has the spinoff that the caller can even start before the listener and wait after "connection refused", because it can tell when the new listener is available.
So...
Problem bypassed with additional benefits.
Problem is still there and I would like to understand the proper cure, because surely I'm not the first to fall over this.
 
Old 05-15-2010, 12:14 PM   #4
dec10
LQ Newbie
 
Registered: May 2010
Posts: 3

Original Poster
Rep: Reputation: 0
Ha!
A single keyword in your answer is the key.
Immediately before Bind, call unlink(name) and ignore any error. This should mean the file is not there or is no longer there.

So I now have both bits in there, and no problems.

Thanks!

[ Still think the original problem is valid if anyone reads this far ]
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
DVD automount fails with "CDROM not ready yet" and "unaligned transfer" erica Linux - Hardware 1 12-14-2009 07:59 PM
anybody familiar with this error "Xlib: connection to ":0.0" refused by server" ? rajnivanza Linux - Newbie 3 03-22-2009 05:10 PM
glibc 2.9 "make check" fails on rt/tst-cputimer with "timer sig[12] invoked to soon" shachter Linux - Software 2 02-14-2009 01:24 PM
net working eth0 eth1 wlan0 "no connection" "no LAN" "no wi-fi" Cayitano Linux - Newbie 5 12-09-2007 07:11 PM
What is "Xlib: connection to ":0.0" refused by server " rack201 Linux - Software 1 02-04-2004 07:49 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:14 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration