LinuxQuestions.org
Visit Jeremy's Blog.
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 02-25-2009, 09:25 PM   #1
scmbg
Member
 
Registered: Oct 2008
Location: Mexico City
Distribution: Fedora
Posts: 65

Rep: Reputation: 15
Unhappy Close socket error/problem C/C++


I have a "little" problem with sockets, there is a little intro:

I'm developing an application, is a remote file manager, i use qt, and need make it Object Oriented, so i use C++, and the BSD Sockets.

The server is concurrent, i use fork(), to create a new process that attend the new connection. Everything is fine, i can list files, copy, moving, delete, etc. from several host.

But when the server is closed and relaunched immediately i have this error:

Error in BIND: Socket/Address already in use.

The socket is a "class" that have the file descriptor, address, etc. The code bellow is part of the "Server" class, that have one pointer to socket object... there is the code.

Code:
// In the constructor, the socket is created, this code is in other member function.
try
{
	this->sckt->Bind();
	this->sckt->Listen(50);
}	
catch(const char* e)
{
	perror(e);
	exit(2);
}
while (1)
{
	// Here start the infinite loop
	int nvo_socket;
	try
	{
		nvo_socket = this->sckt->Accept();
	}
	catch(const char* e)
	{
		perror(e);
		exit(1);
	}

	// Create the child
	pid_t id = fork();
	if (id == 0)
	{
		// In the child i close the parent socket
		// create a new socket with the client socket file descriptor
		close(this->sckt->desc_socket);
		Socket* sk = new Socket(nvo_socket, this->sckt);

		// redirect and recreate the child pointers [that pointed to parent adress]
		this->sckt = sk;
		this->buff = new char[256];

		// Here I take the new conection and make my work
		this->Atender();

		// delete the socket
		// this make a close(sock_fd)
		delete this->sckt;

		// Free the buffer.
		delete [] this->buff;

		// Done with the child
		// Do not return or break because
		// in the main i delete server that delete socket
		// and i have a "double free memory" error.
		exit(0);
	}
	else
	{
		close(nvo_socket);
		// Here i put a break to test with only one petition,
		// Just attend one conecction and die.
		break;
	}

}
After the break, return to main, here i delete the server; in the destructor i delete the socket [close the socket] and free the buffer.

The client is fine, create, connect and close one socket per action [ls, cp, mv, rm].

I try some solutions, but now i have no more ideas, or clues, can you help me

If you want i can post or upload more code, just tell me
 
Old 02-26-2009, 02:44 PM   #2
clvic
Member
 
Registered: Feb 2008
Location: Rome, Italy
Distribution: OpenSuSE 11.x, vectorlinux, slax, Sabayon
Posts: 206
Blog Entries: 2

Rep: Reputation: 45
Look after the SO_REUSEADDR option for the socket() function call. Its function is described in the manpage.
This should do the trick
 
Old 02-26-2009, 02:45 PM   #3
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
To quote from Volume 1 (third edition) of UNIX Network Programming, by W. Richard Stevens of happy memory, published by Addison Wesley:
Quote:
SO_REUSEADDR allows a listening server to start and bind its well-known port, even if previously established connections exist that use this port as their local port. This condition is typically encountered as follows:
  1. A listening server is started.
  2. A connection request arrives and a child process is spawned to handle that client.
  3. The listening server terminates, but the child continues to service the client on the existing connection.
  4. The listening server is restarted.
By default, when the listening server is restarted in (d) by calling socket, bind, and listen, the call to bind fails because the listening server is trying to bind a port that is part of an existing connection (the one being handled by the previously spawned child). But if the server sets the SO_REUSEADDR socket option between the calls to socket and bind, the latter function will succeed. All TCP servers should specify this socket option to allow the server to be restarted in this situation.

This scenario is one of the most frequently asked questions on USENET.
If you intend to devote much time to network programming, get this book, and spend a rainy weekend curled up reading most of it. It will save you oodles of time, and perhaps help you solve problems you don't know you have until your application is out in the field.

Hope this helps.
 
Old 03-04-2009, 12:25 PM   #4
scmbg
Member
 
Registered: Oct 2008
Location: Mexico City
Distribution: Fedora
Posts: 65

Original Poster
Rep: Reputation: 15
Thank you so much, SO_REUSEADDR do the work.

I see anothers tips in that book. Actually i think change the child-per-client, to thread-per-client.
 
Old 03-04-2009, 04:50 PM   #5
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
i think change the child-per-client, to thread-per-client.
There can be steep long-term maintenance costs for that decision.

For what purpose would you change from processes to threads?
 
Old 03-12-2009, 10:55 PM   #6
scmbg
Member
 
Registered: Oct 2008
Location: Mexico City
Distribution: Fedora
Posts: 65

Original Poster
Rep: Reputation: 15
Just for performance research, at the school we want create a cluster from the scratch, we cant use MPI or something like that, so i need create the comunication software and use threads could help in performance.
 
Old 03-13-2009, 04:33 AM   #7
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
Originally Posted by scmbg View Post
Just for performance research
Interesting. Thank you.
 
  


Reply

Tags
close, socket



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
close socket + kernel module ibaniski Linux - Kernel 2 11-11-2008 02:45 PM
Why can i not close socket with command /quit lrios Programming 11 05-08-2008 11:06 PM
command to close tcp socket powah Linux - Networking 1 08-17-2007 12:22 PM
race condition in close socket?? jwstric2 Programming 3 03-18-2005 05:01 PM
Close socket/port THETEZ Linux - General 4 03-28-2004 08:05 PM

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

All times are GMT -5. The time now is 04:50 PM.

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