|
//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
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>
...
hTerminateEvent = ::CreateEvent(NULL, false, false, NULL);
hNetEvent = ::CreateEvent(NULL, true, false, NULL);
::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;
if (dwEventIndex == EVENT_TERMINATE)
{
break;
}
if (dwEventIndex == EVENT_NET)
{
WSANETWORKEVENTS WsaNetworkEvents;
if (0 == ::WSAEnumNetworkEvents(svr_sck, hNetEvent, &WsaNetworkEvents))
{
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)
{
...
total_scks++;
}
}
}
}
}
::ExitThread(0U);
return 0U;
AcceptConditionProc():
<small>int CALLBACK AcceptConditionProc(LPWSABUF lpCallerId,
LPWSABUF lpCallerData,
LPQOS lpSQOS,
LPQOS lpGQOS,
LPWSABUF lpCalleeId,
LPWSABUF lpCalleeData,
GROUP FAR * g,
DWORD_PTR dwCallbackData)
{
int ConnectionCount = (int)dwCallbackData;
if (ConnectionCount < MAX_SCKS)
{
return CF_ACCEPT;
}
return CF_REJECT;
}</small>
On the client:
<small>
...
result = mySck.Connect(ip address, port number);
if (!result)
{
...
}
else
{
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
|
|
|
|
|
Thanks a lot. This is exactly what I was looking for.
jpyp
|
|
|
|
|
How can i in c++ (or with mfc) how can i detect if mouse button three is down? ( the scroll wheel button being pressed)
like this:
while(true)
{
while(MBUTTON3 == DOWN)
{
//executed when down
}
}
thanks
/Johannes
|
|
|
|
|
Johpoke wrote: while(MBUTTON3 == DOWN)
Are you looking for WM_MBUTTONDOWN ?
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
Yea kinda, i know of that, as ive used while left button down, but i cant find out how to do while scroll wheel down...
/Johannes
|
|
|
|
|
Johpoke wrote: ive used while left button down...
Why would you be using WM_MBUTTONDOWN with the left mouse button?
Johpoke wrote: but i cant find out how to do while scroll wheel down...
What about WM_MOUSEWHEEL ?
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
He said MBUTTON, not LBUTTON right?
Check this out: About Mouse Input[^]
especially the section "The Mouse Wheel"
|
|
|
|
|
Yea thats it, i need to use these;
WM_MBUTTONDOWN //The middle mouse button was pressed.
WM_MBUTTONUP //The middle mouse button was released.
i read at http://msdn2.microsoft.com/en-us/library/ms645601.aspx
but i cant find out how to use it.. it needs windows.h, but what else do i need to do? so that this can work:
while(true)
{
while(WM_MBUTTONDOWN == TRUE)
{
//does when it down
}
}
thanks
EDIT: its important that it works globaly, so i can have this minimized and im working in another app and it should still detect it
/Johannes
|
|
|
|
|
Johpoke wrote: but i cant find out how to use it.. it needs windows.h, but what else do i need to do?
Uh oh. Have you written a windows app yet? WM_MBUTTONDOWN is a message sent by the system to
the window with focus. How you respond to this message in code is different with straight C/C++
than MFC. Maybe you have some studying to do??
|
|
|
|
|
Uh oh, well i havent done any of the programing yet, ille do that part when i know i can get this to work, so for me it doesnt matter if its mfc, win32, win32 console or anything.. just as long as its c++... is it hard to detect middle mouse globaly?
/Johannes
|
|
|
|
|
Johpoke wrote: is it hard to detect middle mouse globaly?
"hard" is relative. I imagine it's hard if you've never written a windows app before.
An understanding of Windows messaging is necessary for sure
|
|
|
|
|
ah sounds like ille have to ditch that idea..
thanks anyway
/Johannes
|
|
|
|
|
You can trap the WM_MBUTTONDOWN and WM_MBUTTONUP messages or you can use the GetAsyncKeyState() function.
You may be right I may be crazy -- Billy Joel --
Within you lies the power for good, use it!!!
|
|
|
|
|
|
My app has a main dialog window. By using PreTranslateMessage() it looks for pressed keys. When a certain key is pressed and held down a popup dialog window appears. When the key is released the window is supposed to close but it doesn't.
The code works in such way that as long as the key is pressed an if-statement is true. In the statement there is a
m_dlgMenu.ShowWindow(SW_SHOW);
which shows the popup dialog.
When the key is released the if-statement is false and a
m_dlgMenu.ShowWindow(SW_HIDE);
is called to close the popup.
The problem is that the popup window isn't closed until the mouse is moved. What may be the reason for this? It seems like it waits for a message before the popup is closed.
Thanks for any help!
|
|
|
|
|
Can you post your PreTranslateMessage() code?
Where is this "if-statement" you speak write of?
|
|
|
|
|
Actually I made a simplification when I described the problem. The PreTranslateMessage() checks for a TAB-keypress and as long as TAB is held down it calls CCameraControlDlg::ManualMode()
The CCameraControlDlg is the main dialog.
BOOL CCameraControlDlg::PreTranslateMessage(MSG* pMsg) <br />
{<br />
<br />
if (GetAsyncKeyState(VK_TAB) < 0) { <br />
CCameraControlDlg::ManualMode();<br />
}<br />
Sleep(10);<br />
return CDialog::PreTranslateMessage(pMsg);<br />
<br />
}
In the CCameraControlDlg::ManualMode() function there are again and if-statement. This look for F2-keypresses as shown below. As long F2 is held down it calls the MouseScroll-function which is used for scrolling up and down a menu in the other dialog. Then m_dlgMenu.ShowWindow(SW_SHOW) are used to show the other dialog. When the F2-key is released the other if-statement will be true and should hide the window. But nothing happens until the mouse is moved (the TAB-key is still pressed).
<br />
void CCameraControlDlg::ManualMode() {<br />
.<br />
.<br />
.<br />
<br />
if ((GetAsyncKeyState(VK_F2) < 0) && ((control_nr == 2) || (control_nr == 0))) { <br />
<br />
x_F2 = MouseScroll(menu_roof, reset_val);<br />
<br />
m_dlgMenu.ShowWindow(SW_SHOW);<br />
m_dlgMenu.m_strHeader.Format("White Balance");<br />
m_dlgMenu.m_strChoice.Format(menu_text_F2[x_F2]);<br />
<br />
m_dlgMenu.UpdateData(FALSE); <br />
<br />
control_nr = 2;<br />
<br />
}<br />
<br />
else if (control_nr == 2) {<br />
<br />
m_dlgMenu.ShowWindow(SW_HIDE)<br />
control_nr = 0;<br />
}<br />
<br />
.<br />
.<br />
.<br />
}<br />
Another thing is that it works as expected if the style of the other dialog is set to "child" instead of "popup" but when F2 is held down the dialog flickers a couple of times before it is shown , which is annoying. This is not the case when the style is "popup".
Thanks for your help
|
|
|
|
|
Hehe, got it to work
I placed a PreTranslateMessage() in the code for the other dialog and it called OnOK() when GetAsyncKeyState(VK_F2) == 0
|
|
|
|
|
hi , i m new n i just managed one clinet server prg but i am not able to connect multiple client to my server.
|
|
|
|
|
<--- maybe that's the problem!
Have you written your server to accept multiple clients?
Bare minimum, I imagine you'll at least need to keep a list/array of SOCKETs.
|
|
|
|
|
Probably should have sent him a McDonalds menu.
led mike
|
|
|
|
|
I actually had a filet-o-fish for lunch. What does this say about my programming "skills"?
Hanging my head in shame,
Mark
|
|
|
|
|
tks for help i am dont with my project
|
|
|
|
|
I'm sorry to ask such a simple question but my compiler is spitting out errors and dosen't recognize CString. I can't figure out what's wrong. String works fine though. I'm trying to use it for MFC. Here is the code:
include <iostream>
#include <string>
#include <cstring>
#include <cstddef>
#include <cctype>
using namespace std;
int main()
{
CString Test;
Test = "It Worked!";
cout<<Test;
cin.get();
return 0;
}
Thanks.
|
|
|
|
|
What errors on what lines?
Are these still the include files for VC 2005?
#include <cstringt.h> //MFC-only string objects
#include <atlstr.h> //Non-MFC string objects
|
|
|
|