Cold wind
Since the SOCKET is used to connect the computer on the network, there are two parts:
First, the Client on Android
Second, the Server on the PC
The implementation code above Android is as follows:
Package Hello. MySocket;
Import java. io. BufferedReader;
Import java. io. BufferedWriter;
Import java. io. IOException;
Import java. io. InputStreamReader;
Import java. io. OutputStreamWriter;
Import java. io. PrintWriter;
Import java.net. Socket;
Import java.net. UnknownHostException;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. util. Log;
Import android. view. View;
Import android. widget. Button;
Import android. widget. EditText;
Import android. widget. TextView;
Public class MySocket extends Activity {
Private final String DEBUG_TAG = "lengfeng ";
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Log. v ("lengfeng", "Android Start Connect ");
Socket socket = null;
String mesg = "my name is log ";
Try {
Socket = new Socket ("10.0.2.2", 54321 );
// Send information to the server
PrintWriter out = new PrintWriter (new BufferedWriter (new OutputStreamWriter (socket. getOutputStream (), true );
Out. println (mesg );
// Accept Server Information
BufferedReader br = new BufferedReader (new InputStreamReader (socket. getInputStream ()));
String mstr = br. readLine ();
If (mstr! = Null)
{
Log. e (DEBUG_TAG, mesg );
} Else
{
Log. e (DEBUG_TAG, "data error ");
}
Out. close ();
Br. close ();
Socket. close ();
} Catch (UnknownHostException e ){
E. printStackTrace ();
} Catch (IOException e ){
E. printStackTrace ();
} Catch (Exception e)
{
Log. e (DEBUG_TAG, e. toString ());
}
}
}
Create a java project for the Server on the PC
Import java. io. BufferedReader;
Import java. io. BufferedWriter;
Import java. io. IOException;
Import java. io. InputStreamReader;
Import java. io. OutputStreamWriter;
Import java. io. PrintWriter;
Import java.net. ServerSocket;
Import java.net. Socket;
Public class mypolicerver {
/**
* @ Param args
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub
System. out. print ("myassumerver Start Runing ");
Try {
ServerSocket serverSocket = new ServerSocket (54321 );
While (true)
{
System. out. println ("receive user connection :");
// Accept client requests
Socket client = serverSocket. accept ();
System. out. println ("accept :");
Try
{
// Accept client information
BufferedReader in = new BufferedReader (new InputStreamReader (client. getInputStream ()));
String str = in. readLine ();
System. out. println ("read:" + str );
// Send messages to the server
PrintWriter out = new PrintWriter (new BufferedWriter (new OutputStreamWriter (client. getOutputStream (), true );
Out. println ("Server Message:" + str );
In. close ();
Out. close ();
} Catch (Exception ex)
{
System. out. println (ex. getMessage ());
Ex. printStackTrace ();
}
Finally
{
Client. close ();
System. out. println ("close ");
}
}
} Catch (IOException e ){
System. out. println (e. getMessage ());
}
}
}