我有一个TCP服务器和客户端,简单的TCP服务器只会接收传入的数据并将其打印出来,而客户端将不断创建套接字连接并在循环中向TCP服务器发送数据。
我得到的信息是,如果TCP连接被正确关闭,这个过程应该继续进行而不会崩溃。
但是,在从客户端到服务器接收了一定量的数据之后,客户端会因错误而崩溃。
total times data send: 16373
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x10d7594]
goroutine 1 [running]:
main.sendData()
/Users/apple/Desktop/Personal/umbrellaserver/src/tests/clinet.go:178
+0xb4
main.main()
/Users/apple/Desktop/Personal/umbrellaserver/src/tests/clinet.go:170
+0x2a
exit status 2
Server.go
package main
import (
"bufio"
"fmt"
"net"
"sync"
)
var wg sync.WaitGroup
var count = 0
var timeX string = ""
var connQueue = make(chan string)
func main() {
tcpListner := startTCPConnection()
incomingTCPListener(tcpListner)
}
//startTCPConnection
func startTCPConnection() net.Listener {
tcpListner, tcpConnectonError := net.Listen("tcp", "localhost:3000")
if tcpConnectonError != nil {
print(tcpConnectonError)
return
}
return tcpListner
}
//incomingTCPListener
func incomingTCPListener(tcpListner net.Listener) {
for {
incomingConnection, incomingConnectionError := tcpListner.Accept()
if incomingConnectionError != nil {
print(incomingConnectionError)
return
}
wg.Add(1)
go processIncomingRequest(incomingConnection)
wg.Wait()
}
}
//processIncomingRequest
func processIncomingRequest(connection net.Conn) {
defer connection.Close()
var scanner = bufio.NewScanner(connection)
var blob = ""
for scanner.Scan() {
fmt.Println("sadd")
text := scanner.Text()
blob += text
}
print(blob)
count++
fmt.Println("totalCount", count)
wg.Done()
}
Client.go
package main
import (
"fmt"
"net"
)
var count = 0
func testJSON2() string {
return `Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32. The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.`
}
func main() {
for i := 0; i < 1000000; i++ {
sendData()
}
}
func sendData() {
connection, connectionError := net.Dial("tcp", "localhost:3000")
defer connection.Close()
if connectionError != nil {
fmt.Println(connectionError)
return
}
newmessage := testJSON2()
connection.Write([]byte(newmessage + "\n"))
count++
fmt.Println(count)
}
有没有办法避免这个崩溃并使其持续运行? 我完全是Go的新手,如果我犯了任何愚蠢的错误,我很抱歉。