How to broadcast Video using UDPCLient Class in C# over internet? - c#-4.0

I am trying to develop a Video Client/functionality that captures video using webcam and transfers to other servent (server-client) somewhere on the internet. I am using UDPCLient Class to do that.
I want my application to be able to listen and tarnsmit video captured from webcam. The capturing, transmission and receiving works fine when i do that on local network.
But when i test the application from behind router (across two differnt networks/internet) after forwarding respective ports, the internet connectivity is lost on both routers (They hang up or something) and i need to restart the routers or switch to an alternate connection. The configuration is as follows:
Servent 1 <--> Router1 <--> Internet Connection#01
Servent 02 <---> Router2 <---> Internet Connection#02
Both connections are on separate DSL Line. One of the routers is ZTE brand and the other is of Netgear.
Code for listenning/transmission is as follows:
private void StartSockets()
{
//For testing across internet i use IPAddress obtained via different function
var IPAddress = getMyIpAddress();
this.udpSender = new UdpClient(IpAddress, 4000);
this.udpListener = new UdpClient(4000);
}
private IPAddress getMyIpAddress()
{
IPAddress localIP ;//= AddressAr[0];
localIP = IPAddress.Parse(GetPublicIP());
return localIP;
}
public string GetPublicIP()
{
String direction = "";
WebRequest request = WebRequest.Create("http://checkip.dyndns.org/");
using (WebResponse response = request.GetResponse())
{
using (StreamReader stream = new StreamReader(response.GetResponseStream()))
{
direction = stream.ReadToEnd();
}
}
//Search for the ip in the html
int first = direction.IndexOf("Address: ") + 9;
int last = direction.LastIndexOf("</body>");
direction = direction.Substring(first, last - first);
return direction;
}
Code for receiving response is as follows:
private void ReceiveData()
{
//For testing across internet i use IPAddress obtained via different function
var IPAddress = getMyIpAddress();
IPEndPoint ep = new IPEndPoint(IPAddress, myPort);
try
{
byte[] receiveBytes = this.udpListener.Receive(ref ep);
this.OnReadImage(new ImageEventArgs(this.ByteToImage(receiveBytes)));
}
catch (Exception)
{
}
}
If i test on local network , i use DNSHostname to get ip address (private ip addresses) and video works fine on local network. That does not work over internet so i switch to live Ip Address and thus i use the method of getPublicIpAddress().
I know there is something seriously wrong with my approach? What would be right approach?
Should i switch to TCP Listenner? I intend to have multiple receiver of same video in future. So would that affect?
Can UDP clients cause routers to crash, hang up and restart? How can i avoid that?
Lastly, if were to avoid port-forwarding what would be the best strategy?
Please help.
Thanks
Steve

Related

How to get the device IP with flutter

I am looking for a way to get the device IP that is not based on Wifi. I am unable to find the local IP of the current device when compiling to Linux app running on an ethernet connection with no wifi card.
I tried using 'package:wifi/wifi.dart' and 'package:network_info_plus/network_info_plus.dart' to get the device's local ip and both packages gave this error: MissingPluginException (MissingPluginException(No implementation found for method ip on channel plugins.ly.com/wifi))
Is there a way to get the device local IP without a Wifi card?
UPDATE:
Using https://stackoverflow.com/a/52411510/5861729 and https://stackoverflow.com/a/43803986/16477035 I was able to figure out a clean way to get my local ip.
// Find localIp
String localIp = '';
final List<String> privateNetworkMasks = ['10', '172.16', '192.168'];
for (var interface in await NetworkInterface.list()) {
for (var addr in interface.addresses) {
for (final possibleMask in privateNetworkMasks) {
if (addr.address.startsWith(possibleMask)) {
localIp = addr.address;
break;
}
}
}
}
Run 'flutter clean' then 'flutter pub get' and re install your app

Convert QueueClient.Create to MessagingFactory.CreateQueueClient

Trying to convert an implementation using the .net library from using QueueClient.Create to the MessagingFactory.CreateQueueClient to be able to better control the BatchFlushInterval as well as to allowing the use of multiple factories over multiple connections to increase send throughput but running into roadblocks.
Right now we are creating QueueClients (they are maintained throughout the app) like this:
QueueClient.CreateFromConnectionString(address, queueName, ReceiveMode.PeekLock); // address is the connection string from the azure portal in the form of Endpoint=sb....
Trying to change it to creating a MessagingFactory in the class construtor that will be used to create the QueueClients:
messagingFactory = MessagingFactory.Create(address.Replace("Endpoint=",""),mfs);
// later on in another part of the class
messagingFactory.CreateQueueClient(queueName, ReceiveMode.PeekLock);
// error Endpoint not found.,
This throws the error Endpoint not found. If I don't replace the Endpoint= it won't even create the MessagingFactory. What is the proper way to handle this?
Notes:
address = Endpoint=sb://pmg-bus-mybus.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=somekey
As an aside, we have a process that is trying to push as many messages as possible to a queue and others reading it. The readers seem to easily keep up with the sender and I'm trying to maximize the send rate.
The address is the base address of namespace(sb://yournamespace.servicebus.windows.net/) you are connecting to. For more information, please refer to MessagingFactory. The following is the demo code :
var Address = "sb://yournamespace.servicebus.windows.net/"; //base address of namespace you are connecting to.
MessagingFactorySettings MsgFactorySettings = new MessagingFactorySettings
{
NetMessagingTransportSettings = new NetMessagingTransportSettings
{
BatchFlushInterval = TimeSpan.FromSeconds(2)
},
TokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider("RootManageSharedAccessKey", "balabala..."),
OperationTimeout = TimeSpan.FromSeconds(30)
}; //specify operating timeout (optional)
MessagingFactory messagingFactory = MessagingFactory.Create(Address, MsgFactorySettings);
var queue = messagingFactory.CreateQueueClient("queueName",ReceiveMode.PeekLock);
var message = queue.Receive(TimeSpan.Zero);

Correct usage of AddressResolver interface

I was wondering if there is an example usage of the AddressResolver interface in apache ignite.
I was trying to 'bind' my local IP addresses (e.g. 192.168.10.101) to my external IP address using the AddressResolver interface, but without luck.
When I do that the Ignite server just hangs (no output from the debug either)
My code for starting the server is:
TcpDiscoverySpi spi = new TcpDiscoverySpi();
TcpDiscoveryVmIpFinder ipFinder = new TcpDiscoveryVmIpFinder();
ipFinder.setAddresses(ipaddresses);
spi.setIpFinder(ipFinder);
spi.setAddressResolver(new IotAddressResolver());
IgniteConfiguration cfg = new IgniteConfiguration();
// Override default discovery SPI.
cfg.setDiscoverySpi(spi);
System.setProperty("IGNITE_QUIET", "false");
// Start Ignite node.
ignite = Ignition.start(cfg);
My implementation for AddressResolver is:
public class IotAddressResolver implements AddressResolver {
#Override
public Collection<InetSocketAddress> getExternalAddresses(
InetSocketAddress internalAddresses) throws IgniteCheckedException {
String host = "XX.XX.XX.XX";
Collection<InetSocketAddress> result = new ArrayList<InetSocketAddress>();
result.add(new InetSocketAddress(host, internalAddresses.getPort()));
return result;
}
}
The last line of the ignite debug log is:
WARNING: Timed out waiting for message to be read (most probably, the reason is in long GC pauses on remote node) [curTimeout=9989]
I will appreciate any help. Thank you
Can you provide more details about your deployment and what you're trying to achieve with the help of address resolver? How many physical hosts and Ignite nodes do you have? Are they located in different networks with the router between them?
I dont know if this is the best way to handle this but I managed to start igntie as local server. I am setting my local ip and port like this:
System.setProperty("IGNITE_QUIET", "false");
TcpDiscoverySpi spi = new TcpDiscoverySpi();
TcpDiscoveryVmIpFinder ipFinder = new TcpDiscoveryVmIpFinder();
TcpCommunicationSpi commSpi = new TcpCommunicationSpi();
// Set initial IP addresses.
ipFinder.setAddresses(ipaddresses);
spi.setIpFinder(ipFinder);
// Override local port.
commSpi.setLocalPort(47501);
commSpi.setLocalAddress("XX.XX.XX.XX");
commSpi.setLocalPortRange(50);
IgniteConfiguration cfg = new IgniteConfiguration();
// Override default communication SPI.
cfg.setCommunicationSpi(commSpi);
cfg.setDiscoverySpi(spi);
cfg.setAddressResolver(new IotAddressResolver());
cfg.setClientMode(true);
// Start Ignite node
ignite = Ignition.start(cfg);
Where XX.XX.XX.XX is my local IP address

New websocket on different instance of site

I have a remote control for 4 Devices running on a node server.
Each device has its own page (/1, /2, /3, /4), but they are all generated from the same html/js.
the only difference is the ip for each device, loaded from a json on the server depending on the url path.
This all works, but the problem is: i have 3 obviously wrong IPs entered for testing purposes, and one correct one. Now if i open the correct one, go back to the parent page and open the page of a device with a wrong IP, it is still shown as online and can be controlled.
I understand this like: the socket stays open across the pages and is actually not built new on every site.
how can i make sure that each subpage generates a new socket?
right now, i just have
socket = new io.connect();
in the browser.js,
ioServer.on('connection', function (socket) {
//etc.
}
in the app.js and it works for ONE device.
Am I right to assume that I need some kind of "destroy socket if page is changed"-function?
Thanks for the help
By the looks of it, you only want to instantiate and boot a device when the device's client connects for the first time and emits 'startup'. If this is not the case, I'd probably instantiate and boot each device when the server starts.
Going with the first case, I'd store your devices in a key-value object and reuse them when needed.
var devices = {};
ioServer.on('connection', function (socket) {
var client_ip_address = socket.request.connection.remoteAddress;
console.log("New Connection from: " + client_ip_address);
var device;
socket.on('startup', function (data) {
var deviceId = data.device;
console.log("[INFO] startup: device " + deviceId);
//If this device is already in devices, resuse it
if (deviceId in devices) {
//Get the device from your devices object
device = devices[deviceId];
//Otherwise, create a new device and store it in devices
} else {
device = new HyperDeck(deviceId);
//Store new device in devices object
devices[deviceId] = device;
device.boot();
}
console.log(device.id);
console.log(device.ip);
});
...

How to get IP of the client?

I've wrote a self-hosted servicestack server and a client, both desktop applications.
In my very basic PING test service I'm trying to retrieve the IP of the client.
Server is on 192.168.0.87:82, client I tried on the same computer and on another computer, but RemoteIp and UserHostAddress always return 192.168.0.87:82. XRealIp is null.
I also tried base.Request.RemoteIp, but still is 192.168.0.87:82.
What am I doing wrong?
public RespPing Any(ReqPing request)
{
string IP = base.RequestContext.Get<IHttpRequest>().RemoteIp;
string MAC = request.iTransactionInfo.MAC;
Log(MAC,IP, base.RequestContext.Get<IHttpRequest>().RemoteIp + base.RequestContext.Get<IHttpRequest>().XRealIp + base.RequestContext.Get<IHttpRequest>().UserHostAddress);
RespPing response = new RespPing { Result = "PONG" };
return response;
}
Thanks!
Made it with:
HttpListenerRequest iHttpListenerRequest = (HttpListenerRequest)base.RequestContext.Get<IHttpRequest>().OriginalRequest;
string IP = iHttpListenerRequest.RemoteEndPoint.ToString().Split(':')[0];
Request.RemoteIP kept giving me the server address.
ServiceStack.4.0.38
string IP = base.Request.RemoteIp;
fwiw I had to do something just a little bit different:
HttpListenerRequest iHttpListenerRequest = (HttpListenerRequest)base.RequestContext.Get<IHttpRequest>().OriginalRequest;
string ip = iHttpListenerRequest.RemoteEndPoint.Address.ToString();

Resources