|
Where are you checking for this value at?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
I setup a function thread to loop over and over to send request from the server as long as Connected=true; once thats set to true the function starts to send request. I made a simple ofstream function to test whether or not it works if the connection was severed.
<br />
ofstream out;<br />
out.open("error.txt",ios::app);<br />
out << WSAGetLastError << endl;<br />
out.close();<br />
Soon as i turned the server off it made the file with error code 183 in it. (Several times too because of the loop)
I need to know how to use this in a if statement.
|
|
|
|
|
dellthinker wrote: I setup a function thread to loop over and over to send request...
If no error occurs, send() returns the total number of bytes sent, which can be less than the number indicated by len for nonblocking sockets. Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling WSAGetLastError() .
dellthinker wrote: out << WSAGetLastError << endl;
You do realize that this will print the address of WSAGetLastError() rather than actually call the function, don't you?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Ok, and error code 183 was returned.
<br />
if(WSAGetLastError==183){<br />
}<br />
That doesnt work. If 183 is some code indicating that the data sent was lots and or not sent then there should be a way to use this in a if statement. Correct?
|
|
|
|
|
dellthinker wrote: if(WSAGetLastError==183){
You do realize this compares the address of WSAGetLastError() to 183 rather than actually call the function, don't you?
dellthinker wrote: That doesnt work. If 183 is some code indicating that the data sent was lots and or not sent then there should be a way to use this in a if statement. Correct?
Your question is really confusing. Do you have something akin to:
while (more_data_exists)
{
int nRet = send(...);
if (SOCKET_ERROR == nRet)
DWORD dwError = WSAGetLastError();
}
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
DavidCrow wrote: our question is really confusing. Do you have something akin to:
while (more_data_exists)
{
int nRet = send(...);
if (SOCKET_ERROR == nRet)
DWORD dwError = WSAGetLastError();
}
Ok let me be more specific. Im going to go step-by-step of what im trying to do here.
If a program that is 'connected' to a server all of a sudden gets disconnected for whatever reason it has to do on the server side, im trying to get it 'reconnect'. In order for me to do that im looking for whatever error WSAGetLastError might spit out. Compare that error with an if statement and do whatever is necessary to reconnect to the server. Understand me now?
|
|
|
|
|
Here's my usual test for socket closure. Once you detect the socket closure, set some flag and exit from your routine. In your main program, check for that flag and call your connect code. I wouldn't put it inside the if/else code block - connection code is too problematic and time-consuming.
iResult = send (m_hSocket, pBuffer, iLength, 0);
if (iResult == SOCKET_ERROR)
{
iResult = WSAGetLastError ()
if ((iResult == WSAENETDOWN) ||
(iResult == WSAENETRESET) ||
(iResult == WSAECONNABORTED) ||
(iResult == WSAETIMEDOUT) ||
(iResult == WSAECONNRESET))
{
}
else
{
}
Judy
|
|
|
|
|
dellthinker wrote: In order for me to do that im looking for whatever error WSAGetLastError might spit out. Compare that error with an if statement and do whatever is necessary to reconnect to the server. Understand me now?
Yes, it makes sense, but still does not explain why you are having trouble comparing the return value from WSAGetLastError() with some constant, like:
DWORD dwError = WSAGetLastError();
if (183 == dwError)
... Furthermore, error 183 equates to ERROR_ALREADY_EXISTS , which I doubt your socket functions are returning when a disconnect has happened.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
In an MFC app on VS C++ 2005, I need to process and close files on a USB drive before it is pulled out. My plan is to do this processing when DBT_DEVICEQUERYREMOVE message is broadcast, and to return a TRUE after I have finished processing files on the USB drive.
I have handled WM_DEVICECHANGE and am getting DBT_DEVICEREMOVECOMPLETE messages. But I am not getting DBT_DEVICEQUERYREMOVE messages despite registering. What do I need to do to get those messages? Am I passing the wrong handle or GUID? I'm at my wit's end here, I've been stuck for a day now!
I handled WM_DEVICECHANGE in my message map in MainFrm.cpp like this:
ON_MESSAGE(WM_DEVICECHANGE, &CMainFrame::OnMyDeviceChange)
This function, stripped down, is:
LRESULT CMainFrame::OnMyDeviceChange(WPARAM wParam, LPARAM lParam)
{
LRESULT lpReturn = TRUE;
if (wParam == DBT_DEVICEQUERYREMOVE)
{
PDEV_BROADCAST_HDR pHdr = (PDEV_BROADCAST_HDR)lParam;
switch( pHdr->dbch_devicetype )
{
case DBT_DEVTYP_DEVICEINTERFACE:
{
}
case DBT_DEVTYP_HANDLE:
{
}
case DBT_DEVTYP_OEM:
{
}
case DBT_DEVTYP_PORT:
{
}
case DBT_DEVTYP_VOLUME:
{
PDEV_BROADCAST_VOLUME pDevVolume = (PDEV_BROADCAST_VOLUME)pHdr;
MessageBox("The user wants to remove this USB drive.");
lpReturn = TRUE;
break;
}
}
}
return lpReturn;
}
Here is how I am calling my registration function in OnCreate() in my MainFrm.cpp:
HDEVNOTIFY hDevNotify = NULL;
GUID InterfaceClassGuid={0xA5DCBF10L, 0x6530, 0x11D2, 0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED};
cHF.DoRegisterDeviceInterface(this->GetSafeHwnd(), InterfaceClassGuid, hDevNotify);
Here is where I am trying to register to get the DBT_DEVICEQUERYREMOVE messages for USB Drives, in my HelperFunctions.cpp:
BOOL CHelperFucntions::DoRegisterDeviceInterface(HWND hWnd, GUID InterfaceClassGuid, HDEVNOTIFY hDevNotify)
{
char szMsg[80];
DEV_BROADCAST_DEVICEINTERFACE NotificationFilter;
ZeroMemory( &NotificationFilter, sizeof(NotificationFilter) );
NotificationFilter.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
NotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
NotificationFilter.dbcc_classguid = InterfaceClassGuid;
hDevNotify = RegisterDeviceNotification(hWnd, &NotificationFilter, DEVICE_NOTIFY_WINDOW_HANDLE);
if(!hDevNotify)
{
wsprintf(szMsg, "Could not register to receive notifications about drive events: %d\n",
GetLastError());
MessageBox(hWnd, szMsg, "RegisterDeviceNotification failed", MB_OK);
return FALSE;
}
return TRUE;
}
So what am I missing here? Any ideas as to why am I not getting the DBT_DEVICEQUERYREMOVE notification I am getting the DBT_DEVICEARRIVAL (0x8000) and DBT_DEVICEREMOVECOMPLETE (0x8004), so it must be something wrong with my registering for the DBT_DEVICEQUERYREMOVE notification?
Any help would be much appreciated. Thanks!
--
Omne Ignotum Pro Magnifico
-- modified at 18:32 Wednesday 10th October, 2007
|
|
|
|
|
can anyone code the VRP problem using genetic alogrithms in c++.
i need that to be done as a report along with code to submit for my exam.
kindly help me in this work
|
|
|
|
|
At least this post is on the correct board.
For more info, see here[^]
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
|
What are you wanting such that memset() does not work?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
|
DQNOK wrote: I'm looking to fill memory with a pattern...
I guess it all depends on what the pattern is. Anything beyond a single byte becomes application-specific, so I would think a nonexistent function would be normal.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
OK. Thanks for your thoughts and replies.
My algorithm is not application specific (that I can see). I'll go ahead and post it here in case anyone else can benefit from it. It looks long, but it's mostly comments. The test case at the end works fine for me when I compile and run it with Digital Mars C++.
#include <string.h>
void memoryfill( void* target, char* pattern, size_t ptrnlen, size_t numRepeats )
{
char* trgt = (char*)target;
if( 0 == numRepeats )
{
;
}
else if( 1 == ptrnlen )
{
memset(target, *pattern, numRepeats);
}
else
{
memcpy(trgt, pattern, ptrnlen);
int snglbit=1;
int thisCnt = numRepeats >> 1;
while( thisCnt > 0 )
{
memcpy(trgt+ptrnlen*snglbit, trgt, ptrnlen*snglbit);
snglbit <<= 1;
thisCnt >>= 1;
}
char* endptr = trgt + ptrnlen*snglbit;
while( (snglbit >>= 1) > 0 )
{
if( snglbit & numRepeats )
{
memcpy(endptr, trgt, ptrnlen*snglbit);
endptr += ptrnlen*snglbit;
}
}
}
return;
}
<br>
#include <stdio.h>
<br>
int main()
{
char buf[1024];
memset(buf, 0, 1024);
memoryfill( buf, "Hello World_", 12, 19 );
printf("\n%s", buf);
}
|
|
|
|
|
This reminds me of how I create large test files. For example, if I wanted a file with 100,000 characters, I would open Notepad, type 10 characters, copy those and paste nine times (yielding 100 characters). Then I would copy those 100 characters and copy those nine times (yielding 1000 characters). I would repeat this two more times to get the final result, all from an initial 10 typed characters.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
That's exactly it, only base-2 instead of base-10.
|
|
|
|
|
Well, probably not the answer you were looking for but if you really want to do this in the most efficient way possible you are going to need to use assembly language and take advantage of the chip specific instructions. I have done this before for some built-in-self-tests for embedded hardware where the timing requirements were that all of the tests had to complete in under 1 clock cycle.
|
|
|
|
|
Thanks for the response. I'm sure you are correct if efficiency is the driving force.
I figured the best approach would be one that does a lot of what memcpy() already does: deals with all possible alignment issues, and once those are out of the way, uses the fastest native size (2, 4, or 8 bytes, depending on the processor) to do the guts of the transfer. Clearly, this is assembly language/processor specific kinds of stuff. Nevertheless, I had thought perhaps someone already had some good cross platform algorithm that would at least do the alignment stuff, then assume int was best for the rest of the transfer.
My approach is pretty good, except that it forces repeated dealing with alignment issues on every single call to memcpy(). Fortunately, it calls memcpy() a max of 63 times. I haven't spent many hours pondering this, but it MIGHT be that there is just no way around repeatedly dealing with alignment issues. If this is the case, then perhaps my algorithm is already optimum. I really don't know.
Thanks again for the response.
David
|
|
|
|
|
You might want to look for an open implementation of memcpy and then create your own version that does the same thing but excludes the alignment stuff, which you could then do once as a separate call. I don't know if the Rotor source code includes an implementation of memcpy for use on non-Windows systems, but it might be a place to start. (I know it does have other low-level C functions that have been reproduced, just not sure if memcpy is one of them.)
|
|
|
|
|
Interesting! Clever idea.
I probably won't do it just because I already have a workable solution, and don't want to spend the kind of time it would require. I was really just fishing, assuming someone out there already had an optimum portable algorithm. For my application, the maximum numRepeats will only be on the order of a few hundred, so the looping won't be severe; for sure won't justify the amount of time I'd spend writing an optimum algorithm. See http://www.codeproject.com/tips/optimizationenemy.asp?df=100&forumid=683&mpp=50&select=2267125&msg=2267125[^]
Thanks again.
David
|
|
|
|
|
|
I took a quick look at the source. It doesn't do any alignment; just the straight-forward
*(target++) = *(src++);
kind of thing. I suppose they're relying on the compiler to recognize the pattern, and substitute the appropriate idiom for memory copy on the selected platform. I guess if the compiler is smart enough, that's a good; well, a *portable* way to do it...
|
|
|
|
|
Ahh...I didn't really look at the source other than to verify that it was actually a memcpy implementation. Oh well...
|
|
|
|
|