Use Socket on Android

Source: Internet
Author: User

Address: http://www.cnblogs.com/kross/p/3621952.html

 

Considering that this knowledge may be used in the future, I will try to write a Demo. I actually learned it before, but I don't remember it ...... (Don't remember anything you can't use)

Directly go to the topic. Using Socket on Android is actually similar to using Socket in Java. Just pay attention to adding network permissions in Manifest (I used to make this mistake ), in addition, the network request can no longer be in the main thread, and it is basically OK.

Write a server applet first. You can simply use Java to write data, which is simple and easy to write.

    main(String[] args) =  ServerSocket(5417=  ("waiting for..."==  BufferedReader(

My idea is that there is an input box and a button on the mobile phone. You can press the button to send the content in the input box to the server. At first I wrote a very confusing demo. To make it easier to use, I encapsulated the Socket and calledSocketManagerThe Code is as follows:

  Socket client =      String ip = "113.251.172.30"      port = 5417      SocketManager newInstance()   SocketManager() =       sendMessage(String msg) = 

Key points:

1. The IP address is the public IP address of the machine running the Java program lock. cmd-> ipconfig can be found.

2. Make sure that the port number is the same as that on the server. How can I communicate without a single pipe.

3. I used the static factory method. I had to create a new SocketManager every time, because I found that the Socket class can only be used once, and it would be useless to send it again after close.

4. To receive the information, the server must close the client, close the output stream, or socket. close. In short, close is required before the server can receive the information exactly.

Then, I wrote a thread to execute this Socket, because it cannot be run in the main thread. I thought for a long time, can I keep this thread running, then, you only need to call the sendMessage method of this thread to send the message. In this way, you only need to create a new thread. However, after thinking for a long time, I cannot figure it out. It seems that this is not the case, the thread either does not have it. If so, it will execute its run () method with a brain. Do you want to hold it for a long time? Add a while (true) in run )? So how can we call its common methods? In short, I did not expect to use a new thread to send N requests. It seems that we need to use the Service to implement it.

  SocketThread = 

There is nothing to say here. Next we will prepare the control and listener directly in MainActivity, and then OK. Let's take a rough look at the code.

  MainActivity  EditText etInput =  Button bSend =  "MainActivity"== SendButtonListener  = Thread(""

OK, the code is like this. Let's take a look at the final running effect.

Start the Java server program first, and then the result is as follows:

Then we run the program on the mobile phone and send a message:

Let's look at the server-side program:

It seems everything is normal. Come back!

Let's look at the server program:

OK, it seems that there is no problem.

 

How does the server return information to the Socket? I haven't tried it yet. I will try it tomorrow ......

 

Address: http://www.cnblogs.com/kross/p/3621952.html

Sina Weibo: http://weibo.com/KrossFord

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.