Winsock inside a service problem

Started by
13 comments, last by Endurion 10 years, 8 months ago

Outputdebug does NOT work. I must have like 20 of them in my service and I never get to see one in visual studio.I'll try what endurion said.

Not sure if debugview doesn't work or I'm not using it the right way...i see nothing in the list

Advertisement

I'd go for a logfile then. C:\out.txt or something like that.

Do breakpoints work? You probably want to narrow the problem down a bit before you start stepping through code though.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

void CSampleService::ServiceWorkerThread(void)
{
	ServStart();
	

	
    // Periodically check if the service is stopping.
	FILE* pFile = fopen("logFile.txt", "a");
    fprintf(pFile, "%s\n","In worker function");
    while (!m_fStopping)
	{
    

	SOCKADDR_IN SenderInfo;
	SOCKET NewConnection;

	int ClientLen = sizeof(SOCKADDR_IN);
	SOCKADDR_IN ClientAddr;

	int ByteReceived, nlen;
	char recvbuff[1024];
		// Main program loop
		NewConnection = SOCKET_ERROR;
		if(NewConnection == SOCKET_ERROR) 
		{
		
			//NewConnection = accept(ListeningSocket, NULL, NULL);	// this is a blocking function
			NewConnection = accept(ListeningSocket,(SOCKADDR*)&ClientAddr, &ClientLen);
			OutputDebugString(L"New client got connected, ready to receive and send data...\n\n");
			ByteReceived = recv(NewConnection, recvbuff, sizeof(recvbuff), 0);

			if (ByteReceived > 0) 
			{
				fprintf(pFile, "%s\n","Received bytes!");
				getsockname(ListeningSocket, (SOCKADDR *)&ServerAddr, (int *)sizeof(ServerAddr));

				memset(&SenderInfo, 0, sizeof(SenderInfo));
				nlen = sizeof(SenderInfo);

				getpeername(NewConnection, (SOCKADDR *)&SenderInfo, &nlen);	
			}

		}
		if (shutdown(NewConnection, 2) != 0)
			OutputDebugString(L"Line 355");
		else
			OutputDebugString(L"shutdown is working...\n");

		

	
	}
	fclose(pFile);
    // Signal the stopped event.
    SetEvent(m_hStoppedEvent);
}

So,see that ServStart() call at the begining,it turns out it calls that function(saw it on a logfile),but after it gets out of it,it doesn't to anything.I mean when it gets out it should post some of those lines of text,but it doesn't.

Why would the call to StartServer do fine,and after that "nothing happens" ?

Print more stuff to the file. You are still using OutputDebugString as well...

Does accept() ever return? You can probably just put a breakpoint on the OutputDebugString following to see if it does.

When tracing I usually log

+functionname()

at the start of each function and

-functionname()

everywhere it returns/exits, along with other information, especially before and after calls to blocking functions...

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

For one, m_fStopping might be true (or worse, uninitialized).

You really need to cover everything from the start downwards to see where the CPU is branching into and where not.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

This topic is closed to new replies.

Advertisement