练习一下Go语言网络编程, 以下是三个例子。
1. 基础接口
server.go:
package main
import (
"fmt"
"net"
"os"
)
func main() {
fmt.Println(os.Args)
ln, err := net.Listen("tcp", ":8088")
if err != nil {
return
}
for {
conn, err := ln.Accept()
if err != nil {
return
}
fmt.Printf("connect\r\n")
go connectHandle(conn)
}
}
func connectHandle(conn net.Conn) bool{
n, err := conn.Write([]byte("connect ok"))
if err != nil{
fmt.Println("write 2 error")
return false
}
var buf = make([]byte, 20)
n, err = conn.Read(buf)
if err != nil{
fmt.Println("read error")
return false
}
if n != 0 {
fmt.Printf("read %d bytes, content is %s\r\n", n, string(buf[0:n]))
}
defer conn.Close()
return true
}
client.go:
package main
import (
"fmt"
"net"
// "time"
)
func main() {
conn, err := net.Dial("tcp", ":8088")
if err != nil {
return
}
// _, err = fmt.Fprintf(conn, "dial hello")
// if err != nil{
// fmt.Println("write error")
// return
// }
n, err := conn.Write([]byte("dial ok"))
if err != nil{
fmt.Println("write 2 error")
return
}
// time.Sleep(1000)
var buf = make([]byte, 20)
n, err = conn.Read(buf)
if err != nil{
fmt.Println("read error")
return
}
if n != 0 {
fmt.Printf("read %d bytes, content is %s\r\n", n, string(buf[0:n]))
}
defer conn.Close()
}
2. TCP 接口
server.go:
package main
import (
"fmt"
"net"
"os"
)
func main() {
fmt.Println(os.Args)
raddr, err := net.ResolveTCPAddr("tcp", ":8088")
if err != nil{
return
}
ln, err := net.ListenTCP("tcp", raddr)
if err != nil {
return
}
for {
conn, err := ln.Accept()
if err != nil {
return
}
fmt.Printf("connect\r\n")
go connectHandle(conn)
}
}
func connectHandle(conn net.Conn) bool{
n, err := conn.Write([]byte("connect ok"))
if err != nil{
fmt.Println("write 2 error")
return false
}
var buf = make([]byte, 20)
n, err = conn.Read(buf)
if err != nil{
fmt.Println("read error")
return false
}
if n != 0 {
fmt.Printf("read %d bytes, content is %s\r\n", n, string(buf[0:n]))
}
defer conn.Close()
return true
}
client.go:
package main
import (
"fmt"
"net"
)
func main() {
raddr, err := net.ResolveTCPAddr("tcp", ":8088")
if err != nil{
return
}
conn, err := net.DialTCP("tcp", nil, raddr)
if err != nil {
return
}
n, err := conn.Write([]byte("dial ok"))
if err != nil{
fmt.Println("write 2 error")
return
}
var buf = make([]byte, 20)
n, err = conn.Read(buf)
if err != nil{
fmt.Println("read error")
return
}
if n != 0 {
fmt.Printf("read %d bytes, content is %s\r\n", n, string(buf[0:n]))
}
defer conn.Close()
}
3.UDP接口
server.go:
package main
import (
"fmt"
"net"
"os"
)
func main() {
fmt.Println(os.Args)
raddr, err := net.ResolveUDPAddr("udp", "127.0.0.1:8088")
if err != nil{
return
}
conn, err := net.ListenUDP("udp", raddr)
if err != nil {
return
}
for {
fmt.Printf("connect\r\n")
connectHandle(conn)
}
defer conn.Close()
}
func connectHandle(conn *net.UDPConn) bool{
var buf = make([]byte, 20)
n, udpaddr, err := conn.ReadFromUDP(buf)
if err != nil{
fmt.Println("read error")
return false
}
if n != 0 {
fmt.Printf("read %d bytes, content is %s\r\n", n, string(buf[0:n]))
}
_, err = conn.WriteToUDP([]byte("connect ok"), udpaddr)
if err != nil{
fmt.Println("write 2 error")
return false
}
return true
}
client.go:
package main
import (
"fmt"
"net"
)
func main() {
raddr, err := net.ResolveUDPAddr("udp", "127.0.0.1:8088")
if err != nil{
return
}
conn, err := net.DialUDP("udp", nil, raddr)
if err != nil {
return
}
_, err = conn.Write([]byte("dial ok"))
if err != nil{
fmt.Println("write 2 error")
return
}
var buf = make([]byte, 20)
n, err := conn.Read(buf)
if err != nil{
fmt.Println("read error")
return
}
if n != 0 {
fmt.Printf("read %d bytes, content is %s\r\n", n, string(buf[0:n]))
}
defer conn.Close()
}