Go语言网络通信---tcp群发消息

2021-02-10 04:17

阅读:575

标签:ack   error   accept   cep   highlight   pac   ror   byte   lis   

server

package main

import (
	"fmt"
	"net"
	"os"
	"time"
)

func SHandleError(err error, when string)  {
	if err != nil{
		fmt.Println("err=", err, when)
		os.Exit(1)
	}
}

func main() {
	listener, e := net.Listen("tcp", ":8080")
	SHandleError(e,"net.Listen")

	conns := make([]net.Conn, 0)

	for len(conns) 

 

client

package main

import (
	"fmt"
	"net"
	"os"
)
func CHandleError(err error, when string)  {
	if err != nil{
		fmt.Println("err=", err, when)
		os.Exit(1)
	}
}
func main() {
	conn, e := net.Dial("tcp", "127.0.0.1:8080")
	CHandleError(e, "net.Dial")
	defer func() {
		conn.Close()
		fmt.Println("客户端已退出")
	}()

	buffer := make([]byte, 1024)
	n, e := conn.Read(buffer)
	CHandleError(e ,"conn.Read")
	msg := buffer[:n]
	fmt.Println(string(msg))
}

  

Go语言网络通信---tcp群发消息

标签:ack   error   accept   cep   highlight   pac   ror   byte   lis   

原文地址:https://www.cnblogs.com/yunweiqiang/p/12745575.html


评论


亲,登录后才可以留言!