Adobe AIR, URLRequest, and local ports - actionscript-3

I'm looking at writing an app with Adobe AIR, but I wanted to figure out if this is feasible with the platform first. (Mostly new to Flash and AIR)
What I would like to do is know ahead of time what local port a URLRequest will be using. If possible, I would like to hold a connection open even and keep using the same socket.
What I'm basically trying to accomplish is doing some NAT-Traversal and port negotiation ahead of time. Once I have both the client and the server negotiated, I'd like them to connect and basically use HTTP in a peer-to-peer way to stream media, like this sample:
var s = new air.Sound();
var url = "http://www.example.com/sounds/bigSound.mp3";
var req = new air.URLRequest(url);
var context = new air.SoundLoaderContext(8000, true);
s.load(req, context);
s.play();
The problem is that I don't see this happening unless I can get some information from AIR on what ports it's planning to use. I would be OK with creating my own Socket connections to the server and using them, but I would need a way to leverage the Sound framework to stream in the same way from a socket.
Ideas? Thoughts? Thanks!

Even if you managed to guess which port AIR is going to use on your device, it is not going to be very helpful, since there is a reasonably high probability that your NAT will translate it to another value IF your AIR device has a private IP address.
This issue does not happen if your AIR server has a public IP address. Most often, you can configure the server's NAT/Router to forward traffic as is. A port scan from the WAN will quickly tell you which port is used.
If you want to 'hijack' an outbound connection created by AIR itself, then you might try to have it contact a special server peer you have implemented which will forward traffic from and to it. Not simple, but possible. Basically you would collect holes punched in the NAT by the server.

Related

In a in WinRT app, how do I connect using TLS1.2?

I've got a Windows Store app that's a WinRT Phone/Desktop app (i.e. not a UWP app), targeting Windows 8.1 and up.
It's been on the store for several years now, but recently it stopped being able to connect with various web APIs and websites (YouTube, as well as my own site) using HTTPS.
I have a WPF version of this app as well, and this happened on that app recently as well, and to fix it I used System.Net.ServicePointManager. Unfortunately, in my WinRT environment, System.Net doesn't include ServicePointManager. In my WPF app, I did this, and it worked just fine:
ServicePointManager.ServerCertificateValidationCallback = delegate
{
Debug.WriteLine("returning true (the ssl is valid)");
return true;
};
// our server is using TLS 1.2
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
In doing some research around the internet, it seems that .NET 4.6 should include ServicePointManager, but I don't see any way to change (or even see) my version of .NET in the WinRT development environment.
I looked some more and found that a StreamSocket could be used to connect with TLS1.2... but that seems primarily designed to enable bluetooth communications, or communications to a web endpoint, but only by hostname... which is insufficient for me. I need to connect to an actual website, not just the base-level domain.
Trying this, I did the following:
StreamSocket socket = new StreamSocket();
string serverServiceName = "https";
socket.Control.KeepAlive = false;
url = "inadaydevelopment.com";
HostName serverHost = new HostName(url);
await socket.ConnectAsync(serverHost, serverServiceName, SocketProtectionLevel.Tls12);
text = await ReadDataFromSocket(socket);
I can include the code for ReadDataFromSocket() if necessary, but it seems to work, reading the data from the socket as expected when I point it at https://google.com. However, I can't seem to figure out how to point the socket at anything useful. The homepage of inadaydevelopment.com isn't what I want; I'm looking to consume a web API hosted on that server, but can't seem to find a way to do that.
Since the first parameter to the ConnectAsync() method is just HostName, the second parameter (remoteServiceName) must be the way to connect to the actual API or webpage I'm trying to connect to. According to the docs, that is The service name or TCP port number of the remote network destination... I haven't seen any example values for this parameter other than https and various numeric values, neither of which is going to get me to the API endpoint or webpage I'm trying to connect to.
So, with that super-long preamble out of the way, my question boils down to this:
Is there a way for me to use System.Net.ServicePointManager in my WinRT app like I do in my WPF app? If so, how?
If not, how can I use StreamSocket to connect to the exact web service or webpage I want to connect to, rather than just the top-level host?
If that's not possible, by what other means can I consume web content using TLS1.2?
Thanks in advance for any help or advice.
Use Windows.Web.Http API instead of System.Net.Http API.
System.Net.Http does not support TLS1.2 but Windows.Web.Http does in WinRT apps.

How to find out the availability status of a Web API from a Windows Store application

I have a Line-of-Business (LoB) Windows 8.1 Store application I developed for a client. The client side-loads it on several Windows 10 tablets. They use it in an environment where WiFi is spotty at best and they would like to get some sort of notification inside the app, regardless of what page they are on, notification that will let them know that they've lost connectivity to the network. I have created a method on my Web API that is not hitting the repository (database). Instead, it quickly returns some static information regarding my Web API, such as version, date and time of the invocation and some trademark stuff that I'm required to return. I thought of calling this method at precise intervals of time and when there's no response, assume that the Web API connectivity is lost. In my main page, the first one displayed when the application is started, I have the following stuff in the constructor of my view model:
_webApiStatusTimer = new DispatcherTimer();
_webApiStatusTimer.Tick += OnCheckWebApiStatusEvent;
_webApiStatusTimer.Interval = new TimeSpan(0, 0, 30);
_webApiStatusTimer.Start();
Then, the event handler is implemented like this:
private async void OnCheckWebApiStatusEvent(object sender, object e)
{
// stop the timer
_webApiStatusTimer.Stop();
// refresh the search
var webApiInfo = await _webApiClient.GetWebApiInfo();
// add all returned records in the list
if (webApiInfo == null)
{
var messageDialog = new MessageDialog(#"The application has lost connection with the back-end Web API!");
await messageDialog.ShowAsync();
// restart the timer
_webApiStatusTimer.Start();
}
}
When the Web API connection is lost, I get a nice popup message that informs me that the Web API is no longer available. The problem I have is that after a while, especially if I navigate away from the first page but not necessary, I get an UnauthorizedAccessException in my application.
I use the DispatcherTimer since my understanding is that this is compatible with
UI threads, but obviously, I still do something wrong. Anyone cares to set me on the right path?
Also, if you did something similar and found a much better approach, I'd love to hear about your solution.
Thanks in advance,
Eddie
First, If you are using Windows Store Apps, then you could possibly use a Background task to check poll for the status of the web api instead of putting this responsibility on your view model, its not the viewmodels concern
Second, if you are connecting from your Windows store app to your API then one successful authentication/ authorization for the first time, how and where do you store the token (assuming you are using token authentication). If you are (and ideally you should), is there a timer that you start which is set to the token expiration time? Is your local storage getting flushed somehow and loosing the aurthorization data?
Need more information.

Can I use "new ServerSocket(0)" with openshift

I have developed an application that allows multiple players to play together on line at various games such shifumi, poker, chess and so on. It works very well on my localhost. I would like to publish it. So I decided to use openshift to do this.
But there is a problem.
It seems it come from this statement : new ServerSocket(0). I do this inside the doPost method of an HttpServlet.
Could you tell me I don't have the permission to do this (new ServerSocket(0)) inside an openshift server?
I think you have a couple of issues going on here.
The first is that when you call new ServerSocket(0), it is going to try to find a socket that it can bind to, probably on either 0.0.0.0 (all ip addresses/interfaces) or 127.0.0.1, neither of which is allowed on OpenShift.
According to the documentation (located here: http://download.java.net/jdk7/archive/b123/docs/api/java/net/ServerSocket.html) you can use one of the overloaded methods to provide an ip address to bind to, which should be your OPENSHIFT__IP (where could be jbosseap, jbossas, wildfly, jbossews, etc).
ServerSocket(int port, int backlog, InetAddress bindAddr)
Your second issue is a bit more complicated, basically what ports you can bind to. OpenShift allows user code to bind to ports 15000-20000, depending on what ports are not being used by other applications or services. However, none of those ports are open to the public internet, they are all internal ports for internal communications, so if you are trying to let a client connect to them, it won't work. The only ports that are publicly available are 80/443/8000/8443, and your application must bind to port 8080 on your OPENSHIFT__IP to be able to be reached using your app-domain.rhcloud.com public url. You can check out this article to read more about how all of the binding and routing works: https://developers.openshift.com/en/managing-port-binding-routing.html
Hopefully that answers the question about why that piece of code is not working.

Sending requests from Flash

Here is a simplified version of some Flash code I have been using to get username of the person playing the game:
var req:URLLoader = new URLLoader();
req.addEventListener(Event.COMPLETE, onLoaded);
req.load(new URLRequest(baseURL + "php/account.php?getUsername"));
function onLoaded(e:Event):void {
if(e.target.data == "Guest") {
LoadingText.text = "Welcome, Guest!"
}
else {
LoadingText.text = "Welcome, " + e.target.data + "!";
}
}
php/account.php?getUsername just gets the username from a PHP session variable and outputs it.
Using something like Tamper Data for Firefox, I can see the request being sent from Flash and can even edit it if I wanted.
This approach feels very slow compared to other online games, and when trying to reverse engineer other games to see how they manage something like this, for some games, requests wont show up in Tamper Data.
So I wonder, are there any alternative ways to send requests in Flash which are faster and more efficient? I don't need anything fancy such as sending post data or editing the referral URL.
Thanks.
Most probably other games use socket connections. This is reason why you don't see request in Tamper Data and why they have better performance.
In case of socket connection you open connection to server once and you can make as many requests as you wish.
With HTTP, TCP connection is opened and closed for each request this affects performance.
With socket connections you can avoid HTTP protocol completely, you can design your protocol much lighter than HTTP. In fact no one uses HTTP when you need high performance.
See flash socket documentation here: Socket
P.S.
For high performance it is better to use UDP instead of TCP but flash sockets are limited to TCP so you don't need to take care of this.

Starting multiple HTTP listeners on IIS using C#.NET 2.0

I have two windows services running on the same machine. Both the services uses
private HttpListener listener;
I specify the baseURL as "http://IPAddress:8080/" & "http://IPAddress:8081/" respectively for each of the services. Then I do the needful and call
listener.Start();
The first service starts successfully at 8080 port. But when I now start the 2nd service,
I get HTTPListenerException "The process cannot access the file because it is being used by another process" for listener object.
Could anybody please tell me:
1) If it is possible to start two HTTP listeners on the same IIS at two different ports.
2) If yes, how can we achecive this?
3) Is there any other way of doing this?
For your information:
I am using C#.NET 2.0 and IIS 6.0 server.
Thanks & Regards,
Hari
Doesn't HttpListener work independently from IIS? Can you stop the IIS service and see what happens?
Or maybe port 8081 is used by another program or process. I suggest to try to set the port to another number. You could open a command line and execute the "netstat" command to see if the port is used before starting your services.
(source: googlepages.com)
The HTTPListener is indeed not a part of the IIS. It's just C# code.
did you do it like this?
String[] prefixes = { "http://localhost:8280/", "http://localhost:8281/"};
HttpListener listener = new HttpListener();
listener.Prefixes.Add("http://localhost:8280/");
listener.Start();
1) If it is possible to start two HTTP listeners on the same IIS at two different ports.
Yes, it is. But listeners on the IIS is not made through code, it's through the IIS administration tool, so that's probably not what you're trying to do. IIS is separate from the c# code you're attempting.
2) If yes, how can we achecive this?
Try some other port numbers (like 58080 and 58081) and try replacing "IPAddress" with "localhost". Most likely your port number is used by some other web server
3) Is there any other way of doing this?
If you're trying to serve web pages, you probably wan't to make a web application instead of making your own HTTP server
If you're doing something custom, you might want to take a look at WCF (Windows Communication Foundation), as it's the new framework for web, sockets, web services etc. and it's what MS will be using going forward
It's usually a good idea to take a quick look at this page when using port numbers...just to make sure a temp number you're trying to use isn't in use already.