开发者

Handling multiple Clients over TCP

Okay, I just started learning golang and I like it so far. However I don't find their documentation good for go starters, Here is my problem. I wanted to write little server program that accepts connections and writes something to the client. No problem doing that so far.

However as soon as the thing get a real functionality, I need to be able to handle multiple clients, which I though would also be a good exercise for goroutines.

listener, error := net.Listen("tcp", remote)
con, error := listener.Accept()
go handleClient(&con);
func handleClient(con *net.Conn) {

I've cut most of the code out. The problem is, no matter what I try, I can't pass con.

con.RemoteAddr undefined (type *net.Conn has no field or method RemoteAddr)

(found that in this example: http://raycompstuff.blogspot.com/2009/12/simpler-chat-server-and-client-in.html). So i tried looking at the documentation, but it just gave me the source of the net package. Read trough the source, and figured it should be

undefined: TCPConn

How can I pass the connection of a client to a goroutine, so i 开发者_JS百科can handle multiple clients at once?


Ok, figured it out. There is some guy who actually already wrote what I wanted to write in go. https://github.com/dustin/gomemcached/blob/master/mc_conn_handler.go

go handleClient(con);
func handleClient(con net.Conn) {
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜