Go Write TCP server/client Basics: Goland Learning Record (1)

About: GO language: Socket TCP

Use go language to write TCP server and client, the most core is net this package. Using this package, we can do TCP development

Server code

/* * Copyright(C),2019-2020, email: [email protected] * Author: DAO * Version: 1.0.0 * Date: 2021/6/11 16:35 * Description: * */

package main

import (
	"fmt"
	"io"
	"log"
	"net"
	"time"
)

func main(a) {
	// The protocol is TCP and the listening address is port
	listener, err := net.Listen("tcp".": 10001")
	iferr ! =nil {
		fmt.Println("Error:"+"TCP protocol is used, listening error."."server")
		log.Fatal(err)
	}
	// Close the listening port
	defer listener.Close()
	fmt.Println("Ready to listen.")
	for {
		// Use conn to receive links
		conn, err := listener.Accept()
		iferr ! =nil {
			fmt.Println("Error:"+"Error receiving link with conn."."server")
			log.Fatal(err)
		}
		// Define a slice
		buf := make([]byte.1024.1024)
		// The size of the received content.
		n, err := conn.Read(buf)
		iferr ! =nil&& err ! = io.EOF {// The other party disconnects.
			fmt.Println("Error:"+"The other party disconnects."."server")
			fmt.Println("Error:"+err.Error(), "server")
			log.Fatal(err)
		}
		data := string(buf[0:n])
		fmt.Println("Received:" + data)
		fmt.Println("(return packet) The server receives: + data)
		conn.Write([]byte("(return packet) The server receives: + data)) Conn's wirte method returns the data to the client.

		// Sleep for 1 second before closing the link and then end the loop.
		time.Sleep(1 * 1e9)
		fmt.Println("Ready to disconnect from client: rested:"+fmt.Sprintf("% d seconds".1), "server")
		// Prepare to disconnect from the client.
		conn.Close()
	}
}

Copy the code

Client code

/* * Copyright(C),2019-2020, email: [email protected] * Author: DAO * Version: 1.0.0 * Date: 2021/6/11 16:43 * Description: * */

package main

import (
	"fmt"
	"io"
	"log"
	"net"
)

func main(a) {
	// The protocol is TCP, dial-up.
	conn, err := net.Dial("tcp".": 10001")
	iferr ! =nil {
		fmt.Println("Error:"+err.Error(), "server")
		log.Fatal(err)
		return
	}
	fmt.Println("Access IP address is:", conn.RemoteAddr().String())
	fmt.Printf("The address and port of the client link is: %v\n", conn.LocalAddr())
	// Send data to the server
	n, err := conn.Write([]byte("Hello, my name is Daolizhe."))
	iferr ! =nil {
		fmt.Println("Error:"+err.Error(), "server")
		log.Fatal(err)
		return
	}
	fmt.Println("The data size sent to the server is :", n)
	// Define a slice
	buf := make([]byte.1024.1024)
	// The size of the received content.
	n, err = conn.Read(buf)
	iferr ! =nil&& err ! = io.EOF {// The other party disconnects
		fmt.Println("Error:"+err.Error(), "server")
		log.Fatal(err)
		return
	}
	// Print out the contents of the return packet.
	fmt.Println(string(buf[:n]))
	// Disconnect the TCP connection.
	conn.Close()
}

Copy the code

The effect

The server effect is as follows:

The client effect is as follows:

The source code

Don’t want code: source code files can be private