Easy Tutorial
❮ Verilog2 Tf Sub Php Ajax Cross Border ❯

7.6.4 Socket Communication Based on UDP Protocol

Category Android Basic Introduction Tutorial

Introduction:

>

This section concludes our series on Socket communication with an exploration of UDP-based Socket communication. In our first section, we have already detailed the differences between TCP and UDP. The key distinction between TCP and UDP lies in whether a connection needs to be established between the client and server before data transmission. If you have studied the first two sections on TCP, you know that data transmission involves starting the server, accepting connections, obtaining the client socket, and then performing IO operations. UDP, however, does not require this. UDP uses datagrams as the carrier for data transmission. To transmit data, you first need to define the data as a datagram, specify the destination Socket (host address and port number), and then send the data in the form of a datagram. After that, it's a wait-and-see situation; whether the server receives the data is unknown unless the server responds with a confirmation datagram. Due to time constraints, I won't be writing an Android example; instead, I'll provide Java code directly.


1. Server Implementation Steps:

>

Step 1: Create a DatagramSocket, specifying the port number
Step 2: Create a DatagramPacket
Step 3: Receive data sent by the client
Step 4: Read the data

Sample Code:

public class UPDServer {
    public static void main(String[] args) throws IOException {
        /*
         * Receive data sent by the client
         */
        // 1. Create a server-side DatagramSocket, specifying the port
        DatagramSocket socket = new DatagramSocket(12345);
        // 2. Create a datagram to receive data sent by the client
        byte[] data = new byte[1024]; // Create a byte array to specify the size of the data packet
        DatagramPacket packet = new DatagramPacket(data, data.length);
        // 3. Receive data sent by the client
        System.out.println("****Server is started, waiting for client to send data");
        socket.receive(packet); // This method blocks until a datagram is received
        // 4. Read the data
        String info = new String(data, 0, packet.getLength());
        System.out.println("I am the server, the client said: " + info);

        /*
         * Respond to the client
         */
        // 1. Define the client's address, port number, and data
        InetAddress address = packet.getAddress();
        int port = packet.getPort();
        byte[] data2 = "Welcome!".getBytes();
        // 2. Create a datagram containing the response data
        DatagramPacket packet2 = new DatagramPacket(data2, data2.length, address, port);
        // 3. Respond to the client
        socket.send(packet2);
        // 4. Close resources
        socket.close();
    }
}

2. Client Implementation Steps:

>

Step 1: Define the message to be sent
Step 2: Create a DatagramPacket containing the message to be sent
Step 3: Create a DatagramSocket
Step 4: Send the data

public class UDPClient {
    public static void main(String[] args) throws IOException {
        /*
         * Send data to the server
         */
        // 1. Define the server's address, port number, and data
        InetAddress address = InetAddress.getByName("localhost");
        int port = 8800;
        byte[] data = "Username: admin; Password: 123".getBytes();
        // 2. Create a datagram containing the data to be sent
        DatagramPacket packet = new DatagramPacket(data, data.length, address, port);
        // 3. Create a DatagramSocket object
        DatagramSocket socket = new DatagramSocket();
        // 4. Send the datagram to the server
        socket.send(packet);

        /*
         * Receive the server's response
         */
        // 1. Create a datagram to receive the server's response
        byte[] data2 = new byte[1024];
        DatagramPacket packet2 = new DatagramPacket(data2, data2.length);
        // 2. Receive the server's response
        socket.receive(packet2);
        // 3. Read the data
        String reply = new String(data2, 0, packet2.getLength());
        System.out.println("I am the client, the server said: " + reply);
        // 4. Close resources
        socket.close();
    }
}

Summary:

This section is relatively straightforward. It mainly involves converting data into bytes and placing them into a DatagramPacket, specifying the recipient's IP address and port number when sending. To receive, a byte array is used to buffer the data. Sending requires creating a DatagramSocket object and then calling the send method to send the datagram packet to the recipient. The code in this section is sourced from a Java Socket tutorial on Mocking. For those interested, you can check it out here: Java Socket Application - Communication Made Easy

-1.0 Android Basic Introduction Tutorial

-1.0.1 2015 Latest Android Basic Introduction Tutorial Table of Contents

-1.1 Background and System Architecture Analysis

-1.2 Development Environment Setup

-1.2.1 Developing Android Apps with Eclipse + ADT + SDK

-1.2.2 Developing Android Apps with Android Studio

-1.3 Solving SDK Update Issues

-1.4 Genymotion Emulator Installation

-1.5.1 Git Tutorial for Basic Local Repository Operations

-1.5.2 Git: Setting Up a Remote Repository on GitHub

-1.6 How to Use the 9-Patch Image

-1.7 Interface Prototype Design

-1.8 Project Source Analysis (Various Files, Resource Access)

-1.9 Android Application Signing and Packaging

-1.11 Decompiling APK to Retrieve Code & Resources

-2.1 Concepts of View and ViewGroup

-2.2.1 LinearLayout (Linear Layout)

-2.2.2 RelativeLayout (Relative Layout)

-2.2.3 TableLayout (Table Layout)

-2.2.4 FrameLayout (Frame Layout)

-2.2.5 GridLayout (Grid Layout)

-2.2.6 AbsoluteLayout (Absolute Layout)

-2.3.1 Detailed Explanation of TextView (Text Box)

-2.3.2 Detailed Explanation of EditText (Input Box)

-2.3.3 Button (Button) and ImageButton (Image Button)

-2.3.4 ImageView (Image View)

-2.3.5.RadioButton (Radio Button) & Checkbox (Checkbox)

-2.3.6 ToggleButton (Toggle Button) and Switch (Switch)

-2.3.7 ProgressBar (Progress Bar)

-2.3.8 SeekBar (Drag Bar)

-2.3.9 RatingBar (Star Rating Bar)

-2.4.1 ScrollView (Scroll Bar)

Follow on WeChat

❮ Verilog2 Tf Sub Php Ajax Cross Border ❯