第13章_用Go语言进行网络服务的开发《Go语言从入门到精通课件》.pptxVIP

  • 11
  • 0
  • 约7.84千字
  • 约 47页
  • 2022-02-23 发布于河北
  • 举报

第13章_用Go语言进行网络服务的开发《Go语言从入门到精通课件》.pptx

;;;C/S架构与B/S架构的概念;;Socket通讯;;基于TCP的Socket通讯;;//?在本机的8818端口监听TCP协议的连接 listenerT,?errT?:=?net.Listen(tcp,?:8818) if?errT?!=?nil?{ ????fmt.Printf(监听端口时发生错误:%v\n,?errT.Error()) ????return } //?循环监听,接受连接并对每个连接新建一个goroutine处理 for?{ ????connectionT,?errT?:=?listenerT.Accept() ????if?errT?!=?nil?{ ????????fmt.Printf(接受连接时发生错误:%v\n,?errT.Error()) ????????//?此时连接无效,直接继续循环而不启动goroutine ????????continue ????} ????//?新建goroutine来处理连接 ????go?connectionHandler(connectionT) } ;func?connectionHandler(connectionA?net.Conn)?{ ????defer?connectionA.Close() //?确保连接最终会被关闭 ????messageCountT?:=?0 ???? ????for?{ /

您可能关注的文档

文档评论(0)

1亿VIP精品文档

相关文档