Android Simple chat room instance based on socket client

Source: Internet
Author: User
Tags gettext socket

In this article we share a simple chat room instance source under Android based on socket client

Establish an Android client:

<p> </p><p>package Com.example.mysocketclient;</p><p>import Java.io.BufferedReader;
Import Java.io.BufferedWriter;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import Java.io.OutputStreamWriter;
Import Java.net.Socket;
Import java.net.UnknownHostException;
Import Java.util.date;</p><p>import android.support.v7.app.ActionBarActivity;
Import Android.support.v7.app.ActionBar;
Import android.support.v4.app.Fragment;
Import Android.os.AsyncTask;
Import Android.os.Bundle;
Import Android.view.LayoutInflater;
Import Android.view.Menu;
Import Android.view.MenuItem;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.view.ViewGroup;
Import Android.widget.Button;
Import Android.widget.EditText;
Import Android.widget.TextView;
Import Android.widget.Toast;
Import Android.os.build;</p><p>public class Mainactivity extends Actionbaractivity {</p><p> Private EditText edit, Edit_content;
Private TextView content;
Private Button test, send;</p><p> @Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
This.edit = (edittext) Super.findviewbyid (R.id.edit);
This.edit_content = (edittext) Super.findviewbyid (r.id.edit_content);
This.content = (TextView) Super.findviewbyid (r.id.show);
This.test = (Button) Super.findviewbyid (r.id.test);
This.send = (Button) Super.findviewbyid (R.id.fasong);</p><p> This.test.setOnClickListener (new test ());
This.send.setOnClickListener (new Send ());</p><p>}</p><p> Private class Test implements Onclicklistener {</p><p> @Override
public void OnClick (View arg0) {</p><p> Connect ();
}</p><p>}</p><p> Private class Send implements Onclicklistener {</p><p> @Override
public void OnClick (View arg0) {</p><p> send ();
}</p><p>}</p><p>//----------------------------------------</p><p> sockets socket = NULL;
BufferedWriter writer = null;
BufferedReader reader = null;</p><p> public void connect () {</p><p>
Thread controls reading and writing of current data
Asynctask<void, string, void> read = new asynctask<void, String, void> () {</p><p> @Override
protected void Doinbackground (void ... arg0) {
try {
Socket = new Socket (MainActivity.this.edit.getText ()
. toString (), 12345);
writer = new BufferedWriter (New OutputStreamWriter (
Socket.getoutputstream ()));
reader = new BufferedReader (New InputStreamReader (
Socket.getinputstream ()));
Publishprogress ("@success");
catch (Unknownhostexception E1) {
Toast.maketext (Mainactivity.this, "Unable to connect to the server!") ",
Toast.length_short). Show ();
E1.printstacktrace ();
catch (IOException E1) {
Toast.maketext (Mainactivity.this, "Unable to connect to the server!") ",
Toast.length_short). Show ();
E1.printstacktrace ();
}
try {
String lineString = null;
while ((lineString = Reader.readline ())!= null) {
Publishprogress (lineString);</p><p>}
catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
return null;
}</p><p> @Override
protected void Onprogressupdate (String ... values) {
if (Values[0].equals ("@success")) {
Toast.maketext (Mainactivity.this, "Connect to the server!") ", Toast.length_short). Show ();
}
</p><p> Content.append ("Others say:" +values[0]);
Super.onprogressupdate (values);
}</p><p>};</p><p> read.execute ();</p><p>}</p><p> public void Send ( ) {
try {
Content.append ("I said:" +edit_content.gettext (). toString () + "\ n");
Writer.write (Edit_content.gettext (). toString () + "\ n");
Writer.flush ();
Edit_content.settext ("");
catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}</p><p>}
</p><p>
</p>


Layout file:

<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:orientation= "Vertical" >

<tablelayout
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:orientation= "Horizontal" >

<TableRow>

<edittext
Android:id= "@+id/edit"
Android:layout_width= "200DP"
android:layout_height= "Wrap_content"
android:text= "10.0.2.2"/>

<button
Android:id= "@+id/test"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:text= "Connection test"/>
</TableRow>
</TableLayout>

<textview
Android:id= "@+id/show"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:text= ""/>

<edittext
Android:id= "@+id/edit_content"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
Android:hint= "Please enter the chat content here"/>

<button
Android:id= "@+id/fasong"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:text= "Send Message"/>

</LinearLayout>

First run the server program written in the previous section, then run the Android client, click on the connection test as follows:


Run two Android virtual machines as follows:


Online real-time chat can be realized.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.