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