net 包 dial - golang

     And has recently been dealing with the hardware, but the hardware and interact with many uses socket. So in the process of using both simple to learn a bit dial net package.

     In go, generally it provides five Dial, comprising:

     1.  Dial(network, address string) (Conn, error)

     2. DialTCP(network string, laddr, raddr *TCPAddr) (*TCPConn, error)

     3. DialUDP(network string, laddr, raddr *UDPAddr) (*UDPConn, error)

     4. DialIP(network string, laddr, raddr *IPAddr) (*IPConn, error)

     5. DialUnix(network string, laddr, raddr *UnixAddr) (*UnixConn, error)

 

    Wherein the dial is several other packages, while achieving Conn interface.

    And several other, although the return TCPConn, UDPConn, IPConn, UnixConn. But underlying them are the same as shown below:

 

  

Eventually these returns are returned to netfd structure. And in addition to the remaining three DialUnix before ultimately taking internetSocket () just calling this method, each handling a little different.

The DialUnix final call is a unixSocket ().

If you want to see the specific implementation, go tcpsock, udpsock, ipsock, unixsock look at these files.

Because they basically have the same realization, and the same return. So their use is basically the same, Here's to dial, tcpdial example.

//创建连接
	conn,err := net.Dial("tcp","192.168.1.254:4001")
	//发送数据
	conn.Write([]byte{0x00,0x07,0x00,0x00,0x00,0x06,0x01,0x03,0x00,0x00,0x00,0x0A})
	defer conn.Close()
	buffer := make([]byte,32)
	//读取返回数据
	result,err := conn.Read(buffer)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(result,buffer)
//进行地址化转换
	tcpAddr,err:=net.ResolveTCPAddr("tcp","192.168.1.254:4001")
	//创建一个连接
	conn,err:=net.DialTCP("tcp",nil,tcpAddr)
	if err != nil {
		fmt.Println(err)
	}
	str := []byte{32, 0, 0, 0, 7, 85, 35, 160, 176, 7, 226, 12, 18, 15, 45, 0}

	//向连接端发送一个数据
	_,err=conn.Write(str)
	if err != nil {
		fmt.Println(err)
	}
	//读取连接端返回来的数据,
	result,err:=ioutil.ReadAll(conn)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(string(result))

      As can be seen from these two requests, Dial fill only direct connection address, and the address of dialtcp they advance into the corresponding types may be filled. In the dial, the connection will be filled according to the type of connection you automatically corresponding to the address into the address type.

At the same time, we will find that we dial the buffer used to receive data, and in dialtcp with the ioutil.ReadAll () returns all data.

     What is the difference of these two methods, the first buffer is filled directly after the implementation of the code behind, but you need to pre-defined buffer size. The second ioutil.ReadAll () If can not get at the end, we will wait until the connection times out before the implementation of the code behind. This will lead to blocking for a long time.

 

Published 48 original articles · won praise 17 · views 50000 +

Guess you like

Origin blog.csdn.net/aixinaxc/article/details/88774257