Click here to Skip to main content
15,927,699 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Frustrated with conversion Pin
Christian Graus15-Nov-06 19:13
protectorChristian Graus15-Nov-06 19:13 
GeneralRe: Frustrated with conversion Pin
Stick^15-Nov-06 19:44
Stick^15-Nov-06 19:44 
QuestionCreating a DLL Pin
colin_os15-Nov-06 10:38
colin_os15-Nov-06 10:38 
AnswerRe: Creating a DLL Pin
Blake Miller15-Nov-06 10:52
Blake Miller15-Nov-06 10:52 
QuestionCSocket connections Pin
jpyp15-Nov-06 8:53
jpyp15-Nov-06 8:53 
AnswerRe: CSocket connections Pin
Mark Salsbery15-Nov-06 10:19
Mark Salsbery15-Nov-06 10:19 
GeneralRe: CSocket connections Pin
jpyp16-Nov-06 6:05
jpyp16-Nov-06 6:05 
GeneralRe: CSocket connections Pin
Mark Salsbery16-Nov-06 7:08
Mark Salsbery16-Nov-06 7:08 
//
jpyp wrote:
The queue size parameter in listen() does not affect the number of simultaneous connections that the server will accept


That's what I meant Smile | :)

For TCP here's how I found to do it. If someone else jumps in with a better solution I'd like to
see it!!

WSAAccept() is the only accept function that allows you to refuse a connection. BUT, with TCP/IP
the connection has to be made to prevent SYN attacks. WSAAccept() will immediately close the
socket though if you refuse the connection. What I do is try a send() of 0 bytes on the client
right after I connect. It will fail if the server refused the connection.
Here's an example (I altered the worker thread a bit so you wouldn't have to do the Sleep() a
little and loop thing)...

Initialize before worker thread runs:
<small>// create listener socket (svr_sck)
...
hTerminateEvent = ::CreateEvent(NULL, false, false, NULL);
hNetEvent = ::CreateEvent(NULL, true, false, NULL); // manual reset
::WSAEventSelect(svr_sck, hNetEvent, FD_ACCEPT);</small>

Worker thread proc:
<small>while (1)
{
	#define EVENT_NET		0
	#define EVENT_TERMINATE	1
	HANDLE Events[] =
	{
		hNetEvent,
		hTerminateEvent
	};
	DWORD dwEventIndex = ::WaitForMultipleObjects(sizeof(Events) / sizeof(HANDLE),
							Events, FALSE, INFINITE);
	if (dwEventIndex == WAIT_FAILED)
	{
		break;
	}
	dwEventIndex -= WAIT_OBJECT_0;
 
	// Check if thread termination requested
 
	if (dwEventIndex == EVENT_TERMINATE)
	{
		break;
	}
 
	// Check if FD_ACCEPT socket notification
 
	if (dwEventIndex == EVENT_NET)
	{
		WSANETWORKEVENTS WsaNetworkEvents;
		if (0 == ::WSAEnumNetworkEvents(svr_sck, hNetEvent, &WsaNetworkEvents)) //(resets hNetEvent)
		{
			if (WsaNetworkEvents.lNetworkEvents & FD_ACCEPT)
			{
				sockaddr SockAddr;
				int SockAddrLen = sizeof(sockaddr);
				SOCKET hSocket = ::WSAAccept(svr_sck, &SockAddr, &SockAddrLen,
							AcceptConditionProc, (DWORD_PTR)tal_scks);
				if (hSocket != INVALID_SOCKET)
				{
					// save the socket in an array
					...
					total_scks++;
				}
			}
		}
	}
 
}  //while (1)
 
::ExitThread(0U);
 
return 0U; //never get here - just makes compiler happy</small>

AcceptConditionProc():
<small>int CALLBACK AcceptConditionProc(LPWSABUF lpCallerId,
				LPWSABUF lpCallerData,
				LPQOS lpSQOS,
				LPQOS lpGQOS,
				LPWSABUF lpCalleeId,
				LPWSABUF lpCalleeData,
				GROUP FAR * g,
				DWORD_PTR dwCallbackData)
{
	// Return CF_ACCEPT, CF_REJECT, or CF_DEFER
 
	int ConnectionCount = (int)dwCallbackData;
 
	if (ConnectionCount < MAX_SCKS)
	{
		return CF_ACCEPT;
	}
 
	return CF_REJECT;
}</small>

On the client:
<small>// create the socket
...
// connect the socket to server
result = mySck.Connect(ip address, port number);
if (!result)
{
// report error
...
}
else
{
	// Check if server refused connection
	int err = 0;
	int sockret = ::send(mySck.m_hSocket, (const char *)&err, 0, 0);
	if (sockret == SOCKET_ERROR)
	{
		err = ::WSAGetLastError();
		mySck.Close();
		TRACE1( "** Connection refused by server (::send() 0 bytes returned errcode %li)\n", err);
		return;
	}
}</small>


*EDIT* formatted the code a bit
GeneralRe: CSocket connections Pin
jpyp16-Nov-06 7:30
jpyp16-Nov-06 7:30 
QuestionDetect button three press Pin
Johpoke15-Nov-06 8:33
Johpoke15-Nov-06 8:33 
QuestionRe: Detect button three press Pin
David Crow15-Nov-06 10:01
David Crow15-Nov-06 10:01 
AnswerRe: Detect button three press Pin
Johpoke16-Nov-06 5:32
Johpoke16-Nov-06 5:32 
QuestionRe: Detect button three press Pin
David Crow16-Nov-06 5:39
David Crow16-Nov-06 5:39 
GeneralRe: Detect button three press Pin
Mark Salsbery16-Nov-06 5:55
Mark Salsbery16-Nov-06 5:55 
GeneralRe: Detect button three press Pin
Johpoke16-Nov-06 7:09
Johpoke16-Nov-06 7:09 
GeneralRe: Detect button three press Pin
Mark Salsbery16-Nov-06 7:18
Mark Salsbery16-Nov-06 7:18 
GeneralRe: Detect button three press Pin
Johpoke16-Nov-06 7:23
Johpoke16-Nov-06 7:23 
GeneralRe: Detect button three press Pin
Mark Salsbery16-Nov-06 7:33
Mark Salsbery16-Nov-06 7:33 
GeneralRe: Detect button three press Pin
Johpoke16-Nov-06 8:39
Johpoke16-Nov-06 8:39 
AnswerRe: Detect button three press Pin
PJ Arends15-Nov-06 16:20
professionalPJ Arends15-Nov-06 16:20 
AnswerRe: Detect button three press Pin
Hamid_RT15-Nov-06 18:39
Hamid_RT15-Nov-06 18:39 
QuestionClosing popup dialog window. Pin
xkrja15-Nov-06 7:23
xkrja15-Nov-06 7:23 
AnswerRe: Closing popup dialog window. Pin
Mark Salsbery15-Nov-06 11:23
Mark Salsbery15-Nov-06 11:23 
GeneralRe: Closing popup dialog window. Pin
xkrja15-Nov-06 21:31
xkrja15-Nov-06 21:31 
GeneralRe: Closing popup dialog window. Pin
xkrja15-Nov-06 22:16
xkrja15-Nov-06 22:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.