Bhoot 0 Junior Poster in Training

I am developing a peer-to-peer RAT tool.
The mechanism i use is as follows :
client-side : connect to the server.
server-side : listen and accept the incoming connection.

The related code is as follows :

// client - side :

void ClientDialog::connectToServer()
{
	if (connectingSocket_.Connect (_T("bhoot"), PORT) == 0)
		MessageBox ("Socket connection from client side failed.");
	else
	{
		if(isConnected_ == TRUE)
		{
			((CButton*) GetDlgItem(IDC_CONNECT))->SetWindowText("Connected!");
			((CButton*) GetDlgItem(IDC_CONNECT))->EnableWindow (FALSE);	
		}
	}	
}

afx_msg void ClientDialog::OnClose()
{
	MessageBox ("OnClose");
	connectingSocket_.Close();
	CDialog::OnClose();
}

Server-side :

ServerFrame::ServerFrame()
{
  //other code

  	if (listeningSocket_.Create (PORT) == 0)
		MessageBox ("Listening socket creation Failed!") ;
	
	if (listeningSocket_.Listen(1) == 0)
		MessageBox ("Listening Failed.") ;
}

void ServerFrame::acceptConnection()
{
	if (listeningSocket_.Accept (receivingSocket_) == 0)
		MessageBox ("Could not accept connection!");
}

void ServerFrame::receiveData()
{	
	nReceivedBytes_ = receivingSocket_.Receive (messageBuffer_, bufferSize_);

	if (nReceivedBytes_ == SOCKET_ERROR)
		MessageBox ("Receiving Failed.");
	else if (nReceivedBytes_ == 0)
		MessageBox ("Client side socket has been closed!");
	else
	{
                //process the received message
        }
}

ServerFrame::~ServerFrame()
{

	listeningSocket_.Close();
	receivingSocket_.Close();
}

As shown in the code, i have set up the server to accept only one connection (.Listen(1)).
When i connect the client to the server for the first time, it connects readily. But, then i close the client window, thereby closing the socket on client side. However the server is still listening. Now, when i again connect to the same server by running a new client window, the server application crashes.
I think it is because the server didnt come to know that the earlier client application was closed. But, i could not find any solution to it.
If the thing is to notify the server before closing the client socket, how do i do it?

Please help me out with some solution.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.