Go core development study notes (thirty) - TCP / IP, network programming

TCP / IP knowledge skipped roughly summarize points

  1. Appreciated that the OSI seven-layer model theory, be understood that four-layer structure TCP / IP model: the application layer, transport layer, network layer, network interface layer.
  2. TCP / IP protocol contains a lot of protocols, TCP and IP are only two among the most representative.
  3. C / S structure: the IM representatives belong to programming TCP SOCKET; B / S structure: Representative mall JD, program belonging to HTTP, TCP is part of HTTP / IP protocol.
  4. About Network section, you must be a deep understanding of TCP / IP, in order to better follow-up development, the Department will not repeat them.
  5. Port less open, to open more than one copy of the risk, the server can open ports less and less open; the system must be pure version; single-port corresponding to a single program, can not be reused;

TCP SOCKET programming Client-side and Server side

  1. Multi-Client generally correspond minority Server.
  2. The processing flow of the server:
    listening port (Listening);
    receiving client TCP requests to establish client and server-side link;
    create goroutine, processing the link request (Client by sending a request packet).
  3. Process flow Client:
    link establishment and service side (listening on port linked server random-defined port);
    send request data, the receiving server returns the results of data;
    close links (TCP accounting for resource consumption, similar to open the file you want to defer shut down to prevent memory leakage);

Actual practice, the Client and Server to write a C / S model for transmitting and receiving information, an input terminal, an output terminal

server-side analysis and comments:

package main

import (
	"fmt"
	"net"
)

func recv(conn net.Conn) {
	//循环接收客户端发送来的请求
	defer conn.Close()    //如果不关闭服务器因为连接没有释放,后续客户端无法登陆了
	for {
		//创建一个新的切片
		buf := make([]byte,1024)
		//fmt.Printf("服务器在等待客户端%v发送信息\n",conn.RemoteAddr().String())
		n, err := conn.Read(buf)  //如果客户端conn不发信息,没有write操作,会一直阻塞,优化做一个timeout
		if err != nil {
			return
		}
		//显示信息到服务器终端,buf[:n]是真正读到的信息,否则切片后一大串东西都会出来,很乱套
		fmt.Print(string(buf[:n]))
	}
}

func main() {
	/*
	需求分析:
	1. 服务端: 监听端口6666,要为多个客户端提供服务;
	   不能阻塞,所以每个客户端请求都用一个goroutine提供服务;
		MPG模型,P调度处一个goroutine为客户端x提供服务,实际上是不同的G在为不同的client提供服务;
		全部开协程,就变成了并发的了,同时进行请求响应。
	2. 客户端: 端口随机,通过Socket发送请求指定到服务器端6666端口
	 */

	/*
	服务器端代码编写: package net中
	大部分使用者只需要Dial、Listen和Accept函数提供的基本接口,以及相关的Conn和Listener接口。
	crypto/tls包提供了相同的接口和类似的Dial和Listen函数。

	Dial函数和服务端建立连接,Listen函数创建的服务端。
	 */
	listener, err := net.Listen("tcp", "0.0.0.0:6666")   //返回listener为接口类型资源
	if err != nil {
		fmt.Println("连接错误,程序退出")  //监听都错误,后面不用玩了
		return
	}
	defer listener.Close()   //类似文件,及时关闭
	//监听成功,获得ln为&{0xc00008e000}
	fmt.Printf("监听成功,获得lnr为%v\n",listener) //相当于监听成功就跑路了,所以需要for循环。
	for {
		//关于conn接口类型资源拥有的方法:
		/*
		// Read从连接中读取数据
		   // Read方法可能会在超过某个固定时间限制后超时返回错误,该错误的Timeout()方法返回真
		   Read(b []byte) (n int, err error)
		   // Write从连接中写入数据
		   // Write方法可能会在超过某个固定时间限制后超时返回错误,该错误的Timeout()方法返回真
		   Write(b []byte) (n int, err error)
		   // Close方法关闭该连接
		   // 并会导致任何阻塞中的Read或Write方法不再阻塞并返回错误
		   Close() error
		   // 返回本地网络地址
		   LocalAddr() Addr
		   // 返回远端网络地址
		   RemoteAddr() Addr
		 */
		conn, err := listener.Accept()  // Accept等待并返回下一个连接到该接口的连接, conn为接口类型实例,通俗点,通信连接线对象
		if err != nil {
			continue    //这里出错了不要使用return或者break,因为并发连接千万,不能因为一个就终止服务器监听
		} else {
			fmt.Printf("Accept() successed conn = %v\n",conn)
			fmt.Printf("客户端IP= %v\n",conn.RemoteAddr().String())   // 返回远端网络地址

		}
		
		//起一个协程为连接进来的客户端提供一对一服务,监听是通过服务器主程序
		// 端口仍然是同一个,类似前台,但是提供读写等,为客户端服务的是协程
		go recv(conn)
	}
}

Client Client and annotation analysis:

package main

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

func main() {
	//客户端使用net.Dial()来完成,无需要监听,只需要conn即可
	conn, err := net.Dial("tcp","127.0.0.1:6666")
	if err != nil {
		fmt.Println("连接错误,错误原因为:",err,"请重新连接")
		return
	} else {
		fmt.Println("conn is successfully connect~",conn)
	}

	reader := bufio.NewReader(os.Stdin)

	for {
		ln, err := reader.ReadString('\n')
		if err != nil {
			fmt.Println("Reading String Error!")
		}


		ln = strings.Trim(ln,"\r\n")
		if ln == "exit" {
			fmt.Println("客户端由于exit命令退出")
			break
		}

		_, err = conn.Write([]byte(ln + "\n"))
		if err != nil {
			fmt.Println("Writing error!",err)
		}
	}
}
Published 49 original articles · won praise 18 · views 3995

Guess you like

Origin blog.csdn.net/weixin_41047549/article/details/90677920