DeviceNetworkInformation: Which is which? - windows-phone-7

I want to check whether any internet connection is available - i.e can I start webrequests expected to succeed.
IsCellularDataEnabled - is this true if there's GPRS/3G/etc available?
IsNetworkAvailble - is this true if VOICE CALLS are possible, or does this too have something to do with the internet?
IsCellularDataRoamingEnabled - Should I be concerned with this at all? (I know what Data Roaming is...)
IsWifiAvailable - If this false,I can still get internet from 3G.
So what I'm looking for is:
if (/*something*/){ //you can use the internet
}
thanks
(EDIT: I don't have a device readily available, otherwise I'd just try it out :) )

IsNetworkAvailable is true, if there is some kind of data connection available, no matter which (GPRS, 3G, Roaming, WiFi or via USB cable).
IsCellularDataEnabled is true if the phone is connected via a mobile data connection. It doesn't give you any status about Voice calls but only data.
IsCellularDataRoamingEnabled: The user is connected via a mobile data connection via a 3rd provider (roaming). You should only use a minimal amount of traffic because roaming data is often expensive for the user. (Because of that you can check this status)
IsWifiAvailable is true if you are connected via WiFi. If false there may be a mobile data connection via 3G and so on.
So, if you only want to know IF there is some kind of connection you can use IsNetworkAvailable - all other states are only giving you more detailed information about what kind of connection there is.

Only
if(NetworkInterface.GetIsNetworkAvailable()) {
}
http://msdn.microsoft.com/en-us/library/system.net.networkinformation.networkinterface.getisnetworkavailable.aspx

Your question contains a false assumption.
You want to know if there's a connection so your request is likely to succeed. Only the oppostie can be true though. You can only know that if there's no connection a request will definitely fail.
There are lots of reasons that it may not be possible to make a successful web request even if there's a connection to an external network available.
You MUST code to handle a request failing even if there is a connection.
Things that can stop a request from being successful even if there is a connection:
proxy servers or firewalls blocking the request
being connected to a local network which doesn't have access to the destination server
a slow network connection causing the request to timeout
the destination server being down/offline/unavailable
an error on the server
etc. ...

Related

How can I check I am actually connected to the internet

I am using Indy to connect to an Ftp server. We have had problems with the server being down, creating a "Host not found" error, which I handle and indicate to the user, however, today the software was installed on a computer that did not have an internet connection. The error produced when I "Connect" is also "Host not found", which is not totally correct. Is there a function/procedure I can call inside the TIdFtp component that will tell me if there is actually a connection to the internet so I can display an error indicating the user is not connected? I am using Delphi XE Seattle.
I have looked through the list of IdFtp properties/functions etc, but nothing seems obvious.
Is there a function/procedure I can call inside the TIdFtp component that will tell me if there is actually a connection to the internet so I can display an error indicating the user is not connected?
No. What you will have to do is attempt a TCP connection to several different servers and see if any of them succeed or if they all fail. For instance, you can use TIdTCPClient to connect to well-known server ports that are pretty much guaranteed to be online 99+% of the time, like google.com/yahoo.com on port 80 (HTTP) or 443 (HTTPS), etc.

why the NetworkInterface.GetIsNetworkAvailable() method always returns true,when i debug it on my windows phone 7 device?

why the NetworkInterface.GetIsNetworkAvailable() method always returns true,when i debug it on my windows phone 7 device?not emulator,is it sure that "NetworkInterface.NetworkInterfaceType==NetworkInterface.None" means a device does't get network access?how can i detect whether a device connected a network that has intenert access? i tried this link,Zune-Detection-and-Network-Awareness,but the NetworkInterface.GetIsNetworkAvailable() method always returns true,which stumbled me a lot.Thanks for your help.
Read the documentation for the method. Here is what MSDN has to say:
A network connection is considered to be available if any network
interface is marked "up" and is not a loopback or tunnel interface.
There are many cases in which a device or computer is not connected to
a useful network but is still considered available and
GetIsNetworkAvailable will return true. For example, if the device
running the application is connected to a wireless network that
requires a proxy, but the proxy is not set, GetIsNetworkAvailable will
return true. Another example of when GetIsNetworkAvailable will return
true is if the application is running on a computer that is connected
to a hub or router where the hub or router has lost the upstream
connection.
Generally, you could say that this method is generally prone to errors. I would recommend taking the approach of failed requests - have a couple of URLs to test against and check if HTTP/DNS resolve requests go through. If some of them pass - there is a connection. If all fail - chances are you are not connected.
Here is a sample.

Stale connection with Pheanstalk

I'm using beanstalkd to offload some work to other machines. The setup is a bit unusual, the server is on the internet (public ip) but the consumers are behind adsl lines on some peoples homes. So there is a linux server as client going out through a dynamic ip and connecting to the server to get a job. It's all PHP and I'm using pheanstalk library.
Everything runs smoothly for some time, but then the adsl changes the IP (every 24h hours the provider forces a disconnect-reconnect) the client just hangs, never to go out of "reserve".
I thought that putting a timeout on the reserve would help it, but it didn't. As it seems, the client issues a command and blocks, it never checks the timeout. It just issues a reserve-with-timeout (instead of a simple reserve) and it is the servers responsibility to return a TIME_OUT as the timeout occurs. The problem is, the connection is broken (but the TCP/IP doesn't know about that yet until any of the sides try to talk to the other side) and if the client blocked reading, it will never return.
The library seems to have support for some kind of timeouts locally (for example when trying to connect to server), but it does not seem to contemplate this scenario.
How could I detect the stale connection and force a reconnect? Is there some kind of keepalive on the protocol (and on the pheanstalk itself)?
Thanks!
You could try to close each connection right after the request is answered and reopen a new connection each time.
There is no close() function but you deleting the Pheanstaly Object with unset($pheanstalk) will close it.
This explanation is quite helpful:
Pheanstalk (PHP client for beanstalk) - how do connections work?
I haven't tried it yet, but I came up with the idea of connecting to the beanstalk server through an SSH tunnel. We can enable the ServerAliveCountMax and ServerAliveInterval options on the tunnel, so that a network or server failure will cause the tunnel to close. This should then cause the pheanstalk client to report an error.

Socket connection rerouting

Most proxy servers perform the job of forwarding data to an appropriate "real" server. However, I am in the process of designing a distributed system in which when the "proxy" receives a TCP/IP socket connection, the remote system actually connects with a real server which the proxy nominates. All subsequent data flows from remote to the real server.
So is it possible to "forward" the socket connection request so that the remote system connects with the real server?
(I am assuming for the moment that nothing further can be done with the remote system. Ie the proxy can't respond to the connection by sending the IP address of the actual server and the remote connections with that. )
This will be under vanilla Windows (not Server), so can't use cunning stuff like TCPCP.
I assume your "remote system" is the one that initiates connection attempts, i.e. client of the proxy.
If I get this right: when the "remote system" wants to connect somewhere, you want the "proxy server" to decide where the connection will really go ("real server"). When the decision is made, you don't want to involve the proxy server any further - the data of the connection should not pass the proxy, but go directly between the "remote system" and the "real server".
Problem is, if you want the connection to be truly direct, the "remote system" must know the IP address of of the "real server", and vice versa.
(I am assuming for the moment that nothing further can be done with
the remote system. Ie the proxy can't respond to the connection by
sending the IP address of the actual server and the remote connections
with that. )
Like I said, not possible. Why is it a problem to have the "proxy" send back the actual IP address?
Is it security - you want to make sure the connection really goes where the proxy wanted? If that's the case, you don't have an option - you have to compromise. Either the proxy forwards all the data, and it knows where the data is going, or let the client connect itself, but you don't have control where it connects.
Most networking problems can be solved as long as you have complete control over the entire network. Here, for instance, you could involve routers on the path between the "remote system" and the "real client", to make sure the connection is direct and that it goes where the proxy wanted. But this is complex, and probably not an option in practice (since you may not have control over those routers).
A compromise may be to have several "relay servers" distributed around the network that will forward the connections instead of having the actual proxy server forward them. When a proxy makes a decision, it finds the best (closest) relay server, tells it about the connection, then orders the client to connect to the relay server, which makes sure the connection goes where the proxy intended it to go.
There might be a way of doing this but you need to use a Windows driver to achieve it. I've not tried this when the connection comes from an IP other than localhost, but it might work.
Take a look at NetFilter SDK. There's a trial version which is fully functional up to 100000 TCP and UDP connections. The other possibility is to write a Windows driver yourself, but this is non-trivial.
http://www.netfiltersdk.com
Basically it works as follows:
1) You create a class which inherits from NF_EventHandler. In there you can provide your own implementation of methods like tcpConnectRequest to allow you to redirect TCP connections somewhere else.
2) You initialize the library with a call to nf_init. This provides the link between the driver and your proxy, as you provide an instance of your NF_EventHandler implementation to it.
There are also some example programs for you to see the redirection happening. For example, to redirect a connection on port 80 from process id 214 to 127.0.0.0:8081, you can run:
TcpRedirector.exe -p 80 -pid 214 -r 127.0.0.1:8081
For your proxy, this would be used as follows:
1) Connect from your client application to the proxy.
2) The connection request is intercepted by NetFilterSDK (tcpConnectRequest) and the connection endpoint is modified to connect to the server the proxy chooses. This is the crucial bit because your connection is coming from outside and this is the part that may not work.
Sounds like routing problem, one layer lower than TCP/IP;
You're actually looking for ARP like proxy:
I'd say you need to manage ARP packets, chekcing the ARP requests:
CLIENT -> WHOIS PROXY.MAC
PROXY -> PROXY.IP is SERVER.IP
Then normal socket connection via TCP/IP from client to server.

What is the difference between "ORA-12571: TNS packet writer failure" and "ORA-03135: connection lost contact"?

I am working in an environment where we get production issues from time to time related to Oracle connections. We use ODP.NET from ASP.NET applications, and we suspect the firewall closes connections that have been in the connection pool too long.
Sometimes we get an "ORA-12571: TNS packet writer failure" error, and sometimes we get "ORA-03135: connection lost contact."
I was wondering if someone has run into this and/or has an understanding of the difference between the 2 errors.
Using a mobile phone analogy:
ORA-12571 (Failure) Means call is dropped.
ORA-03135 (Connection Lost) Other party hung up.
My understanding is that 3135 occurs when a connection is lost. This doesn't tell you why the connection was lost, though. It may have been terminated by the server because the server failed to recieve a response to a probe for a certain amount of time, and assumed that the connection was dead. Or (I'm not sure about this) the exact reverse of that: the client failed to recieve a probe response from the server for a certain amount of time, so it assumed the connection was lost. The "certain amount of time" is cotrolled by SQLNET.EXPIRE_TIME=[minutes] in sqlnet.ora.
As for 12571, my (again vague) understanding is that there was a sudden failure to send a packet during communication with the server, and that this is typically caused by some software or hardware interfering with the connection (either by design, or by error). For instance, if you pull out your ethernet cable and then try to execute a query, you'll probably get this. Or if a firewall or anti-malware application decides to block the traffic.

Resources