Server-side client application with .NET and Xamarin

I searched the Internet for many hours, but I could not find anything that would fit my case.

I just want to implement a Server / Client application with TCP or UDP, where my Android application (Xamarin) acts as a server and my .NET application as a client. Since I have little application development experience and experience with Xamarin, I was looking for an example. All I have found is:

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

First of all, it is the other way around (the server on .NET and the client as an application), and, in addition, it is for Android Studio, so it is difficult for me to translate these things into Xamarin without errors.

Please can someone help and give me an example how to implement my problem?

Thanks!

+5
source share
2 answers

In Xamarin.Android you can use all the usual .Net socket classes:

Namespace:

 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:

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

Example Asynchronous Server Socket on MSDN works as an example of cut / paste without changes.

i.e.

Using the MSDN code, you can call the static AsynchronousSocketListener.StartListening method on the stream 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 connect to 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 '^]'. 

After connecting, enter This is a test<EOF> , and Android will return it back:

 This is a test<EOF> 
+3
source

You do this as in regular .net, except that you must request permission to use sockets.

There are some simple examples of creating a tcp listener connection in C #.

The problem that you have to face is to know the IP address of your server (in the phone), since it probably changes often when the user moves.

+2
source

Source: https://habr.com/ru/post/1247812/


All Articles