################ Basic Requirements: ################
- Xamarin Studio 6.1.6 (Latest)
- SDK Tools Version: 25.2.3
- SDK Platform Tools Version: 25.0.1
- SDK Build Tools Version: 23.0.1
- Android NDK
- SDK Platform:
4.0.3 (API level 15)
4.2 (API level 17)
4.4 (API level 19)
6.0 (API level 23)
7.0 (API level 24)
- Microsoft .NET 4.0.30319.42000
- Xamarin.Android Version: 7.0.1.6
- GTK# 2.12.38
################ Key Features: ################
- Xamarin Xmpp Client Sample (C#) For Android
- Socket Connection
################ Feedback: ################
- For any kind of further queries or bugs, kindly do not hesistate to write me at: dbh4ck@gmail.com
- http://dbh4ck.blogspot.in
Download Full Source Code From My Github
Download Apk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Android.App; | |
using Android.Widget; | |
using Android.OS; | |
using System.Net.Sockets; | |
using System; | |
using System.Text; | |
using System.Threading; | |
using Android.Content; | |
using Android.Views; | |
namespace Nimbuzz_Xamarin_Android_Db | |
{ | |
[Activity(Label = "Nimbuzz Xamarin Android By Db~@NC", MainLauncher = true, Icon = "@mipmap/icon")] | |
public class MainActivity : Activity | |
{ | |
// Make Socket Universal throughout App (Make Use socket anywhere in any Activity of this App) | |
public static TcpClient client; | |
public static string passid; | |
protected override void OnCreate(Bundle savedInstanceState) | |
{ | |
base.OnCreate(savedInstanceState); | |
// Set our view from the "main" layout resource | |
SetContentView(Resource.Layout.Main); | |
Button btnSignIn = FindViewById<Button>(Resource.Id.btnSignin); | |
btnSignIn.Click += delegate | |
{ | |
login(); | |
}; | |
} | |
void login() | |
{ | |
// Get UserName and PassWord From User | |
EditText userName = FindViewById<EditText>(Resource.Id.txtUser); | |
EditText userPwd = FindViewById<EditText>(Resource.Id.txtPwd); | |
var usrid = userName.Text; | |
var usrpwd = userPwd.Text; | |
// Initialize New TcpClient Connection | |
try | |
{ | |
client = new TcpClient() { NoDelay = true }; | |
client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true); | |
client.Client.Connect("o.nimbuzz.com", 5222); | |
Toast.MakeText(this, "Connecting...", ToastLength.Short).Show(); | |
NetworkStream stream = client.GetStream(); | |
if (client.Client.Connected) | |
{ | |
client.Client.Send(Encoding.UTF8.GetBytes("<stream:stream xmlns='jabber:client' to='nimbuzz.com' version='1.0' xmlns:stream='http://etherx.jabber.org/streams' xml:lang='en' >")); | |
string str = Convert.ToBase64String(Encoding.UTF8.GetBytes("\0" + usrid + "\0" + usrpwd)); | |
client.Client.Send(Encoding.UTF8.GetBytes("<auth id='sasl2' xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='PLAIN'>" + str + "</auth>")); | |
} | |
// Start Thread On Successfull Connection | |
Thread rec = new Thread(new ThreadStart(dbThread)); | |
rec.IsBackground = true; | |
rec.Start(); | |
} | |
catch (SocketException ex) { | |
} | |
} | |
void dbThread() | |
{ | |
try | |
{ | |
while (client.Connected) | |
{ | |
byte[] buffbyte = new byte[client.ReceiveBufferSize]; | |
int i = client.Client.Receive(buffbyte); | |
if (i != 0) | |
{ | |
if (i < buffbyte.Length) | |
{ | |
byte[] buffbyte2 = new byte[i]; | |
for (int b = 0; b < i; b++) | |
{ | |
buffbyte2[b] = buffbyte[b]; | |
} | |
buffbyte = buffbyte2; | |
} | |
// Handles the incoming data received from Server side | |
string xml = Encoding.UTF8.GetString(buffbyte); | |
DataArrival(xml); | |
// Ping Server every 60 seconds to avoiding auto-disconnect | |
int startin = 60 - DateTime.Now.Second; | |
var t = new System.Threading.Timer(o => client.Client.Send(Encoding.UTF8.GetBytes("<iq to='nimbuzz.com' type='get' id='db1_a2'><ping xmlns='urn:xmpp:ping' /></iq>")), null, startin * 1000, 60000); | |
} | |
} | |
} | |
catch (Exception ex) | |
{ | |
//Toast.MakeText(this, ex.ToString(), ToastLength.Short).Show(); | |
} | |
} | |
void DataArrival(string xml) | |
{ | |
xml = xml.Replace("\"", "'"); | |
if (xml.IndexOf("<success xmlns='urn:ietf:params:xml:ns:xmpp-sasl'/>", System.StringComparison.OrdinalIgnoreCase) + 1 != 0) | |
{ | |
// Register Client | |
// Bind Resource | |
// Open session | |
client.Client.Send(Encoding.UTF8.GetBytes("<stream:stream xmlns='jabber:client' to='nimbuzz.com' version='1.0' xmlns:stream='http://etherx.jabber.org/streams' xml:lang='en' >")); | |
client.Client.Send(Encoding.UTF8.GetBytes("<iq type='set'><bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'><resource>dbhere</resource></bind></iq>")); | |
client.Client.Send(Encoding.UTF8.GetBytes("<iq id='jcl_2' type='set'><session xmlns='urn:ietf:params:xml:ns:xmpp-session'/></iq>")); | |
} | |
if (xml.IndexOf("<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'>", System.StringComparison.OrdinalIgnoreCase) + 1 != 0) | |
{ | |
//Toast.MakeText(this, "Logged in Successfully!", ToastLength.Short).Show(); | |
client.Client.Send(Encoding.UTF8.GetBytes("<presence><status>Online Via Xamarin C# (Android)</status><priority>1</priority><show>Online</show><nick xmlns='http://jabber.org/protocol/nick'>XamarinDb</nick></presence>")); | |
EditText userName = FindViewById<EditText>(Resource.Id.txtUser); | |
passid = userName.Text; | |
var LoggedInt = new Intent(this, typeof(LoggedActivity)); | |
LoggedInt.PutExtra("usrid", passid); //pass value to Logged Activity | |
StartActivity(LoggedInt); | |
} | |
if (xml.Contains("<failure xmlns=")) | |
{ | |
//Toast.MakeText(this, "Invalid Username & Password", ToastLength.Short).Show(); | |
client.Client.Close(); // close socket | |
} | |
} | |
public override bool OnCreateOptionsMenu(IMenu menu) | |
{ | |
base.OnCreateOptionsMenu(menu); | |
// Inflate the menu; this adds items to the action bar if it is present. | |
MenuInflater inflater = this.MenuInflater; | |
inflater.Inflate(Resource.Menu.menuoption, menu); | |
return true; | |
} | |
public override bool OnOptionsItemSelected(IMenuItem item) | |
{ | |
base.OnOptionsItemSelected(item); | |
switch (item.ItemId) { | |
case Resource.Id.about: | |
aboutDialog(); | |
break; | |
case Resource.Id.exit: | |
Android.OS.Process.KillProcess(Android.OS.Process.MyPid()); | |
break; | |
default: | |
break; | |
} | |
return true; | |
} | |
void aboutDialog() | |
{ | |
/* | |
var myalert = new AlertDialog.Builder(this); | |
myalert.SetTitle("About App"); | |
myalert.SetMessage("This is Xamarin Android App Coded in C# based on Socket Programming"); | |
myalert.SetNeutralButton("OK", (senderAlert, args) => | |
{ | |
this.Dispose(); | |
}); | |
Dialog dialog = myalert.Create(); | |
dialog.Show(); | |
**/ | |
Toast.MakeText(this, "Xamarin Android Coded By Db~@NC", ToastLength.Long).Show(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using Android.App; | |
using Android.Content; | |
using Android.OS; | |
using Android.Runtime; | |
using Android.Views; | |
using Android.Widget; | |
namespace Nimbuzz_Xamarin_Android_Db | |
{ | |
[Activity(Label = "LoggedActivity")] | |
public class LoggedActivity : Activity | |
{ | |
protected override void OnCreate(Bundle savedInstanceState) | |
{ | |
base.OnCreate(savedInstanceState); | |
// Set our view from the layout resource | |
SetContentView(Resource.Layout.LoggedLayout); | |
TextView jidlbl = FindViewById<TextView>(Resource.Id.textView5); | |
TextView jidUser = FindViewById<TextView>(Resource.Id.textView6); | |
jidUser.Text = Intent.GetStringExtra("usrid") + "@nimbuzz.com"; | |
//MainActivity.client.Client.Send(Encoding.UTF8.GetBytes("<message to='db~@nimbuzz.com' type='chat' id='-4587545787'><body>im Online</body><active xmlns ='http://jabber.org/protocol/chatstates' /><request xmlns='urn:xmpp:receipts' /></message>")); | |
MainActivity.client.Client.Send(Encoding.UTF8.GetBytes("<presence to='db~@nimbuzz.com' type='subscribe' />")); | |
Button logout_btn = FindViewById<Button>(Resource.Id.logoutbtn); | |
logout_btn.Click += delegate | |
{ | |
if (MainActivity.client.Client.Connected == true) { | |
MainActivity.client.Client.Dispose(); | |
MainActivity.client.Client.Close(); | |
// Throw back to MainACtivity after Logout Connection Closing | |
StartActivity(typeof(MainActivity)); | |
} | |
}; | |
} | |
public override bool OnCreateOptionsMenu(IMenu menu) | |
{ | |
base.OnCreateOptionsMenu(menu); | |
// Inflate the menu; this adds items to the action bar if it is present. | |
MenuInflater inflater = this.MenuInflater; | |
inflater.Inflate(Resource.Menu.loggedmenuoption, menu); | |
return true; | |
} | |
public override bool OnOptionsItemSelected(IMenuItem item) | |
{ | |
base.OnOptionsItemSelected(item); | |
string strinfo = "You are currently Logged as: " + Intent.GetStringExtra("usrid") + "@nimbuzz.com"; | |
switch (item.ItemId) | |
{ | |
case Resource.Id.info: | |
Toast.MakeText(this, strinfo , ToastLength.Long).Show(); | |
break; | |
case Resource.Id.exit: | |
Android.OS.Process.KillProcess(Android.OS.Process.MyPid()); | |
break; | |
default: | |
break; | |
} | |
return true; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<ScrollView xmlns:tools="http://schemas.android.com/tools" | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
android:id="@+id/scroll" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:transcriptMode="normal" | |
android:fillViewport="true"> | |
<RelativeLayout xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:paddingBottom="@dimen/activity_vertical_margin" | |
android:paddingLeft="@dimen/activity_horizontal_margin" | |
android:paddingRight="@dimen/activity_horizontal_margin" | |
android:paddingTop="@dimen/activity_vertical_margin"> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="561.5dp" | |
android:orientation="vertical" | |
android:layout_centerVertical="true" | |
android:layout_alignParentLeft="true" | |
android:layout_alignParentStart="true" | |
android:layout_marginBottom="0.0dp"> | |
<TextView | |
android:textSize="20dp" | |
android:textColor="#fb0564" | |
android:id="@+id/lbldb" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="46dp" | |
android:text="@string/hello_db" | |
android:textAlignment="center" | |
android:gravity="center_horizontal" /> | |
<TextView | |
android:textSize="20sp" | |
android:textColor="#fce907ed" | |
android:id="@+id/textView2" | |
android:layout_width="345.5dp" | |
android:layout_height="wrap_content" | |
android:text="@string/login" | |
android:layout_marginTop="16dp" | |
android:layout_below="@+id/lbldb" | |
android:textAlignment="center" | |
android:gravity="left" /> | |
<Space | |
android:layout_width="338.0dp" | |
android:layout_height="10dp" /> | |
<EditText | |
android:id="@+id/txtUser" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:hint="Enter Your Username" | |
android:inputType="text" | |
android:layout_below="@+id/textView2" | |
android:layout_alignParentLeft="true" | |
android:layout_alignParentStart="true" | |
android:layout_marginTop="22dp" | |
android:textColor="@color/colorPrimaryDark" /> | |
<Space | |
android:layout_width="match_parent" | |
android:layout_height="20dp" /> | |
<EditText | |
android:id="@+id/txtPwd" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:hint="Enter Your Password" | |
android:inputType="textPassword" | |
android:layout_below="@+id/txtUser" | |
android:layout_alignParentLeft="true" | |
android:layout_alignParentStart="true" | |
android:layout_marginTop="21dp" | |
android:textColor="@color/colorPrimaryDark" /> | |
<ProgressBar | |
style="?android:attr/progressBarStyle" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:id="@+id/progressBar" | |
android:layout_above="@+id/btnSignin" | |
android:layout_centerHorizontal="false" | |
android:indeterminate="false" | |
android:visibility="invisible" | |
android:layout_gravity="center" /> | |
<Button | |
android:textColor="#0b42f6" | |
android:id="@+id/btnSignin" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:text="@string/btn_login" | |
android:layout_marginBottom="44dp" | |
android:layout_above="@+id/textViewDbLink" | |
android:layout_centerHorizontal="true" /> | |
<TextView | |
android:textColor="#fc4108" | |
android:autoLink="web" | |
android:id="@+id/textViewDbLink" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:text="@string/lbl_dbh4ck" | |
android:textAlignment="center" | |
android:layout_marginBottom="14dp" | |
android:layout_above="@+id/textViewDbEmail" | |
android:layout_alignParentLeft="true" | |
android:layout_alignParentStart="true" | |
android:layout_alignParentRight="true" | |
android:layout_alignParentEnd="true" | |
android:gravity="center_horizontal" /> | |
<TextView | |
android:textColor="#198104" | |
android:id="@+id/textViewDbEmail" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:text="@string/lbl_dbmail" | |
android:layout_marginBottom="20dp" | |
android:layout_alignParentBottom="true" | |
android:layout_centerHorizontal="true" | |
android:textAlignment="center" | |
android:gravity="center_horizontal" /> | |
</LinearLayout> | |
</RelativeLayout> | |
</ScrollView> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<ScrollView xmlns:tools="http://schemas.android.com/tools" | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
android:id="@+id/scroll" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:transcriptMode="normal" | |
android:fillViewport="true"> | |
<RelativeLayout | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:elevation="1dp"> | |
<!-- TODO: Update blank fragment layout --> | |
<TextView | |
android:id="@+id/textView5" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="44dp" | |
android:text="@string/botid" | |
android:textStyle="normal|bold" | |
android:textSize="18sp" | |
android:textAlignment="center" | |
android:textColor="@color/colorPrimaryDark" | |
android:gravity="center_horizontal" /> | |
<TextView | |
android:text="TextView" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="36dp" | |
android:id="@+id/textView6" | |
android:textSize="20sp" | |
android:textColor="@color/colorPrimaryDark" | |
android:textStyle="normal|bold" | |
android:fontFamily="monospace" | |
android:layout_below="@+id/textView5" | |
android:layout_alignParentLeft="true" | |
android:layout_alignParentStart="true" | |
android:textAlignment="center" | |
android:gravity="center_horizontal" /> | |
<Button | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="@string/logout" | |
android:id="@+id/logoutbtn" | |
android:textSize="20sp" | |
android:textColor="#ff1ca6f4" | |
android:textAllCaps="false" | |
android:gravity="center" | |
android:typeface="serif" | |
android:textStyle="bold" | |
android:layout_centerHorizontal="true" | |
android:layout_centerVertical="true" /> | |
</RelativeLayout> | |
</ScrollView> |
No comments:
Post a Comment