Go语言服务器开发之简易TCP客户端与服务端实现方法
程序员文章站
2022-06-17 10:09:46
本文实例讲述了go语言服务器开发之简易tcp客户端与服务端实现方法。分享给大家供大家参考。具体实现方法如下:
go语言具备强大的服务器开发支持,这里示范了最基础的服务器开...
本文实例讲述了go语言服务器开发之简易tcp客户端与服务端实现方法。分享给大家供大家参考。具体实现方法如下:
go语言具备强大的服务器开发支持,这里示范了最基础的服务器开发:通过tcp协议实现客户端与服务器的通讯。
一 服务端,为每个客户端新开一个goroutine
复制代码 代码如下:
func serverbase() {
fmt.println("starting the server...")
//create listener
listener, err := net.listen("tcp", "192.168.1.27:50000")
if err != nil {
fmt.println("error listening:", err.error())
return
}
// listen and accept connections from clients:
for {
conn, err := listener.accept()
if err != nil {
fmt.println("error accepting:", err.error())
return
}
//create a goroutine for each request.
go doserverstuff(conn)
}
}
func doserverstuff(conn net.conn) {
fmt.println("new connection:", conn.localaddr())
for {
buf := make([]byte, 1024)
length, err := conn.read(buf)
if err != nil {
fmt.println("error reading:", err.error())
return
}
fmt.println("receive data from client:", string(buf[:length]))
}
}
fmt.println("starting the server...")
//create listener
listener, err := net.listen("tcp", "192.168.1.27:50000")
if err != nil {
fmt.println("error listening:", err.error())
return
}
// listen and accept connections from clients:
for {
conn, err := listener.accept()
if err != nil {
fmt.println("error accepting:", err.error())
return
}
//create a goroutine for each request.
go doserverstuff(conn)
}
}
func doserverstuff(conn net.conn) {
fmt.println("new connection:", conn.localaddr())
for {
buf := make([]byte, 1024)
length, err := conn.read(buf)
if err != nil {
fmt.println("error reading:", err.error())
return
}
fmt.println("receive data from client:", string(buf[:length]))
}
}
二 客户端 连接服务器,并发送数据
复制代码 代码如下:
func clientbase() {
//open connection:
conn, err := net.dial("tcp", "192.168.1.27:50000")
if err != nil {
fmt.println("error dial:", err.error())
return
}
inputreader := bufio.newreader(os.stdin)
fmt.println("please input your name:")
clientname, _ := inputreader.readstring('\n')
inputclientname := strings.trim(clientname, "\n")
//send info to server until quit
for {
fmt.println("what do you send to the server? type q to quit.")
content, _ := inputreader.readstring('\n')
inputcontent := strings.trim(content, "\n")
if inputcontent == "q" {
return
}
_, err := conn.write([]byte(inputclientname + " says " + inputcontent))
if err != nil {
fmt.println("error write:", err.error())
return
}
}
}
//open connection:
conn, err := net.dial("tcp", "192.168.1.27:50000")
if err != nil {
fmt.println("error dial:", err.error())
return
}
inputreader := bufio.newreader(os.stdin)
fmt.println("please input your name:")
clientname, _ := inputreader.readstring('\n')
inputclientname := strings.trim(clientname, "\n")
//send info to server until quit
for {
fmt.println("what do you send to the server? type q to quit.")
content, _ := inputreader.readstring('\n')
inputcontent := strings.trim(content, "\n")
if inputcontent == "q" {
return
}
_, err := conn.write([]byte(inputclientname + " says " + inputcontent))
if err != nil {
fmt.println("error write:", err.error())
return
}
}
}
注:由于liteide不支持同时运行多个程序,所以需要在终端通过 go run 命令来同时运行服务端和(一个或多个)客户端,可观察到服务器对并发访问的支持。
希望本文所述对大家的go语言程序设计有所帮助。
上一篇: linux 安装 MySql 5.7.20 一主多从配置【亲测】
下一篇: c语言面试题__指针篇