问题描述
我在互联网上搜索了很多小时,但是找不到与我的情况相符的东西。
我只是想用以下方式实现服务器/客户端应用TCP或UDP,我的Android应用(Xamarin)充当服务器,而.NET应用充当客户端。由于我没有太多的应用程序开发经验,也没有Xamarin的经验,因此我一直在寻找一个例子。我发现的全部是这样的:
首先,这是相反的方式(.NET上的服务器以及作为App的客户端),另外,它是针对Android Studio的,因此,我很难将这些东西正确地转换为Xamarin。 / p>
请有人帮忙给我一个例子,以解决我的问题?
谢谢!
在 Xamarin.Android
上,您可以使用所有常规的.Net套接字类:
命名空间:
使用System.Net;
使用System.Net.Sockets;
示例:
IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList [0];
IPEndPoint localEndPoint =新的IPEndPoint(ipAddress,11000);
System.Diagnostics.Debug.WriteLine(ipAddress.ToString());
//创建一个TCP / IP套接字。
套接字侦听器=新套接字(AddressFamily.InterNetwork,
SocketType.Stream,ProtocolType.Tcp);
AndroidManifest.xml所需权限为:
< uses-permission android:name = android.permission.INTERNET>< / uses-permission>
基于MSDN的示例可以用作剪切/粘贴示例,而无需进行任何更改。
ie
使用MSDN代码,您可以调用静态方法 AsynchronousSocketListener.StartListening
,在一个线程中开始侦听 AsynchronousSocketListener
类中定义的端口11000。
new Thread(new ThreadStart(委托{
AsynchronousSocketListener.StartListening();
}))。Start();
一旦它在您的设备/模拟器上运行,您可以远程登录到您的Android TCP套接字服务器:
> telnet 10.71。 34.100 11000
正在尝试10.71.34.100 ...
已连接到10.71.34.100。
转义字符为 ^]。
连接后,键入这是一个测试< EOF>
,Android会回显它:
测试< EOF>
I searched around the internet a lot of hours but I couldn't find anything that matches my case.
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
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?
Thank you!
On Xamarin.Android
you can use all of the regular .Net socket classes:
Namespaces:
using System.Net;
using System.Net.Sockets;
Example:
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 Required Permissions are:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
The MSDN-based Asynchronous Server Socket example works as a cut/paste example with no changes.
i.e.
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 ();
Once it is running on your device/emulator, you can telnet into your Android TCP socket server:
>telnet 10.71.34.100 11000
Trying 10.71.34.100...
Connected to 10.71.34.100.
Escape character is '^]'.
Once connected, type in This is a test<EOF>
and the Android will echo it back:
This is a test<EOF>
这篇关于.NET和Xamarin的服务器客户端应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!