|
Ok,
I have finally found a documented alternative: mktime .
Best regards.
Fred.
There is no spoon.
|
|
|
|
|
I am using the following function to find out whether I can connect to a particular IP address(for ex:3.205.202.6 etc...) in VC++.
This function returns TRUE on Windows 2000 if the given IP address is communicating one. But on Windows XP system it always returns FALSE (However I can still say ping to this IP :- 3.205.202.6 from DOS command prompt. It replies correctly).
Can anyone please help me to root cause this issue in Windows XP system. Is this socket API not valid on Win XP system? On Win2000, it always gives me the correct status.
BOOL IsOnline(const CString &strIPAddr)
{
SOCKET tmpSocket;
struct sockaddr_in SClient;
u_long argp;
fd_set stReadFDS;
fd_set stWriteFDS;
fd_set stExceptFDS;
struct timeval stTimeOut;
int nSelectReturn;
DWORD nTicks;
DWORD nTickStart;
//create a socket
tmpSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP) ;
if (tmpSocket == INVALID_SOCKET)
{
return FALSE;
}
// make the socket non blocking
argp = 1L; // non zero enables nonblocking mode
if (ioctlsocket(tmpSocket, FIONBIO, (u_long FAR *) &argp) == SOCKET_ERROR)
{
closesocket(tmpSocket);
return FALSE;
}
// connect to the given IP address
SClient.sin_family = AF_INET;
SClient.sin_port = htons(502);
SClient.sin_addr.s_addr = inet_addr(strIPAddr);
int nLastErr;
if (connect(tmpSocket, (sockaddr *) &SClient, sizeof(sockaddr_in)) == SOCKET_ERROR)
{
nLastErr = WSAGetLastError();
if (nLastErr != WSAEWOULDBLOCK)
{
closesocket(tmpSocket);
return FALSE;
}
nTicks = 500;
nTickStart = GetTickCount();
nSelectReturn = SOCKET_ERROR;
int ic = 0;
while (nSelectReturn <= 0 && (GetTickCount() - nTickStart) < nTicks)
{
// clear all sockets from FDS structure, then put our socket into the socket descripter
FD_ZERO(&stReadFDS);
FD_ZERO(&stWriteFDS);
FD_ZERO(&stExceptFDS);
FD_SET(tmpSocket, &stReadFDS);
FD_SET(tmpSocket, &stWriteFDS);
FD_SET(tmpSocket, &stExceptFDS);
// set a timeout of 1 second
stTimeOut.tv_sec = 1;
stTimeOut.tv_usec = 0;
nSelectReturn = select(-1, &stReadFDS, &stWriteFDS, &stExceptFDS, &stTimeOut);
if (nSelectReturn == SOCKET_ERROR)
{
nLastErr = WSAGetLastError();
if (nLastErr != WSAEINPROGRESS)
{
closesocket(tmpSocket);
return FALSE;
}
}
}
if (nSelectReturn <= 0 || stWriteFDS.fd_count == 0)
{
closesocket(tmpSocket);
return FALSE;
}
}
closesocket(tmpSocket);
return TRUE;
}
Thanks
|
|
|
|
|
Check your Firewall settings
Greetings from Germany
|
|
|
|
|
Setttings are the same in both....
|
|
|
|
|
The only thing I see right away is that your timeout value is pretty short. Half a second...
Factor in the accuracy of clock ticks and it often could be much shorter.
For what it's worth, here's an alternative method using an event instead of looping (which is
inefficient use of CPU!)...
tmpSocket = ::socket(PF_INET, SOCK_STREAM, IPPROTO_TCP) ;
if (tmpSocket != INVALID_SOCKET)
{
BOOL fConnectSuccess = FALSE;
HANDLE hNetEvent = ::CreateEvent(NULL, true, false, NULL);
::WSAEventSelect(tmpSocket, hNetEvent, FD_CONNECT);
sockaddr_in SClient;
SClient.sin_family = AF_INET;
SClient.sin_port = htons(502);
SClient.sin_addr.s_addr = inet_addr(strIPAddr);
if (SOCKET_ERROR == ::connect(tmpSocket, (sockaddr *) &SClient, sizeof(sockaddr_in)))
{
int rc = WSAGetLastError();
if (rc == WSAEWOULDBLOCK)
{
if (WAIT_OBJECT_0 == ::WaitForSingleObject(hNetEvent, 10000))
{
WSANETWORKEVENTS WsaNetworkEvents;
if (0 == ::WSAEnumNetworkEvents(tmpSocket, hNetEvent, &WsaNetworkEvents))
{
if (0 != WsaNetworkEvents.iErrorCode[FD_CONNECT_BIT])
{
}
else
{
fConnectSuccess = TRUE;
}
}
}
else
{
}
}
else
{
}
}
else
{
fConnectSuccess = TRUE;
}
::closesocket(tmpSocket);
::CloseHandle(hNetEvent);
return fConnectSuccess;
}
else
{
return FALSE;
}
-- modified at 13:42 Wednesday 23rd May, 2007
added cleanup for event handle
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
|
|
|
|
|
Hi,
Having a problem in parsing XML. I am MSXML 4.0 api to parse a custom defined xml file which store different data related to a specific trace. It is like this
<DataSet>
<LG1>
<LG2>
<LG3>
........
</DataSet>
No. of child element under DataSet is unknown. It varies with the no. of data available during save.
Here I can't use selectNodes api call with parent element (MSXML2::IXMLDOMElement) to collect all child element starts with LG. Is there any way to solve this problem. Can I create a element list containing all element start with "LG"
Thanks in advanve....
Regards
Sandipan.
Sandip
|
|
|
|
|
If you can modify the XML file, would'nt it be nicer to make it in this way?
<DataSet>
<LG>
<1>
<2>
...
</LG>
</DataSet>
Then you could search for the LG node and get all the childs directly...
Hope this helps.
|
|
|
|
|
Dear joan,
Thanks for your candid advice. But the problem is like that only and I am looking for a way out to solve it. But anyway thanks for the reply.
Sandip
|
|
|
|
|
Well, the saving function should put in a tag into the stream that indicates the number of nodes.
Can you use simple text processing to get at the data? There have been very few situations like this where I could not quickly skip through the data using something simple as memory-mapping the XML file and walking through the file data. Hell, for your example above, even using _tcsstr( cpXMLStream, _T( "<LG" ) ) would likely work allowing you to get at each LG node...
Peace!
-=- James Please rate this message - let me know if I helped or not!<HR> If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! See DeleteFXPFiles
|
|
|
|
|
I'm trying to register a DLL created by Visual Studio 2005 in Debug Mode.It fails with the return code as 0xc0000005.
What is the prime reason for this error?.I have full administrative rights on my machine. Also, the Release DLL registration succeeds for the Same project...
Rane
|
|
|
|
|
Rane wrote: It fails with the return code as 0xc0000005.
Does it fail on your development machine, or another machine?
|
|
|
|
|
On all Machines...
Rane
|
|
|
|
|
Use dependency walker, Depends.exe ("Program Files\Microsoft Visual Studio\Common\Tools\DEPENDS.EXE") for checking the dependency of your dll. Provide all the dependent dlls. Hope this would serve your purpose.
Regards,
Paresh.
|
|
|
|
|
Rane wrote: I'm trying to register a DLL created by Visual Studio 2005 in Debug Mode.It fails with the return code as 0xc0000005.
Are you using regsvr32.exe ?
"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
|
|
|
|
|
Well I am working on VC++ and I wish to develop an application that creates an xml file. Can anybody guide ??
-- modified at 2:38 Wednesday 23rd May, 2007
|
|
|
|
|
kunal.tawde wrote: Well I am working on VC++ and I wish to develop an application taht creates an xml file. Can anybody guide ??
MSXML[^
Look up this[^] too.
|
|
|
|
|
|
Well thanks for tyour precious guidance..
|
|
|
|
|
|
Thanks a lot for your priceless guidance. I will check out the link you sent and I feel that it will work for me.
Once again thanks a lot.
|
|
|
|
|
|
I am trying to create a function that takes a pointer as a parameter that will end up being an array. The function returns the number of elements that will be in the array. I'm having a problem with the allocating part in the function. Here is what I have:
<br />
struct FooStruct<br />
{<br />
int aFoo, bFoo;<br />
}<br />
<br />
int GetFooList(FooStruct *pFoos)<br />
{<br />
<br />
pFoos = new FooStruct[MaxFoos];<br />
<br />
return MaxFoos;<br />
}<br />
My function works, but the problem comes from the calling function. If I create a pointer to a FooStruct and pass that, it does not get updated with the address that the new comes back with. I still don't fully grasp pointers (just the basics) so I may be going about this the wrong way.
I would imagine that this would be a standard situation with programming (calling a function to get an unknown number of items returned), but I don't know what this would be called so my searches have not come up with anything useful. Is this a good way to achieve what I am looking for, or am I wandering down the wrong path?
Any suggestions or pointers (sorry for the pun) on ways to accomplish this would be greatly appreciated!!
|
|
|
|
|
hpjchobbes wrote: My function works, but the problem comes from the calling function. If I create a pointer to a FooStruct and pass that, it does not get updated with the address that the new comes back with. I still don't fully grasp pointers (just the basics) so I may be going about this the wrong way.
That is the standard behavior. To make things easier, see pointers as being a value (the value is the address of a certain memory area). Thus you are in fact passing a value to a function, and the function will make a copy of this value for itself. You create a new array and you store this new value in the copy. Thus, your calling function will never be updated.
To solve this 'problem', pass the pointer by reference. This instruct your function that it must work on the variable and not make a copy of it:
int GetFooList(FooStruct* &pFoos)
|
|
|
|
|
In short:
int GetFooList( FooStruct **ppFoos ) {
...
*ppFoos = new FooStruct[MaxFoos];
return(MaxFoos);
}
FooStruct *pFoos = NULL;
int nFoos = GetFooList(&pFoos);
In long:
A pointer is just another data type like an integer (char, short, int, long, long long) or real (float, double).
So, turn the function around, what if you wanted to return the pointer and update the size as a parameter:
FooStruct* GetFooList( int *nFoos ) {
...
FooStruct *pfs = new FooStruct[MaxFoos];
*nFoos = MaxFoos;
return(pfs);
}
int nFoos = 0;
FoosStuct *pFoos = GetFooList(&nFoos);
Don't get wrapped up by the fact that a pointer data type is defined with a *.
[EDIT]
Or, as Cedric pointed out, use references.
I was trying to stay with a pointer based explaination.
[/EDIT]
...cmk
Save the whales - collect the whole set
|
|
|
|
|
Well, the reason of failure is; "I still don't fully grasp pointers (just the basics)"
The problem is that you pass the pointer by value; IOW the actual pointer gets copied and the very copy is passed to the function. Inside of the function, you assign the address [returned by new] to the copy of the original pointer and thus the original pointer will not be affected.
To make it work as you expect, make the following changes in the function prototype;
int GetFooList(FooStruct *& pFoos) {
...
}
Noticed? Now you pass the pointer by its reference.
--
=====
Arman
|
|
|
|