Android connects to the server Socket and androidsocket

Source: Internet
Author: User

Android connects to the server Socket and androidsocket

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;
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 the sending thread
New Thread (SendThread). start ();
}
});
MyHandler = new Handler () {// UI main thread Message Processing Function

Public void handleMessage (Message msg ){
Bundle bundle = msg. getData ();
String string = bundle. toString ();
EdReceive. setText (string );
}
};
}

// Interface for connecting 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 successful ",
Toast. LENGTH_LONG). show ();
Else
Toast. makeText (getApplicationContext (), "connection failed ",
Toast. LENGTH_LONG). show ();
// Initialize the sending thread
SendThread = new SendThread (socket );
} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
};
// Message receiving Interface
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 );
}
}
};

// Sending 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 ();
}
}

}

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.