Explain in detail the socket communication of Android based on TCP and UDP protocols

Originally, I wanted to talk about the basic knowledge points of network communication. I found it too boring, but I often asked questions in this regard in the written examination. Therefore, the Xiaobian will put the knowledge points of communication in the interview, because this knowledge point will be used in the interview in practice

1. What is a socket?

2. Socket communication model:

Analysis of socket communication implementation steps:

Step 1: create ServerSocket and socket

Step 2: open the input / output stream of the socket connected to

Step 3: read / write the socket according to the protocol

Step 4: close the I / O stream and socket

OK, let's write a simple example. After opening the server, the client clicks the button, then links to the server, and sends a string to the server, indicating that the server is linked through the socket~

1、 1. Writing based on tcpsocket server:

The server needs to do these things:

Step 1: create a ServerSocket object and bind the listening port

Step 2: call the accept () method to listen for the client's request

Step 3: after the connection is established, read the request information sent by the client through the input stream

Step 4: send response information to the client through output step 5: close related resources

Code implementation:

Create a java project and paste the Java code in it! Here, you can use eclipse to write the server side and as to write the Android side

Then we run the code and the console will print:

OK, next to the Android client!

2. Preparation of socket client: the client needs to do these things:

Step 1: create a socket object to indicate the address and pin number of the server to be linked

Step 2: after the link is established, send the request information to the server through output

Step 3: obtain the information of the server response through the output stream

Step 4: close related resources

Code implementation:

MainActivity.java:

Because Android does not allow network operations in the main thread (UI thread), we need to open another thread to connect to the socket!

Operation results:

After clicking the button, the server console Prints:

3. Simple chat room

Then through the above case, we can make a simple chat software. Just know how to implement it here. In practice, we all use the third-party API, such as Netease cloud. I will write a Netease cloud IM communication

Effect picture of implementation:

Run our server first:

Then run our program to two simulators:

Next, let's write the code:

The first is the server, which is to put the operation of reading and writing sockets into the user-defined thread. After creating the ServerSocket, call the accept method circularly. When a new client accesses, add the socket to the collection, and create a new thread in the online process pool!

In addition, in the method of reading information, judge the input string. If it is a bye string, remove the socket from the collection and close it!

Then to the client, the difficulty of the client is to open up another thread, because Android does not allow network operation directly in the main thread, and it does not allow UI operation in threads outside the main thread. Here, the practice is to create a new thread and update the UI through hanlder. It is not recommended to do so directly in actual development!!!

Layout files: activity_ main.xml:

MainActivity.java:

2、 Socket communication based on UDP protocol

The biggest difference between TCP and UDP is whether the client and server need to establish a connection before data transmission,

TCP: before transmission, open the server, accept, and other client access, then obtain the client socket, and then perform IO operation, while UDP does not

UDP: take datagram as the transmission carrier of data. When transmitting, first define the transmitted data as datagram, specify the socket (host address and port number) to which the data will arrive in the datagram, and then send the data in the form of datagram

1. Server implementation steps:

Step 1: create datagram socket and specify the port number

Step 2: create datagram packet

Step 3: receive the data information sent by the client

Step 4: read data

Example code:

2. Client implementation steps:

Step 1: define sending information

Step 2: create a datagram packet containing the information to be sent

Step 3: create datagram socket

Step 4: send data

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>