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());
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>
source share