草庐IT

socket_handle

全部标签

sockets - golang tcp 套接字在写入后不立即发送消息

我的GO版本是1.1.1服务器在连接关闭后收到消息,但设置了NoDelay。有什么问题吗addr,_:=net.ResolveTCPAddr("tcp","localhost:5432")conn,err:=net.DialTCP("tcp",nil,addr)deferconn.Close()iferr!=nil{fmt.Println("connectfail")return}err=conn.SetNoDelay(true)iferr!=nil{fmt.Println(err.Error())}for{varmessagestring_,err:=fmt.Scanln(&mess

sockets - 在 go 中不阅读而窥视 Conn

我有一个服务器net.Conn,我想在读出字节之前查看它,以检查它是客户端尝试使用的纯文本协议(protocol),还是SSL/TLS。正在检查http://golang.org/pkg/net/,Conn接口(interface)似乎没有这样的东西。我知道我可以使用iobuf.Reader,但如果客户端是使用SSL/TLS,bufio.Reader将从原始的Conn读取,因此tls.Conn中的握手将失败。那么有什么方法可以查看Go中的Conn(类似于C/C++套接字中的MSG_PEEK)?或者,在我从底层Conn中读出前几个字节后创建一个tls.Conn?

error-handling - Go 中的自定义错误处理

我一直在阅读thisblogpost但我仍然不相信我确切地知道该怎么做才能获得我可以从我的函数返回并在它们之外处理的自定义错误。这是我目前正在做的:funcdoSomething()int{x:=0//Dosomethingwithx....ifsomethingBadHappened{return-1}ifsomethingElseBadHappened{return-2}returnx}这就是我想做的:funcdoSomething()int,?{...ifsomethingBadHappened{return?,err}ifsomethingElseBadHappened{ret

sockets - 从 go 中的套接字读取时遇到问题

我正在尝试学习go语言,并且正在编写一个简单的echo服务器。不过,我很难让它发挥作用。funclisten(serverstring){varbuf[]bytelistener,ok:=net.Listen("tcp",server)ifok!=nil{fmt.Fprintf(os.Stderr,"Couldnotlistenonsocket:%s\n",ok.String())return}conn,ok:=listener.Accept()ifok!=nil{fmt.Fprintf(os.Stderr,"Couldnotacceptconnectiononsocket:%s\n"

http.Handle(Handler 或 HandlerFunc)

下面的功能是如何实现的?funchandle(patternstring,handlerinterface{}){//...whatgoeshere?...http.Handle(pattern,?)}handle("/foo",func(whttp.ResponseWriter,rhttp.Request){io.WriteString(w,"foo")}handle("/bar",BarHandler{})handle()被传递给匹配http.HandlerFunc类型的函数或实现http.Handler接口(interface)的类型。 最佳答案

go - 使用 Go 的 socket.io 客户端

从一个有效的socket.io示例开始(后端:Python/Flask,前端:socket.io.jsv2.0.3),我现在尝试设置一个客户端走却连握手阶段都过不了。很抱歉发了这么长的帖子……(最后我还添加了一个Python客户端,它可以完成我想在Go中实现的功能)(以下工作):后端:@socketio.on('connect',namespace='/endpoint')defconnect():print("Clientconnectedwithrequestsid"+request.sid)@socketio.on('join',namespace='/endpoint')def

戈朗 :how do I handle index out of range error?

我正在用Go编写CLI接口(interface)程序。我的程序要求用户输入文件名作为参数。以下是我编写的处理代码用户不输入任何参数的情况。但它出现panic并给出错误“索引超出范围”。我该如何处理?packagemainimport("encoding/hex""fmt""io/ioutil""log""os")funcmain(){iflen(os.Args)==0{fmt.Println("usage:gohex")os.Exit(1)}else{filename:=os.Args[1]data,err:=ioutil.ReadFile(filename)iferr!=nil{lo

sockets - 如何使用 Golang 编写原始 TCP 数据包(使用 gopacket)并通过原始套接字发送

我想使用gopacket制作客户TCP数据包,然后使用原始套接字发送它们。这是一个简短易读的示例go程序,它演示我想做什么:packagemainimport("code.google.com/p/gopacket""code.google.com/p/gopacket/examples/util""code.google.com/p/gopacket/layers""log""net")funcmain(){deferutil.Run()()//XXXcreatetcp/ippacketsrcIP:=net.ParseIP("127.0.0.1")dstIP:=net.ParseIP

google-app-engine - Appengine with Go : Is there a http. Handle prehook 或类似的东西?

假设我有以下init函数路由请求。funcinit(){http.HandleFunc("/user",handler1)http.HandleFunc("/user/profile",handler2)http.HandleFunc("/user/post",handler3)........}所有这些都要求我有用户的个人资料。我知道我可以funchandler1(whttp.ResponseWriter,r*http.Request){getUserdata()//Actualhandlercode......}但是,有没有一种方法可以在不将函数调用放入每个处理程序的情况下获取数据

windows - 从 Windows 上的 Go *net.UDPConn 获取 syscall.Handle?

如何在Windows上获取*net.UDPConn的底层syscall.Handle?我希望此句柄通过syscall.SetsockoptInt设置IP_MULTICAST_TTL。在Linux上,我执行以下操作:funcsetTTL(conn*net.UDPConn,ttlint)error{f,err:=conn.File()iferr!=nil{returnerr}deferf.Close()fd:=int(f.Fd())returnsyscall.SetsockoptInt(fd,syscall.SOL_IP,syscall.IP_MULTICAST_TTL,ttl)}但是在W