Why is my UDP saying it is connected when it is not? - vb.net

My issue is is that my UDP client is reporting that it is connected when I have nothing on the network. I'm trying to connect to a PLC module with IP 10.10.10.10 at port 7775 from a computer port of 7777. The Connect() function from the client is working, even though there is no Ethernet or wireless connection turned on.
The core of my problem revolves around my connection code I believe
Public Sub Connect()
Try
If (plcType = 0) Then
udpClient = New UdpClient(7777)
udpClient.Client.Connect(IPAddress, port)
SendCommand("ME")
End If
Catch
MsgBox("Could not connect to the controller, please check IP address." & vbCrLf & Err.Description)
'Connect()
End Try
End Sub
If I insert a break point after the Connect() command I get extremely strange behavior. After the break point is hit if I look into udpClient.Client's properties it says it is connected, but if I leave those properties and then look at it again it will suddenly change and say it is not connected and the program will correctly read that it is not connected after that point.
This issue only appeared After I connected a second computer and attempted to run the same program on it to the same device. Neither computer can correctly talk to the device anymore, that I can understand, but I can't understand why one of my computers believes that it is talking to the device when it clearly isn't.
Thank you for any help.
Update: Any connection available will trigger this condition, only if all networks are disable or disconnected will it behave as expected. However, it is still reporting as connected when there is no device on the network of that IP.

So #CodeCaster is right saying that "UDP has no notion of being connected". UDP is a connectionless fire-and-forget protocol. When you send a datagram, you have no guarantee it will reach its destination.
Different "connect" operations that are available across a multitude of languages merely mean "ok, we'll use this socket for talking to this specific address and port". This knowledge does not translate to packets being sent over the network. It is a local thing that allows you to use send function instead of sendto.
If you want to know if there's a device on your network listening to a specific IP and UDP port, then you're going to need your own synchronizing mechanism. The protocol does not support that out of the box. So examples are:
Heartbeat. Let your remote peer send a special multicast message every 5 seconds. If you haven't received a heartbeat message in 15 seconds, assume the remote peer is down. Adjust numbers at your own discretion.
Ping-pong, or echo request - echo response. Periodically send a special datagram to the remote peer and expect a response.
Handshakes, acknowledges, retries... The more robust channel you need, the more it is going to remind TCP. This is a kind of a sign to switch protocols!

Related

Windows Phone: How to determine if application is connected to the internet when it starts

Is there a way to find out if my windows phone application is connected to the internet as soon as it loads.
To be clear, I'm not interested in listening to Network Changes. I just want to know what the connection status is at one point in time.
You could use NetworkInformation.GetInternetConnectionProfile(), which will return null if the device is not connected to the internet.
There is the NetworkInterface.GetIsNetworkAvailable() method which will return true if a network connection is available. With the caveat that:
A network connection is considered to be available if any network interface is marked "up" and is not a loopback or tunnel interface.
There is no guarantee that your remote host will still be reachable, even if you are connected to The Internet

Can i use Local ICE Candidates as indications of connectivity?

My task is to determine the connectivity of a single browser client. I was wondering if the emitted Local ICE Candidates are any indication to the client's connectivity. So examining a candidate originating from google's STUN server:
mid: audio, candidate: a=candidate:941443129 2 udp 1845501695 2.84.19.92 49669 typ srflx raddr 192.168.1.2 rport 49669 generation 0
I can see my public ip 2.84.19.32 and i can also view my local, behind the NAT, ip 192.168.1.2. At this point i also know that the browser is listening on UDP port 49669 and waiting for connections. Can i infer at this point that the STUN client has made a successful connection to my public ip and port?
I understand it is ICE's job to determine if a connection is possible to that Candidate, but is it possible to determine connectivity with a single client (browser)?
My ultimate question / task, is to tap into the ICE process and know for a fact if the [single] client is behind a NAT and what type of NAT that is. Is that possible?
The presence of candidates doesn't give the information that you have connectivity. But if you analyze the content of them, you can conclude some things.
You need to take into account what STUN/TURN servers you are using.
If nothing is defined you can still have candidates, if the device is connected in a network and has an IP attributed (even if the network has no access to the Internet), you will only have candidates with local IP address.
If you define a server, and you get candidates with other types than host, it means that you have connection (at least connection to the defined STUN/TURN server, that is supposed to be outside of your machine).
To know if you are behind a NAT, you can check the presence of host candidates with local IP's.
After you get a candidate, it means that that host:ip is accessible from the internet. That's the whole idea of contacting a external STUN/TURN server. Is to open that ports... But, the fact that they are accessible, doesn't mean that you will receive attacks. You can in fact receive, but that is where the key negotiation enters and checks that only other peer (the ones with the keys) can "establish a connection" to that host:port
There are two ways to get this data out of the webRTC connection object:
pc.iceConnectionState, this tells you the connection process
pc.iceGatheringState, this is about the 'getting' of the ice candidates
But both don't really seem to work, as I am behind a firewall which blocks the RTC, but pc.iceConnectionState still gives me connected, but you might give it a go and see if it works for you.

PeerConnection based on local IP's

What I want is, basically, to create a connection between two different computers on same local network. But i want to do this by computers' local IP's. (like 192.168.2.23 etc)
This must be a totally local connection. no TURN or STUN Servers. I am not sure if this is possible. Because there are not much documentation/example/information about WebRTC.
So, how can I create a connection from my computer to another one just passing its local IP as parameter?
Update: To be more clear; imagine there is an html page contains some code that activates my camera and audio services. and another -almost same- page is open in other computer. Waiting a connection request... And there is a textbox in my page to type an IP belongs to other computer on my local network. type 192.168.2.xx and bingo! i have connection between me and other computer.
I want this process as IP based, because there may be more than 2 devices on the network. And all of them are possible devices to create connection. So i need to reach them by their IP's.
Any example code or explanation would be great! even if it tells that this is not possible.
Thanks
Peer discovery is a vital part in any WebRTC application. It's an expensive term for saying: "Hi, I'm computer 4 and I want to talk to you!".
See it as calling a friend over the phone. You need to dial his number first.
This part is not defined in the WebRTC standards. You need to implement this logic in your application. Once you know who you want to call, you need a way of exchanging vital information. This is called signaling, like flo850 put in his answer.
Signaling is needed before any peer-to-peer connection can be set up.
To come up with an idea for your use case of 7 devices in a LAN.
If you have these devices connected to for example a WebSockets server and are in the same channel.
The WebSockets server can be written to route messages to specific receivers.
Devices connected to the channel often are identified with some kind of ID, imagine you use the device's IP.
When you want to talk to computer 4 with IP 192.168.0.4 you send the exchange messages (signaling) on the channel to the receiver with ID, the IP of the device you want to connect with.
How to send the signaling (offer, answer) is described here with example code.
Hope this helps
Users usually sit behind NATs; that's why ICE concept implemented in WebRTC.
If both users are sitting behind same NAT; you can skip ICE servers by passing "NULL" parameter value over "RTCPeerConnection" constructor:
var peer = new [webkit|moz]RTCPeerConnection ( null );
Now, browser will use "host" candidates, also known as "local" candidates.
you still need a signaling server. During the ICE candidate search, your clients will exchange their local ip through this signaling server

How do I ping a server port with iOS 5 SDK

Basically, I want to check to see if a game server is online/offline but the server is hosted through a port, how would I go about pinging it to check this. For testing purposes the server I want to ping is fr7.mooshroom.net:25667, however eventually I will be importing the server IP and port from an online plist so could the ip address and port be separated
If you could give me a step by step guide on how to do this, that would be much appreciated.
I am using the Reachability by tonymillion because the apple one doesnt work with iOS 5.
First, it depends on if you are talking about TCP or UDP.
If it's TCP, then the answer may be simple: try to open a TCP connection to the server at that port. If the connection opens, drop the connection and report success. Otherwise, failure. This is the simplistic view. It's possible if there are load balancers or firewalls in front of the server, the TCP connection may open but the backend server is down.
UDP would be harder. There is no way to know when you send UDP data that the server got the data unless it sends you some kind of response. It's possible that if the UDP server is down and you sent a message to the port, your computer might get an ICMP Error message back. That would definitely let you know the server is down. But firewalls may block this message getting back to your device, so that might not be reliable.
Otherwise, you need to send a properly formatted message to the server to get some kind of response. This is protocol-dependent but is the most reliable.
The traditional "ping" message is an ICMP echo query and response message. As such, there is no "port" associated with ICMP.

UDP: How does the client know its server?

I'm sniffing between two devices which communicate in a duplex fashion via udp. I'm using wireshark to sniff. The config file for the first device (a piece of hardware under test) states that the client port is 54718 and the server is 54717. In the config file for the second device (a simulator written years ago), only one port is specified. That of 54718.
The two devices communicate without any problems.
But how does the second device manage to connect and send to 54717 when it has no knowledge of it?
In wireshark I can see that the first device is sending to the second device such that the source port is 54717 and the destination is 54718. I can also see that the second device is sending to the first device such that the source port is 54718 and the destination is 54717.
The first device sends first and the protocol is described as that of UDP in Wireshark. The simulator replies, also via UDP. Subsequent exchanges are described as being STUN ChannelData TURN Messages. I've no knowledge of this protocol but maybe it explains why I don't see 54717 in the simulators config file.
Thanks for your help,
Barry
First, in UDP communication, there is no "connect" action. UDP is not connection-oriented.
Second, the second device will get the peer address and port from recvfrom() api call.
In all probability, the first device's use of the terminology "client port" and "server port" do not refer to two different ports within the client device. Instead, the "client port" refers to the port to be used as the point of origin within the first device, and the "server port" refers to the remote destination port on the far device, to which the first device's outgoing traffic will be sent.
The second device, on the other hand, is probably fundamentally a "listening" device. It only knows the UDP port it needs to listen on, and waits for any queries destined to that port to arrive from anywhere.
So, I will refer to the "first device" as the client, and the "second device" as the server.
Each datagram sent from the client to the server contains two sets of address information:
1) The destination IP address and port, and
2) The return IP address and port.
The server can use recvfrom() to extract the complete return address (including port number) from each incoming request.
This way, we really only need one port number to be predfined and agreed upon by both the server and the client ahead of time: The server's port number.
The client could conceivably choose to use any random port number as its origin port (but by convention it would likely choose to avoid any of the well-known reserved ports to avoid potential interoperability problems), and the server could dynamically read the return address information from each incoming request and send its responses to the correct destination dynamically.
But how does the second device manage to connect and send to 54717 when it has no knowledge of it?
UDP is connectionless, and your program likely gets the 54717 as a default fallback value if nothing else is specified (e.g. in a config file).