Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Server Client Application with .NET and Xamarin

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!

like image 581
Canox Avatar asked Apr 24 '16 12:04

Canox


People also ask

What is Xamarin?

Dig deeper: What is Xamarin? Businesses worldwide, spanning all industries, use Xamarin and .NET to build performant native mobile apps. See mobile apps that other developers have created using Xamarin, .NET, and Visual Studio. Xamarin apps are native apps!

Is there an idenity server for your Xamarin app?

Setting up an Idenity Server for your Xamarin app May 11, 2021 Have you ever wondered how hard it would be to set up a minimal viable authentication server that uses industry standards and usable from your mobile Xamarin application?

What is the future of Xamarin and NET Framework?

.NET Framework, .NET Core and Xamarin are built on and share many underlying technologies. In the future, Microsoft is invested in moving .NET forward and that includes.NET Framework, .NET Core and Xamarin.

How to sign in to the Xamarin Android app?

However, the main launcher of Xamarin.Android application will display the sign-in page. You can sign In only if you already have registered otherwise you cannot navigate to the next page.


2 Answers

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>
like image 157
SushiHangover Avatar answered Sep 18 '22 14:09

SushiHangover


You do this like in normal .net, except you have to ask permissions to use sockets.

There are tons of simple example of creating a listening tcp connection in c#.

The problem you will have is to know the IP address of your server (in the phone) as it will likely change often when the user is moving.

like image 38
Softlion Avatar answered Sep 19 '22 14:09

Softlion