github.com/keysonZZZ/kmg@v0.0.0-20151121023212-05317bfd7d39/kmgNet/netTester/writeOnly.go (about)

     1  package netTester
     2  
     3  import (
     4  	"net"
     5  	"time"
     6  
     7  	"github.com/bronze1man/kmg/kmgNet"
     8  	"github.com/bronze1man/kmg/kmgTime"
     9  )
    10  
    11  //client只写入,不读取
    12  func writeOnly(listenerNewer ListenNewer, Dialer DirectDialer, debug bool) {
    13  	kmgTime.MustNotTimeout(func() {
    14  
    15  		toWrite := []byte("hello world")
    16  		listener := listenAccept(listenerNewer, func(c net.Conn) {
    17  			defer c.Close()
    18  			for i := 0; i < 2; i++ {
    19  				mustReadSame(c, toWrite)
    20  				time.Sleep(time.Microsecond)
    21  			}
    22  		})
    23  		defer listener.Close()
    24  		conn1, err := Dialer()
    25  		mustNotError(err)
    26  		if debug {
    27  			conn1 = kmgNet.NewDebugConn(conn1, "writeOnly")
    28  		}
    29  		defer conn1.Close()
    30  		for i := 0; i < 2; i++ {
    31  			_, err = conn1.Write(toWrite)
    32  			mustNotError(err)
    33  			time.Sleep(time.Microsecond)
    34  		}
    35  		conn1.Close()
    36  
    37  		listener.Close()
    38  	}, time.Second)
    39  
    40  }