I've made a simple webserver in C++ based on this one here. And this works fine.

Now, my assignment is to pass some info from this server to an html-file that later can be opened in IE or Firefox and then display info like screen resolution. Now.... This is supposed to go in an infinet do-while loop until(!) the localhost:port connection check (that I run manually from IE or Firefox) is closed! Then it is supposed to close sockets and end the run. My program however, never reaches the while-requirement for closing the connection! Why?

do
	{
		ClientSocket = accept(ListenSocket, NULL, NULL);
		if(ClientSocket == INVALID_SOCKET)
		{
			cout << "\naccept failed with error: " << WSAGetLastError() << endl;
                        closesocket(ListenSocket);
			WSACleanup();
			return 1;	
		} // end if
		else
		{
			cout << "\naccept() said: " << ClientSocket << endl;
			cout << "\nClient connected." << endl;
		} // end else

		iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
		if(iResult > 0)
		{
			cout << "\nBytes received: " << iResult << endl;
			iSendResult = send(ClientSocket, recvbuf, iResult, 0);
			if (iSendResult == SOCKET_ERROR) 
			{
                cout << "\nsend failed with error: " << WSAGetLastError() << endl;
                closesocket(ClientSocket);
                WSACleanup();
                return 1;
            } // end if
			else
			{
				cout << "\nBytes sent: " << iSendResult << endl;
			} // end else
		} // end if
		else if (iResult == 0)
		{
			cout << "\nConnection closing...\n" << endl;
		} // end else if
        else  
		{
            cout << "\nrecv failed with error: " << WSAGetLastError() << endl;
            closesocket(ClientSocket);
            WSACleanup();
            return 1;
        }

		//GET screen resolution
		int cx = GetSystemMetrics(SM_CXSCREEN);
		int cy = GetSystemMetrics(SM_CYSCREEN);

		// The HTML.
		ofstream index;
		file.open("file.html", ios::in);
		if(file.is_open())
		{
			file<< "<html><head><title>\n";
			file << "</title></head><body bgcolor='#990000'>\n";
			file << "<P>Screen resolution is: " << cx << " x " << cy << "...<P>\n";
			file << "</body></html>";
			file.close();
		}
		else
		{
			cout << "\nThe file was not found!" << endl;
		}

		closesocket(ClientSocket);
	} while (iResult > 0);

I tried placing the accept() outside the do-while, but then it exited before I ever got near to closing the localhost-connection.

Every answere and any help is greatly appreaciated!

Recommended Answers

All 3 Replies

Give the full source code and not just the loop.

If the Do-While Loop is not ending then it is because it is not being given a value to make it quit.

iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);

What is the code for this?

The reason why the loop will not end is because you have not decremented iResult, so when the computer executes "while (iResult > 0)", it will always evaluate to be true, and therefore keep on running.

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.