Android Connection server-side socket

Source: Internet
Author: User

Package com.example.esp8266;

Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.OutputStream;
Import Java.io.PrintStream;
Import Java.net.Socket;

Import android.app.Activity;
Import Android.os.Bundle;
Import Android.os.Handler;
Import Android.os.Message;
Import Android.view.View;
Import Android.widget.Button;
Import Android.widget.EditText;
Import Android.widget.Toast;

public class Mainactivity extends Activity {
Private EditText edsend, edreceive;
Private Button btnconnect, btnsend;
Private Handler MyHandler;
Private Sendthread Sendthread;
Private Boolean isreceive = false;
Private Boolean isconnect = false;
private static final String HOST = "192.168.4.1";
private static final int PORT = 333;
String strmessage;
Socket socket = NULL;

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Edsend = (EditText) Findviewbyid (r.id.edsend);
Edreceive = (EditText) Findviewbyid (r.id.edreceive);
Btnconnect = (Button) Findviewbyid (r.id.btconnect);
Btnsend = (Button) Findviewbyid (r.id.btsend);
Connection
Btnconnect.setonclicklistener (New View.onclicklistener () {

public void OnClick (View v) {
TODO auto-generated Method Stub
if (!isconnect) {
New Thread (Connectthread). Start ();
Isconnect = true;
}
}
});
Send
Btnsend.setonclicklistener (New View.onclicklistener () {

public void OnClick (View v) {
Start Send Thread
New Thread (Sendthread). Start ();
}
});
MyHandler = new Handler () {//UI main thread Message handler function

public void Handlemessage (Message msg) {
Bundle bundle = Msg.getdata ();
String string = Bundle.tostring ();
Edreceive.settext (string);
}
};
}

Interfaces connected to the server
Runnable connectthread = new Runnable () {

public void Run () {
TODO auto-generated Method Stub
try {
Socket = new Socket (HOST, PORT);
if (socket! = NULL)
Toast.maketext (Getapplicationcontext (), "Connection succeeded",
Toast.length_long). Show ();
Else
Toast.maketext (Getapplicationcontext (), "Connection Failed",
Toast.length_long). Show ();
Initialize the Send thread
Sendthread = new Sendthread (socket);
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
};
interface to receive messages
Runnable Receive = new Runnable () {
InputStream instream;
Private byte[] buffer;
Private String str = NULL;

public void Run () {
TODO auto-generated Method Stub
while (!isreceive) {
Buffer = new BYTE[512];
try {
Instream = Socket.getinputstream ();
Instream.read (buffer);
} catch (IOException e) {
E.printstacktrace ();
}
str = new String (buffer);
Bundle bundle = new bundle ();
Bundle.get (str);
Message message = new Message ();
Message.setdata (bundle);
Myhandler.sendmessage (message);
}
}
};

Send Thread
Private class Sendthread extends Thread {
Private OutputStream outstream = null;

Private String str = NULL;

Sendthread (socket socket) {
try {
OutStream = Socket.getoutputstream ();
} catch (IOException e) {
E.printstacktrace ();
}
}

public void Run () {
while (true) {
str = Edsend.gettext (). toString (). Trim ();
PrintStream pt = new PrintStream (OutStream);
Pt.print (str);
New Thread (Receive). Start ();
// }
}
}

protected void OnDestroy () {
TODO auto-generated Method Stub
Super.ondestroy ();
if (Receive! = null) {
Isreceive = false;
(Thread) Receive). Interrupt ();
}
}

}

Android Connection server-side socket

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.