Android uses socket to communicate with a local PC

Source: Internet
Author: User

1. Create a java project first. Use it as a server-side program. The Code is as follows:

Package com. server;

Import java. io. IOException;
Import java. io. OutputStream;
Import java.net. ServerSocket;
Import java.net. Socket;

Public class Myserver {
Public static void main (String [] args ){
Try {
ServerSocket server = new ServerSocket (5000 );
While (true ){
Socket client = server. accept ();
OutputStream out = client. getOutputStream ();
String msg = "Hello android ";
Out. write (msg. getBytes ());
Client. close ();

}
} Catch (IOException e ){

E. printStackTrace ();
}
}

}

 

2. Create an android client. The Code is as follows:

Package com. android;

Import java. io. IOException;
Import java. io. InputStream;
Import java.net. InetAddress;
Import java.net. Socket;
Import java.net. UnknownHostException;

Import android. app. Activity;
Import android. OS. Bundle;
Import android. util. Log;
Import android. widget. TextView;

Public class SocketTestActivity extends Activity {
Private TextView myTextView;

@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
MyTextView = (TextView) findViewById (R. id. msgTextView01 );
MyTextView. setText ("dd ");
Try {

Socket socket = new Socket ("127.0.0.1", 5000 );
MyTextView. setText ("ff ");
InputStream in = socket. getInputStream ();
Byte [] buffer = new byte [in. available ()];
In. read (buffer );
String msg = new String (buffer );
System. out. println (msg );
} Catch (UnknownHostException e ){
 
E. printStackTrace ();
} Catch (IOException e ){
MyTextView. setText ("ee ");
E. printStackTrace ();
}
}
}

3. Then run the program in the java project, check the port, and wait for the listener.

4. Run the android Application on the client and report an error. The socket connection cannot be established. The error message is java.net. PlainSocketImpl. connect. This indicates that the connection is successful because of an incorrect IP address or port number.

It took a long time to check the cause on the Internet. Some people say that the ip address is changed to 10.0.2.0. The result is no. 10.0.2.2 is the IP address of the simulator, which is equivalent to the local 127.0.0.1. is the IP address assigned by the android router to each simulator.

Enter cmd, enter ipconfig, and directly use the IP address.

Later, port ing was performed and telnet localhost 5554 using the command,

Diret tcp: 5000: 6000 does not work either. Later, it was found that the request was forwarded on the two-day simulator and the information needed to be configured was not capable of communication between the simulator and the PC.

Finally, I tried to write the port number as the simulator's port number. I am 5554 or 5556, but it still doesn't work.

This is killing me.

This is the king, and this is the correct solution:

 

This is written on the official website of android: local network connection is restricted. Each simulator runs on a vro in the background. The simulator cannot access a physical router. The simulator interaction information may be blocked by the local firewall or by the firewall you connect. That is to say, the two simulators can communicate directly through request forwarding, And the simulators and PCs cannot communicate.

 

My solution: Use a real machine to test and pass. There is no real machine, so there is no way. I hope that those who have not solved the problem will not waste their time. We hope Google can solve this problem quickly.


From Yanyun's column

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.