Connect to a network computer using Socket in Android

Source: Internet
Author: User

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

 

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.