It’s all in the notes

Requirements:

The server establishes a UDP listener on port 8888 of the local host

Receive client messages in a loop and automatically reply “read XXX” no matter what the client says.

If the client says “IM off”, reply with “bye” and disconnect

Dial-up to port 8888 of the server host to establish the connection

The loop reads a line of user input from standard input (command line) and sends it to the server

Receives and prints the server information. If the message is bye, exit the program

First, we simply implement the communication based on UDP protocol.

package main

import (
	"fmt"
	"net"
	"os"
)

func ServerHandleError(err error,when string) {
	iferr ! =nil {
		fmt.Println(err,when)
		os.Exit(1)}}func main(a) {
	// Parse to get the UDP address
	udpAddr, err := net.ResolveUDPAddr("udp"."127.0.0.1:8888")
	ServerHandleError(err,"net.ResolveIPAddr")

	// Set up a UDP listener at the UDP address to get a connection
	udpConn, err := net.ListenUDP("udp", udpAddr)
	ServerHandleError(err,"net.Listen")

	defer udpConn.Close()
	// Create a buffer
	buffer := make([]byte.1024)
	// Use connect IO
	// Read the content from the connection and throw it into the buffer
	n, remoteAddr, err := udpConn.ReadFromUDP(buffer)
	ServerHandleError(err,"ReadFromUDP")
	fmt.Printf("Read from %v: %s\n",remoteAddr,string(buffer[:n]))

	// Write content to the client
	udpConn.WriteToUDP([]byte("I have read it!"),remoteAddr)
}
Copy the code
package main

import (
	"fmt"
	"net"
	"os"
)

func ClientHandleError(err error,when string) {
	iferr ! =nil {
		fmt.Println(err,when)
		os.Exit(1)}}func main(a) {
	// Dial to request a connection
	conn, err := net.Dial("udp"."127.0.0.1:8888")
	ClientHandleError(err,"net.Dial")

	defer conn.Close()

	// Hold the connection IO

	// Write information to the connection

	n, err := conn.Write([]byte("Only main inside main can run."))
	ClientHandleError(err,"conn.Write")
	fmt.Printf("Successfully write %d bytes \n",n)

	// Create buffer
	buffer := make([]byte.1024)
	n, err = conn.Read(buffer)
	ClientHandleError(err,"conn.Read")
	fmt.Println(string(buffer[:n]))
}
Copy the code

After the improvement:

package main
/ * requirements: The server establishes a UDP listening loop on port 8888 on the host to receive messages from the client. No matter what the client says, it automatically replies with "read XXX". If the client says "IM off", reply with "bye" and disconnect. The connection loop reads a line of user input from standard input (command line), sends the server to receive and print the server information, and if the message is "bye", exits the program */
import (
	"fmt"
	"net"
	"os"
)


func ServerHandleError(err error,when string) {
	iferr ! =nil {
		fmt.Println(err,when)
		os.Exit(1)}}func main(a) {
	// Parse to get the UDP address
	udpAddr, err := net.ResolveUDPAddr("udp"."127.0.0.1:8888")
	ServerHandleError(err,"net.ResolveIPAddr")

	// Set up a UDP listener at the UDP address to get a connection
	udpConn, err := net.ListenUDP("udp", udpAddr)
	ServerHandleError(err,"net.Listen")

	defer udpConn.Close()
	// Create a buffer
	buffer := make([]byte.1024)
	// Use connect IO
	// Read the content from the connection and throw it into the buffer
	for{
		n, remoteAddr, err := udpConn.ReadFromUDP(buffer)
		ServerHandleError(err,"ReadFromUDP")
		clientMsg := string(buffer[:n])
		fmt.Printf("Read from %v: %s\n",remoteAddr,clientMsg)

		// Write content to the client
		ifclientMsg ! ="im off"{
			udpConn.WriteToUDP([]byte("I have read it!"+clientMsg), remoteAddr)

		}else{
			udpConn.WriteToUDP([]byte("bye"),remoteAddr)
		}
	}

}
Copy the code
package main

import (
	"bufio"
	"fmt"
	"net"
	"os"
)

func ClientHandleError(err error,when string) {
	iferr ! =nil {
		fmt.Println(err,when)
		os.Exit(1)}}func main(a) {
	// Dial to request a connection
	conn, err := net.Dial("udp"."127.0.0.1:8888")
	ClientHandleError(err,"net.Dial")

	defer conn.Close()
	// Create buffer
	buffer := make([]byte.1024)
	reader := bufio.NewReader(os.Stdin)
	for{
		lineBytes, _, _ := reader.ReadLine()
		// Write information to the connection
		n, err := conn.Write(lineBytes)
		ClientHandleError(err,"conn.Write")

		fmt.Printf("Successfully write %d bytes \n",n)

		n, err = conn.Read(buffer)
		ClientHandleError(err,"conn.Read")
		fmt.Println("Server:"+string(buffer[:n]))

		if string(buffer[:n]) == "fuckoff"{
			conn.Write([]byte("Woo woo woo"))
			break
		}

	}
	fmt.Println("game over!")}Copy the code