一、简单连接
net.DialTCP("tcp", nil, tcpAddr)
net.ListenTCP(协议, addr)
net.ResolveTCPAddr(协议, 端口)
服务端
package main
import (
"fmt"
"net"
)
func main() {
tcpAddr, _ := net.ResolveTCPAddr("tcp", ":8888")
listener, _ := net.ListenTCP("tcp", tcpAddr)
for {
conn, err := listener.AcceptTCP()
if err != nil {
fmt.Println(err)
return
}
go handleConnection(conn)
}
}
func handleConnection(conn *net.TCPConn) {
fmt.Println(conn.RemoteAddr().String())
}
客户端
package main
import "net"
func main() {
tcpAddr, _ := net.ResolveTCPAddr("tcp", ":8888")
_, _ = net.DialTCP("tcp", nil, tcpAddr)
}
二、客户端通过代码发送消息
服务端
package main
import (
"fmt"
"net"
)
func main() {
tcpAddr, _ := net.ResolveTCPAddr("tcp", ":8888")
listener, _ := net.ListenTCP("tcp", tcpAddr)
for {
conn, err := listener.AcceptTCP()
if err != nil {
fmt.Println(err)
return
}
go handleConnection(conn)
}
}
func handleConnection(conn *net.TCPConn) {
for {
buf := make([]byte, 1024)
n, err := conn.Read(buf)
if err != nil {
fmt.Println(err)
break
}
str := string(buf[0:n])
fmt.Println(conn.RemoteAddr().String(), str)
}
}
客户端
package main
import (
"fmt"
"net"
)
func main() {
tcpAddr, _ := net.ResolveTCPAddr("tcp", ":8888")
conn, _ := net.DialTCP("tcp", nil, tcpAddr)
_, err := conn.Write([]byte("申请出战"))
if err != nil {
fmt.Println(err)
return
}
}
三、客户端通过命令行发送消息
服务端
package main
import (
"fmt"
"net"
)
func main() {
tcpAddr, _ := net.ResolveTCPAddr("tcp", ":8888")
listener, _ := net.ListenTCP("tcp", tcpAddr)
for {
conn, err := listener.AcceptTCP()
if err != nil {
fmt.Println(err)
return
}
go handleConnection(conn)
}
}
func handleConnection(conn *net.TCPConn) {
for {
buf := make([]byte, 1024)
n, err := conn.Read(buf)
if err != nil {
fmt.Println(err)
break
}
str := string(buf[0:n])
fmt.Println(conn.RemoteAddr().String(), str)
}
}
客户端
package main
import (
"bufio"
"fmt"
"net"
"os"
)
func main() {
tcpAddr, _ := net.ResolveTCPAddr("tcp", ":8888")
conn, _ := net.DialTCP("tcp", nil, tcpAddr)
reader := bufio.NewReader(os.Stdin)
for {
bytes, _, _ := reader.ReadLine()
_, err := conn.Write(bytes)
if err != nil {
fmt.Println(err)
break
}
}
}
四、服务端发送消息
服务端
package main
import (
"fmt"
"net"
)
func main() {
tcpAddr, _ := net.ResolveTCPAddr("tcp", ":8888")
listener, _ := net.ListenTCP("tcp", tcpAddr)
for {
conn, err := listener.AcceptTCP()
if err != nil {
fmt.Println(err)
return
}
go handleConnection(conn)
}
}
func handleConnection(conn *net.TCPConn) {
for {
buf := make([]byte, 1024)
n, err := conn.Read(buf)
if err != nil {
fmt.Println(err)
break
}
str := string(buf[0:n])
fmt.Println(conn.RemoteAddr().String(), str)
_, err = conn.Write([]byte("收到了" + str))
if err != nil {
fmt.Println(err)
break
}
}
}
客户端
package main
import (
"bufio"
"fmt"
"net"
"os"
)
func main() {
tcpAddr, _ := net.ResolveTCPAddr("tcp", ":8888")
conn, _ := net.DialTCP("tcp", nil, tcpAddr)
reader := bufio.NewReader(os.Stdin)
for {
bytes, _, _ := reader.ReadLine()
_, err := conn.Write(bytes)
if err != nil {
fmt.Println(err)
break
}
rb := make([]byte, 1024)
rn, _ := conn.Read(rb)
fmt.Println(string(rb[0:rn]))
}
}