Socket programming, connection reset by peer

3,830 views
Skip to first unread message

Tong Sun

unread,
Jan 16, 2013, 11:39:56 AM1/16/13
to golan...@googlegroups.com
Hi, 

I'm trying with the socket programming in go, following the sample code from https://github.com/astaxie/build-web-application-with-golang/blob/master/08.4.md, but get "connection reset by peer" error: 

$ go run Socket-Client.go localhost:1200
Fatal error: read tcp 127.0.0.1:1200: connection reset by peerexit status 1

$ netstat -a | grep 1200
tcp        0      0 *:1200                  *:*                     LISTEN     

I've isolate the problem to be on the server side, because the client can connect to other normal sites just fine. 

I've paste the server code here, 

Please help to see what's going wrong. 

Thanks

steve wang

unread,
Jan 16, 2013, 12:27:50 PM1/16/13
to golan...@googlegroups.com
I doubt whether the program which you failed to run is the same as the one you posted here.
It's unbelievable that the system told you an error that "connection reset by peer" when you have not yet got any connection established.

Tong Sun

unread,
Jan 16, 2013, 1:50:00 PM1/16/13
to steve wang, golang-nuts
Good point, which turns my attention away from TCP connection/listening code, because I wasn't able to find any problem from it. 

So now I think the real problem is how the server code handle the connection. I now believe that the connection is established without any problem but the rest of the code, the connection handling part, is the problem. 


--
 
 

Tong Sun

unread,
Jan 16, 2013, 2:00:32 PM1/16/13
to steve wang, golang-nuts
Confirmed. If I do not wrap the connection handling part in go routine, it would work fine:

$ go run Socket-Client.go localhost:7777
2013-01-16 13:54:39.779224 -0500 EST


To recap, the only difference to me is whether to have used go routine or not. How to fix that? 

Thanks

Tamás Gulácsi

unread,
Jan 16, 2013, 5:44:33 PM1/16/13
to golan...@googlegroups.com
Premature end of main goroutine?
Who waits for the handler send the data?

Tong Sun

unread,
Jan 16, 2013, 11:58:55 PM1/16/13
to Tamás Gulácsi, golang-nuts

On Wed, Jan 16, 2013 at 5:44 PM, Tamás Gulácsi <tgula...@gmail.com> wrote:
Premature end of main goroutine?
Who waits for the handler send the data?

and the solution would be? 

Sorry, I'm still trying to learn Go programming by examples, and go concurrent + socket programming + bad example will only make my head spin. 

Please help. Thanks

Tong Sun

unread,
Jan 17, 2013, 10:48:31 AM1/17/13
to golang-nuts
Hi again, 

Could anyone help me take a look at this simple code please? 



I'm still trying to learn Go programming by examples, and am trying to make the code to handle multiple client requests by using go routine. 

The code is working fine and is very short, and there would not be much change necessary I believe. Just because all of go, go concurrent and socket programming are new to me, I don't know how to make the change myself. I've include the whole code below: 

package main

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

func main() {
    service := ":7777"
    tcpAddr, err := net.ResolveTCPAddr("tcp4", service)
    checkError(err)
    listener, err := net.ListenTCP("tcp", tcpAddr)
    checkError(err)
    for {
        conn, err := listener.Accept()
        if err != nil {
            continue
        }
        daytime := time.Now().String()
        conn.Write([]byte(daytime)) // don't care about return value
        conn.Close()                // we're finished with this client
    }
}
func checkError(err error) {
    if err != nil {
        fmt.Fprintf(os.Stderr, "Fatal error: %s", err.Error())
        os.Exit(1)
    }
}


Please help. Thanks

Carlos Castillo

unread,
Jan 17, 2013, 2:37:07 PM1/17/13
to golan...@googlegroups.com
My suggestion would be to take the code that is in the loop after the connection has been established and checked for errors, and put it into it's own function that takes the connection as a parameter.

For example:
func handleRequest(c net.Conn ) {
// do stuff
c.Close()
}

Once that's working to your liking, then simply change the call to have the go keyword in front of it. http://play.golang.org/p/DWePWiMrcZ

Although it's possible to use an anonymous function instead of the named function, this way the main() function is smaller (easier to read), and should you want to add the error checking you omitted, it won't make main bigger.

Tong Sun

unread,
Jan 17, 2013, 4:06:09 PM1/17/13
to Carlos Castillo, golang-nuts
On Thu, Jan 17, 2013 at 2:37 PM, Carlos Castillo <cook...@gmail.com> wrote:
My suggestion would be to take the code that is in the loop after the connection has been established and checked for errors, and put it into it's own function that takes the connection as a parameter.

For example:
func handleRequest(c net.Conn ) {
// do stuff
c.Close()
}

Once that's working to your liking, then simply change the call to have the go keyword in front of it. http://play.golang.org/p/DWePWiMrcZ

Thanks for your help Carlos. 

That's exactly what I've been doing (search for the http://play.golang.org/p/YrohMJWuIw at the end of this message). I.e., everything works, until changing the call to have the go keyword in front of it.

As Tamás put, the problem is premature ending of main goroutine, because nobody waits for the handler to send the data.

I don't know if it is the case, and I don't know what is the fix. 

Anyone? 

Thanks

--
 
 

Dave Cheney

unread,
Jan 17, 2013, 7:02:22 PM1/17/13
to Tong Sun, Carlos Castillo, golang-nuts
Line 19 is questionable

The rest works ok

odessa:src dfc$ telnet 127.0.0.1 1200
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
2013-01-18 11:01:41.618236838 +1100 ESTConnection closed by foreign host.
> --
>
>
Reply all
Reply to author
Forward
0 new messages