Server Client Application with .NET and Xamarin

16,141

Solution 1

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>

Solution 2

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.

Share:
16,141
Canox
Author by

Canox

Updated on July 19, 2022

Comments

  • Canox
    Canox almost 2 years

    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!

  • Canox
    Canox about 8 years
    Great! Thank you for this!
  • Canox
    Canox about 8 years
    That's ok because I just need it when the phone is directly near my computer. Thank you!
  • slaphshot33324
    slaphshot33324 over 5 years
    I have implemented a Xamarin app using the Asynchronous Server Socket example and it is running on my Android emulator but I cannot telnet to it with the error "Could not open connection to the host...". Any ideas on why I am unable to connect?
  • SushiHangover
    SushiHangover over 5 years
    @slaphshot33324 Are you using the correct IP address from the emulator's v-net interface?
  • slaphshot33324
    slaphshot33324 over 5 years
    @SushiHangover I'm not sure, how do I find that IP address?
  • SushiHangover
    SushiHangover over 5 years
    @slaphshot33324 You can use ipconfig/ifconfig (depending upon platform), that is just one way of many.
  • slaphshot33324
    slaphshot33324 over 5 years
    @SushiHangover I found that I can telnet to localhost 5554 by following the instructions where: medium.com/@5XvYrLT7/… I have setup the server to listen on port 11000 per the Asynchronous Socket Server example. I tried to setup a redirection using the command redir add tcp:5000:11000 Now when do a: telnet localhost 5000 it seems to be connecting but i don't get a connection message, it just shows a blank screen.
  • SushiHangover
    SushiHangover over 5 years
    @slaphshot33324 localhost 5554 is the "console" of the emulator, not your running app's IP listener.
  • slaphshot33324
    slaphshot33324 over 5 years
    @SushiHangover Got it, thanks. I'm using Windows so ipconfig, but it doesn't show a network adapter for the emulator. Is there another way I can get the IP? BTW, I set a breakpoint to inspect the line: IPAddress ipAddress = ipHostInfo.AddressList[0]; to see what IP address it is using (192.x.x.x) but I can't telnet to that address either.
  • SushiHangover
    SushiHangover over 5 years
    @slaphshot33324 How about netsh interface ipv4 show address (that is from the top of my head, not in front of a Windows PC right now, so might not be 100% correct...😉)
  • slaphshot33324
    slaphshot33324 over 5 years
    @SushiHangover Great memory for the command syntax! But still none of the ip addresses I have found using these commands allows me to telnet to port 11000 of the Android emulator.
  • slaphshot33324
    slaphshot33324 over 5 years
    @SushiHangover I have created a new question with all of my details and code here: stackoverflow.com/questions/54622085/…