Brothers of the previous one
Previous address: https://www.jianshu.com/p/457b24e5e0a5
UDP based
UDP protocol takes data packets as the carrier of data transmission, that is, when data transmission, we need to package data into datagrams, in which the host address and port number are specified, and then the datagrams are sent out, using two key categories:
- Datagram packet
- Datagram socket class for end-to-end communication
The server
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); new Thread(){ @Override public void run() { try { //Step 1 create datagram socket on the server side and specify the port DatagramSocket socket = new DatagramSocket(8899); //Step 2 create a datagram to receive the data sent by the client byte[] data =new byte[1024];//Create a byte array to specify the received packet size DatagramPacket packet=new DatagramPacket(data, data.length); //Step ③ receive the data sent by the client System.out.println("server loaded success,waiting..."); socket.receive(packet);//This method will block until a datagram is received //Step 4 read the data String info=new String(data, 0, packet.getLength()); //For the sake of simplicity, the logic of verification is no longer written here //Step ⑤ define the address, port number and data of the client InetAddress address=packet.getAddress(); int port=packet.getPort(); byte[] data2="Welcome!!!".getBytes(); //Step 6 create a datagram containing the data information of the response DatagramPacket packet2=new DatagramPacket(data2, data2.length, address, port); //Step ⑦ respond to the client socket.send(packet2); //Step ⑧ close resources socket.close(); } catch (SocketException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }.start(); } }
client
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); new Thread(){ @Override public void run() { //Step ① define the address, port number and data of the server InetAddress address= null; try { address = InetAddress.getByName("localhost"); } catch (UnknownHostException e) { e.printStackTrace(); } int port=8899; byte[] data="admin:123".getBytes(); //Step 2 create a datagram, including the data information sent DatagramPacket packet=new DatagramPacket(data, data.length, address, port); try { //Step 3 create DatagramSocket object DatagramSocket socket = new DatagramSocket(); //Step 4 send datagram to server socket.send(packet); //Step ⑤ create a datagram to receive the data from the server byte[] data2=new byte[1024]; DatagramPacket packet2=new DatagramPacket(data2, data2.length); //Step 6 receive the data from the server socket.receive(packet2); //Step ⑦ read data String reply=new String(data2, 0, packet2.getLength()); System.out.println("server reply : "+reply); //Step ⑧ close resources socket.close(); } catch (SocketException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }.start(); } }