Client fails to send data to TCP server in golang?
Asked Answered
V

3

9

I have both TCP server and a client, Simple TCP server will just receive incoming data and print it and the client will be continuously creating a socket connection and send data to TCP server in a loop.

The information I got is that if a TCP connection is properly closed, this process should keep continued without any crashes.

But after some amount of data received from client to server the client crashes with error

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)
}

Is there any way to avoid this crash and make it run continuously? I'm totally new to Go so if I'm making any silly mistake, I'm sorry.

Varmint answered 2/3, 2019 at 3:39 Comment(11)
@ThunderCat Thanks for the info, I will check it and get back to you.Varmint
@ThunderCat I tried it by removing all wg and printing error and return after that. but still, I'm getting the same errors and if you can please tell me what I'm doing wrong with wg hereVarmint
What version of go and what OS are you running on?Friedrich
@Friedrich I'm currently running in both Mac OS Mojave and Linux 16.4Varmint
i see a panic from the connection.Close() in the event Dial fails, otherwise runs ok for me on OSX 10.14.3 and go 1.11.4Friedrich
See play.golang.org/p/1Yez0wttdvO.Sweven
@Friedrich yes, Why is it panicing and if you re-run the client again the server won't respondVarmint
@ThunderCat When I ran the same code locally, I'm getting this error 2019/03/02 11:47:30 dial tcp [::]:60273: connect: resource temporarily unavailable exit status 1Varmint
This is not 'client fails to send'. This is 'client crashes with a SIGSEGV'.Pudency
@Pudency can you explain how to fix this problem?Varmint
@Friedrich this is happened in mac os but not in LinuxVarmint
C
12

Firstly, I would recommend if you want to print to stderr, (i.e. your print calls) use the fmt library

fmt.Fprintln(os.Stderr, "hello world")

Why: Because the print function is not guaranteed to stay in the language. 1


Second, it is common practice to give errors names just as err, you don't have to spell out errors such as tcpConnectionError.


Thirdly, since you are using tcp in connection, connectionError := net.Dial("tcp", "localhost:3000") the server is listening on both ipv6 and ipv4. I have observered at least on a Windows machines the connection open two connections one for ipv4 and ipv6 and then discards the ipv6 connection in favor of ipv4.


Lastly, When a TCP connections closes the port cannot be reused immediately afterwards because the Operating System has to wait for the duration of the TIME_WAIT interval (maximum segment lifetime, MSL).
Your client code is opening a huge amount of tcp connections that are very short lived, and depending on the range of you empheral port range your code may or may not crash. Judging from the amount of 16373, you have the default range. 2

>> sysctl net.inet.ip.portrange.first net.inet.ip.portrange.last
net.inet.ip.portrange.first: 49152
net.inet.ip.portrange.last: 65535

Finally,

If you would like to avoid crashes due to running out ports:
1. Increase ephemeral range of ports
2. Use Docker, containers are on their own network, hence are using a different set of ports bypassing the limited range of ports.
3. Introduce a ticker into your client code, to simulate a connection every x seconds/minutes

package main

import (
    "fmt"
    "net"
    "time"
)

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() {
    max := 1000

    timer1 := time.NewTicker(5 * time.Second)

    i := 0
    for range timer1.C {
        sendData()
        if i == max {
            timer1.Stop()
        }
        i++
    }
}

func sendData() {

    connection, connectionError := net.Dial("tcp", "localhost:3000")
    fmt.Println(connection.LocalAddr())

    if connectionError != nil {
        fmt.Println(connectionError)
        return
    }
    newmessage := testJSON2()
    connection.Write([]byte(newmessage + "\n"))
    count++
    fmt.Println(count)

    err := connection.Close()
    if err != nil {
        fmt.Println(err)
    }
}

References
^1: https://tip.golang.org/pkg/builtin/#print
^2: What is the range of ephemeral ports on mac?
^3: https://www.fromdual.com/huge-amount-of-time-wait-connections

Charmion answered 12/3, 2019 at 18:36 Comment(0)
J
1
if connectionError != nil {
        fmt.Println(connectionError)
        return 
    }
defer connection.Close()

defer connection.Close() should be after the error check as connection variable can be nil incase dial return with some error.

Jasminjasmina answered 3/3, 2019 at 11:13 Comment(0)
S
1

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)
    wg.Wait()
}

//startTCPConnection
func startTCPConnection() net.Listener {
	tcpListner, tcpConnectonError := net.Listen("tcp", "localhost:3000")
	if tcpConnectonError != nil {
		print(tcpConnectonError)
        // return 
        log.Fatal(tcpConnectonError)
	}
	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()
}

The problem is in server.go. My guess is you ran out of ports by calling wg.Wait() inside incomingTCPListener() func instead of main(). Also tne naked return in the startTCPConnection() function will cause compiler error.

Sundin answered 10/3, 2019 at 20:49 Comment(2)
Thanks a lot, I was able to figure out some of the issues in my code. I will check it and let you know. Thank you so much for your effort and helpVarmint
Still crashes (received data count 16375) panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x10d7534] goroutine 1 [running]: main.sendData() /Users/apple/Desktop/umbrellaServer/src/clientTest.go:24 +0xb4 main.main() /Users/apple/Desktop/umbrellaServer/src/clientTest.go:16 +0x2a exit status 2 Albins-MBP:src apple$Varmint

© 2022 - 2025 — McMap. All rights reserved.