dongritan5654 2011-05-28 13:43
浏览 43
已采纳

通过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 can handle multiple clients at once?

  • 写回答

1条回答 默认 最新

  • duan0531 2011-05-28 13:55
    关注

    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) {
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改
  • ¥50 vue router 动态路由问题