博客
关于我
高并发C/S的TCP版本golang实现
阅读量:267 次
发布时间:2019-03-01

本文共 1122 字,大约阅读时间需要 3 分钟。

Go?????????????????????????????????????????

???????????????????????????Go?????????????????????????????? goroutine ??????????????????????

package mainimport (	"fmt"	"net"	"strings")// ???Read???????0 ??????func main() {	// ???????	listener, err := net.Listen("tcp", "127.0.0.1:8001")	if err != nil {		fmt.Println("listen err", err)		return	}	defer listener.Close()	for {		// ?????????		fmt.Println("??????????")		conn, err := listener.Accept()		if err != nil {			fmt.Println("listener.Accept err", err)			return		}		// ????????????????		go HandlerConnect(conn)	}}func HandlerConnect(conn net.Conn) {	defer conn.Close()	// ????????Addr	addr := conn.RemoteAddr()	fmt.Println(addr, "???????---")	// ???????????	buf := make([]byte, 4096)	for {		n, err := conn.Read(buf)		if n == 0 {			fmt.Println("?????????????????")			return		}		if err != nil {			fmt.Println("conn Read err", err)			return		}		fmt.Println("????????", string(buf[:n]))		// ???????		conn.Write([]byte(strings.ToUpper(string(buf[:n])))	}}

???????????Go???????????????????????????????? goroutine ??????????????????? goroutine ???????????Go????????

转载地址:http://eqpv.baihongyu.com/

你可能感兴趣的文章
TCP基本入门-简单认识一下什么是TCP
查看>>
tableviewcell 中使用autolayout自适应高度
查看>>
Orcale表被锁
查看>>
svn访问报错500
查看>>
Orderer节点启动报错解决方案:Not bootstrapping because of 3 existing channels
查看>>
org.apache.ibatis.exceptions.PersistenceException:
查看>>
org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned
查看>>
org.apache.ibatis.type.TypeException: Could not resolve type alias 'xxxx'异常
查看>>
org.apache.poi.hssf.util.Region
查看>>
org.apache.xmlbeans.XmlOptions.setEntityExpansionLimit(I)Lorg/apache/xmlbeans/XmlOptions;
查看>>
org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for /
查看>>
org.hibernate.HibernateException: Unable to get the default Bean Validation factory
查看>>
org.hibernate.ObjectNotFoundException: No row with the given identifier exists:
查看>>
SQL-CLR 类型映射 (LINQ to SQL)
查看>>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
查看>>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
查看>>
org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded
查看>>
org.tinygroup.serviceprocessor-服务处理器
查看>>
org/eclipse/jetty/server/Connector : Unsupported major.minor version 52.0
查看>>
org/hibernate/validator/internal/engine
查看>>