How to communicate over local network between C# d

2020-02-20 05:40发布

I have a C# (.net 3.5) desktop application which prints out some numbers on the screen. Now I would like to extend this to a mobile device. (Android device in my case.) It has to display the same numbers as on the computer screen. The whole thing is just a proof of concept, it doesn't have to look nice, it doesn't have to work 100%, have a nice GUI or easy setup, it just has to display some numbers.

I have been looking into MonoDroid, as it has the potential to use a WCF service. (I know the mobile device and the desktop computer will be connected to the same network.) Unfortunately MonoDroid's trial version doesn't support a real device and I don't want to spend a few hundred bucks just to prove a point.

I have a basic knowledge of Java, and I think I am able to quickly develop a sample application with the Android SDK to fetch data from a web page on the network, interpret it (XML, JSON, whatever) and display it on the screen.

So my question becomes: is it possible to, from within an existing application, create some kind of web service (without setting up an entire IIS server) that contains a web page with some XML data that I can refresh at a given interval and is accessible from a computer on the same network? How should I do this, which technology to use?

Or are there alternative ways to achieve something like this?

3条回答
霸刀☆藐视天下
2楼-- · 2020-02-20 05:57

You can self-host a WCF service service on the desktop (that is, host it within your application rather than through IIS). It's relatively easy to do.

http://msdn.microsoft.com/en-us/library/ms731758.aspx

Just set up your WCF service to return XML, JSON, whatever you want; and then hit it from the 'droid device.

查看更多
爷、活的狠高调
3楼-- · 2020-02-20 05:59

I use a C# HttpListener as follows...

private void CreateListener
{
    HttpListener listener = null;
    HttpListenerContext context = null;
    HttpListenerRequest request = null;
    HttpListenerResponse response = null;
    string PortNumber = "9876";
    string requestUrl;
    Boolean listen = false;

    try
    {
        if (listener == null)
        {
            listener = new HttpListener();
            listener.Prefixes.Add("http://*:" + PortNumber + "/");
            listener.Start();
            listen = true;
            while (listen)
            {
                try
                {
                    context = listener.GetContext();
                }
                catch (Exception e)
                {
                    listen = false;
                }
                if (listen)
                {
                    request = context.Request;
                    requestUrl = request.Url.ToString();

                    // Process request and/or request Url
                }
            }
        }
    }
}

Basically listener.GetContext(); blocks until an HTTP request is received. You can then use request = context.Request to retrive the HTTP request data and process it. You can then use context.Response to return a response.

Fairly simple to implement and adapt.

查看更多
欢心
4楼-- · 2020-02-20 06:22

You can build an android app that uses web technologies: HTML, CSS, Javascript - essentially it'd be like showing a web page but you envelope it with an app. Then you can run with the idea of serving data to it on the network from your PC, but don't need to really use the Android SDK and Java.

There's a good O'Reilly book on the subject: Building Android Apps with HTML, CSS, and JavaScript. Looks like it's even freely accessible online.

查看更多
登录 后发表回答