带有 .NET 和 Xamarin 的服务器客户端应用程序 [英] Server Client Application with .NET and Xamarin

查看:29
本文介绍了带有 .NET 和 Xamarin 的服务器客户端应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在互联网上搜索了很多小时,但找不到与我的情况相符的任何内容.

I searched around the internet a lot of hours but I couldn't find anything that matches my case.

我只想实现一个带有 TCP 或 UDP 的服务器/客户端应用程序,其中我的 Android 应用程序 (Xamarin) 充当服务器,而我的 .NET 应用程序充当客户端.由于我没有太多的应用程序开发经验和 Xamarin 的经验,我正在寻找一个例子.我只找到了这个:

I simply want to implement a Server/Client App with TCP or UDP where my Android App (Xamarin) acts as a server and my .NET application as Client. Since I have not much experience with app development and no experience with Xamarin, I was looking for an example. All I found was this:

http://www.codeproject.com/Articles/340714/Android-How-to-communicate-with-NET-application-vi

首先,这是相反的方式(.NET 上的服务器和客户端作为应用程序),另外它适用于 Android Studio,因此我很难将这些东西正确地转换为 Xamarin.

First of all this is the opposite way (Server on .NET and Client as App) and additionaly it is for Android Studio so it's hard for me to translate these things into Xamarin without errors.

请有人帮忙并举例说明如何解决我的问题?

Please can someone help and give me an example how to realize my issue?

谢谢!

推荐答案

Xamarin.Android 上,您可以使用所有常规的 .Net 套接字类:

On Xamarin.Android you can use all of the regular .Net socket classes:

using System.Net;
using System.Net.Sockets;

示例:

IPHostEntry ipHostInfo = Dns.GetHostEntry (Dns.GetHostName ());
IPAddress ipAddress = ipHostInfo.AddressList [0];
IPEndPoint localEndPoint = new IPEndPoint (ipAddress, 11000);
System.Diagnostics.Debug.WriteLine(ipAddress.ToString());
// Create a TCP/IP socket.
Socket listener = new Socket (AddressFamily.InterNetwork,
                     SocketType.Stream, ProtocolType.Tcp);

AndroidManifest.xml 所需的权限是:

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

基于 MSDN 的 异步服务器套接字 示例用作剪切/粘贴示例,没有任何更改.

The MSDN-based Asynchronous Server Socket example works as a cut/paste example with no changes.

使用 MSDN 代码,您可以在线程中调用静态方法 AsynchronousSocketListener.StartListening,以开始侦听 AsynchronousSocketListener 类中定义的端口 11000.>

Using the MSDN code, you can call the static method, AsynchronousSocketListener.StartListening, in a thread to start listening on port 11000 defined in the AsynchronousSocketListener class.

new Thread (new ThreadStart (delegate {
    AsynchronousSocketListener.StartListening();
})).Start ();

<小时>

一旦它在您的设备/模拟器上运行,您就可以远程登录到您的 Android TCP 套接字服务器:

<小时>

>telnet 10.71.34.100 11000

<小时>

Trying 10.71.34.100...
Connected to 10.71.34.100.
Escape character is '^]'.

<小时>

连接后,输入This is a test,Android会回显:


Once connected, type in This is a test<EOF> and the Android will echo it back:

This is a test<EOF>

这篇关于带有 .NET 和 Xamarin 的服务器客户端应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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