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

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

问题描述

我有一个 C# (.net 3.5) 桌面应用程序,它在屏幕上打印出一些数字.现在我想将其扩展到移动设备.(在我的情况下是 Android 设备.)它必须显示与计算机屏幕上相同的数字.整个事情只是一个概念证明,它不需要看起来很好,不需要 100% 工作,不需要有漂亮的 GUI 或简单的设置,它只需要显示一些数字.

我一直在研究 MonoDroid,因为它有可能使用 WCF 服务.(我知道移动设备和台式电脑会连接到同一个网络.)不幸的是 MonoDroid 的试用版不支持真实设备,我不想花几百美元只是为了证明一个观点.

我有 Java 的基本知识,我认为我能够使用 Android SDK 快速开发一个示例应用程序,从网络上的网页中获取数据,解释它(XML、JSON 等等)并显示它在屏幕上.

所以我的问题变成了:是否可以从现有应用程序中创建某种 Web 服务(无需设置整个 IIS 服务器),其中包含一个带有一些 XML 数据的网页,我可以在给定的时间刷新这些数据间隔并且可以从同一网络上的计算机访问?我应该怎么做,使用哪种技术?

或者有其他方法可以实现这样的目标吗?

解决方案

我使用 C# HttpListener 如下...

private void CreateListener{HttpListener 监听器 = null;HttpListenerContext 上下文 = null;HttpListenerRequest 请求 = null;HttpListenerResponse 响应 = 空;字符串端口号 = "9876";字符串 requestUrl;布尔听 = 假;尝试{如果(侦听器 == 空){监听器 = 新的 HttpListener();listener.Prefixes.Add("http://*:" + PortNumber + "/");listener.Start();听=真;同时(听){尝试{context = listener.GetContext();}捕获(例外 e){听=假;}如果(听){请求 = 上下文.请求;requestUrl = request.Url.ToString();//处理请求和/或请求 Url}}}}}

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

实施和调整相当简单.

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?

解决方案

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.

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

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