TCP通信需求
服务端: 服务端在本机的8888端口建立TCP监听 为接入的每一个客户端开辟一条独立的协程 循环接收客户端消息,不管客户端说什么,都自动回复“已阅xxx” 客户端: 如果客户端说的是“im off”,则回复“bye”并断开其连接 拨号连接服务端主机的8888端口,建立连接 循环从标准输入(命令行)读取一行用户输入,向服务端发送 接收并打印服务端消息,如果消息是“bye”,就退出程序
服务端:
package main
import (
"net"
"fmt"
"os"
)
func ServerHandleError(err error, when string) {
if err != nil {
fmt.Println(err, when)
os.Exit(1)
}
}
func main() {
listener, err := net.Listen("tcp", "127.0.0.1:8888")
ServerHandleError(err, "net.Listen")
for {
conn, e := listener.Accept()
ServerHandleError(e, "listener.Accept()")
go ChatWith(conn)
}
}
func ChatWith(conn net.Conn) {
buffer := make([]byte, 1024)
for {
n, err := conn.Read(buffer)
ServerHandleError(err, "conn.Read(buffer)")
clientMsg := string(buffer[0:n])
fmt.Printf("收到%v的消息:%s\n", conn.RemoteAddr(), clientMsg)
if clientMsg != "im off" {
conn.Write([]byte("已阅:" + clientMsg))
} else {
conn.Write([]byte("bye"))
break
}
}
conn.Close()
fmt.Printf("客户端%s已经断开连接\n", conn.RemoteAddr())
}
客户端:
package main
import (
"net"
"fmt"
"os"
"bufio"
)
func ClientHandleError(err error, when string) {
if err != nil {
fmt.Println(err, when)
os.Exit(1)
}
}
func main() {
conn, err := net.Dial("tcp", "127.0.0.1:8888")
ClientHandleError(err, "net.Dial")
buffer := make([]byte, 1024)
reader := bufio.NewReader(os.Stdin)
for {
lineBytes, _, _ := reader.ReadLine()
conn.Write(lineBytes)
n, _ := conn.Read(buffer)
serverMsg := string(buffer[0:n])
fmt.Println("服务端:", serverMsg)
if serverMsg == "bye" {
break
}
}
fmt.Println("GAME OVER!")
}
结果
UDP通信需求
服务端: 服务端在本机的8888端口建立UDP监听,得到广口连接 循环接收客户端消息,不管客户端说什么,都自动回复“已阅xxx” 如果客户端说的是“im off”,则回复“bye”xxx” 客户端: 拨号连接服务端主机的8888端口,建立UDP连接 循环从标准输入(命令行)读取一行用户输入,向服务端发送 接收并打印服务端消息,如果消息是“bye”,就退出程序
服务端:
package main
import (
"net"
"fmt"
"os"
)
func main() {
udpAddr, err := net.ResolveUDPAddr("udp", "localhost:8888")
ServerHandleError(err, "net.ResolveUDPAddr")
udpConn, err := net.ListenUDP("udp", udpAddr)
ServerHandleError(err, "net.ListenUDP")
buffer := make([]byte, 1024)
for {
n, remoteAddr, _ := udpConn.ReadFromUDP(buffer)
clientMsg := string(buffer[:n])
fmt.Printf("收到来自%v的消息:%s\n", remoteAddr, clientMsg)
if clientMsg != "im off"{
udpConn.WriteToUDP([]byte("已阅:"+clientMsg), remoteAddr)
}else{
udpConn.WriteToUDP([]byte("fuckoff"), remoteAddr)
}
}
}
func ServerHandleError(err error, when string) {
if err != nil {
fmt.Println(err, when)
os.Exit(1)
}
}
客户端:
package main
import (
"fmt"
"os"
"net"
"bufio"
)
func main() {
conn, e := net.Dial("udp", "127.0.0.1:8888")
ClientHandleError(e,"net.Dial")
reader := bufio.NewReader(os.Stdin)
buffer := make([]byte, 1024)
for {
lineBytes, _, _:= reader.ReadLine()
conn.Write(lineBytes)
n, _ := conn.Read(buffer)
serverMsg := string(buffer[:n])
fmt.Println("服务端:",serverMsg)
if serverMsg == "fuckoff"{
conn.Write([]byte("吾去也!"))
break
}
}
fmt.Println("GAME OVER!")
}
func ClientHandleError(err error, when string) {
if err != nil {
fmt.Println(err, when)
os.Exit(1)
}
}
结果: D:\go_work\src\udp>Server.exe 收到来自127.0.0.1:57267的消息:test 收到来自127.0.0.1:57267的消息:im off 收到来自127.0.0.1:57267的消息:吾去也!
HTTP 发起POST、GET请求
POST请求 对https://httpbin.org/post发起post请求,携带表单数据,键值自拟,打印回复的内容
package main
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
)
func main() {
resp, err := http.Post("https://httpbin.org/post", "application/x-www-form-urlencoded", strings.NewReader("name=user1&age=12"))
PostHandleError(err, "http.Post")
defer resp.Body.Close()
bytes, _ := ioutil.ReadAll(resp.Body)
fmt.Println(string(bytes))
}
func PostHandleError(err error, when string) {
if err != nil {
fmt.Println(err, when)
os.Exit(1)
}
}
GET请求 对https://httpbin.org/get发起get请求,键值自拟,打印回复的内容
package main
import (
"fmt"
"io/ioutil"
"net/http"
"os"
)
func main() {
resp, err := http.Get("https://httpbin.org/get?name=golang")
GetHandleError(err, "http.Get")
defer resp.Body.Close()
bytes, _ := ioutil.ReadAll(resp.Body)
fmt.Println(string(bytes))
}
func GetHandleError(err error, when string) {
if err != nil {
fmt.Println(err, when)
os.Exit(1)
}
}
|