如何在C#桌面应用程序和移动(安卓)设备之间的本地网络进行通信? [英] How to communicate over local network between C# desktop application and mobile (android) device?

查看:148
本文介绍了如何在C#桌面应用程序和移动(安卓)设备之间的本地网络进行通信?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C#打印出一些数字在屏幕上(.NET 3.5)的桌面应用程序。现在,我想这种扩展到移动设备。 (Android设备在我的情况)。它能够显示的相同数字在计算机屏幕上。整个事情是概念只是一个证明,它没有看起来很好,它没有工作100%,有一个很好的GUI或简单的设置,它只是显示一些数字。

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.

我一直在寻找到MonoDroid,因为它使用WCF服务的潜力。 (我知道在移动设备和桌面电脑将被连接到同一个网络。)不幸的是MonoDroid的试用版不支持真正的设备,我不想花几百块钱只是为了证明这一点。

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.

我对Java的基本知识,我认为我能够迅速开发了Android SDK的一个示例应用程序从网页抓取网络上的数据,除preT它(XML,JSON,无论),并在屏幕上显示它。

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.

所以我的问题是:是否有可能,从现有的应用程序中,创建某种网络服务(不含建立一个整个IIS服务器),其中包含了一些XML数据,我可以在给定的刷新网页间隔和是从同一网络上的计算机访问?我应该怎么做,要使用哪种技术?

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?

推荐答案

我使用C#的HttpListener如下...

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
                }
            }
        }
    }
}

基本上 listener.GetContext(); 块,直到接收到一个HTTP请求。然后,您可以使用请求= context.Request 以检索HTTP请求数据,并对其进行处理。然后,您可以使用 context.Response 返回响应。

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.

相当容易实现和适应。

这篇关于如何在C#桌面应用程序和移动(安卓)设备之间的本地网络进行通信?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆