go net bag abbreviated

 

TCP server

go every language can establish a link to create a goroutine to deal with, very easy to use goroutine concurrency and efficient.

General flow of processing procedures TCP server
1. Create and bind Socket: First, use the server socket () function creates a network socket, then bind () function and the socket is bound specified IP ports;

2. listens for requests: Next, the server using listen () function to listen to client requests to bind IP and port;

3. Receive connection: If there is a request over and through a successful three-way handshake to establish a connection, use the accept () function receives the link;

4. Create a link to handle goroutine: function reads the server request data sent by the client read () has been established from the connection described above, and then processed by a write () function transmits the response data to the client

TCP client general process flow
1. The establishment Socket: the client using the same socket () function creates a network socket;

2. Establish a connection: connection: then calls the connect () function incoming IP and port number to establish a connection with the specified server network program;

3. Send request reception response: After the connection is established, the client can () function to send through the write data to the server, and uses the read () function receives a response from the server.

TCP server

main Package 

Import ( 
	"FMT" 
	"log" 
	"NET" 
) 

FUNC chkError (ERR error) { 
	IF ERR = nil {! 
		log.Fatal (ERR); 
	} 
} 

FUNC main () { 
	// create a TCP server 
	tcpaddr, ERR: = net.ResolveTCPAddr ( "tcp4", "127.0.0.1:8081"); 
	chkError (ERR); 
	// listening port 
	tcplisten, ERR2: = net.ListenTCP ( "tcp", tcpaddr); 
	chkError (ERR2); 
	fmt.Println ( "Start Server") 
	. // establish links and deal with 
	Go FUNC () { 
		for { 
			// if the client link over, blocking will return 
			conn, ERR: = tcplisten.AcceptTCP () 
			IF ERR = nil! { 
				fmt.Println ( "ERR the Accept",err)
				continue
			}
			// link has been established with the client, handling business 
			Go FUNC () { 
				for { 
					buf: = the make ([] byte, 512) 
					cnt, ERR: = conn.Read (buf) 
					! = Nil {IF ERR 
						fmt.Println ( "ERR the recv buf", ERR) 
						Continue 
					} 
					// echo 
					IF _, ERR: = conn.Write (buf [: CNT]!); ERR = nil { 
						fmt.Println ( "Write buf BAK ERR", ERR) 
						Continue 
					} 
				} 
			} () 
		} 
	} () 
	// blocked state 
	SELECT {} 
}

TCP Client

main Package Penalty for 

Import ( 
	"fmt" 
	"NET" 
	"Time" 
) 

FUNC main () { 
	// Create a link 1 link remote server, get a link conn. 
	conn, ERR: = net.Dial ( "tcp", "127.0.0.1 : 8081 ") 
	! = nil {IF ERR 
		fmt.Println (" ERR Client Start, Exit ")! 
		return 
	} 
	I: =. 1 
	for { 
		. // write 2 write data link call 
		_, err: = conn.Write ([ ] byte (fmt.Sprintf ( "% S:% D", "Server the Hello", I))) 
		! = nil {IF ERR 
			fmt.Println ( "Write Conn ERR", ERR) 
			return 
		} 

		buf: the make = ([ ] byte, 512) 
		CNT, ERR: = conn.Read (buf) 
		! IF ERR = nil { 
			fmt.Println ( "read buf err")
			return
		}
		fmt.Printf("Server call back:%s,cnt = %d\n",buf,cnt)
		i++
		time.Sleep(1)
	}

  

 

Guess you like

Origin www.cnblogs.com/-wenli/p/12321196.html
Bag