Go语言中的网络编程

huangapple go评论83阅读模式
英文:

Network programming in Go

问题

我正在学习用于网络编程的Go语言。问题是Go的文档太简单了。例如,我不知道何时使用net.DialTCP,何时使用TCPListener对象的AcceptTCP方法,它们有什么区别?客户端如何与另一个客户端进行通信?而不是客户端与服务器之间的通信。

英文:

I'm studying Go for network programming. The problem is Go documentation is too simple. For example, I don't know when to use net.DialTCP, and when to use TCPListener object to AcceptTCP, what's the difference? How about client communicate with another client? Not client to server.

答案1

得分: 15

连接

在Go语言中,你可以使用net包中的Dial函数来连接到远程机器。

net.Dial("tcp", "google.com:80")
net.Dial("udp", "tracker.thepiratebay.org:6969")
net.Dial("ip", "kremvax.su")
net.Dial("unix", "/dev/log")

这将返回一个表示刚刚建立的连接的抽象Conn对象。Conn实现了io包中的ReadWriteCloser接口以及其他一些函数。你可以使用这个对象来发送和接收数据。

监听

要监听,也就是打开一个端口,你可以使用net包中的Listen函数。调用Listen函数会返回一个Listener对象。使用Accept函数来接受传入的连接。Accept函数返回另一个可以像上面那样使用的Conn对象。

ls, err := net.Listen("tcp", ":1337")
if err != nil {
    // 端口可能被阻塞,插入错误处理代码
}

conn, err := ls.Accept()
if err != nil {
    // 错误处理
}

conn.Write("Hello, world!")

DialTCP和ListenTCP

这些函数可以让你更好地控制TCP连接。我建议只在你的程序确实需要时使用它们,因为DialListen更简单、更通用,并且可以轻松地使你的程序适应其他类型的网络连接。

英文:

Connecting

In Go, you use the <code>Dial</code> function from net to connect to a remote machine.

net.Dial(&quot;tcp&quot;,&quot;google.com:80&quot;)
net.Dial(&quot;udp&quot;,&quot;tracker.thepiratebay.org:6969&quot;)
net.Dial(&quot;ip&quot;,&quot;kremvax.su&quot;)
net.Dial(&quot;unix&quot;,&quot;/dev/log&quot;)

This gives you an abstract Conn object that represents the connection you just established. Conn implements the <code>ReadWriteCloser</code> interface from io and a couple of other functions. You can use this object to send and receive data.

Listening

To listen, i.e. open a port, you use the <code>Listen</code> function from net. Calling Listen gives you a Listener object. Use Accept to accept incoming connections. Accept returns another Conn object that can be used as above.

ls, err := net.Listen(&quot;tcp&quot;,&quot;:1337&quot;)
if err != nil {
    // port probably blocked, insert error handling here
}

conn, err := ls.Accept()
if err != nil {
    // error handling
}

conn.Write(&quot;Hello, world!&quot;)

DialTCP and ListenTCP

These functions give you more control over TCP connections. I suggest you to only use them if they are definitly needed for your program as Dial and Listen are simpler, more generic and easily allow you to adapt your program to other types of network connections.

答案2

得分: 2

net.DialTCP 用于客户端创建与远程服务器的连接。

net.TCPListener.AcceptTCP 用于服务器端接受新的连接(可能由 net.DialTCP 发起,如果客户端是用 Go 编写的)。请注意,监听器可以接受多个连接,逐个地提供服务给多个客户端(例如,每个客户端在不同的 goroutine 中)。

根据你是编写客户端还是服务器端,你可以使用 net.DialTCPnet.TCPListener

也许你应该先了解一般的网络编程?然后这些内容会更有意义,我想。

英文:

net.DialTCP is used on the client side to create a connection to remote server.

net.TCPListener.AcceptTCP is used on the server side to accept new connection (possibly initiated by net.DialTCP if client is written in Go). Note that listener may accept multiple connections, one by one, thus serving multiple clients at once (e.g. each in different goroutine).

Depending on whether you are writing client or server, you use net.DialTCP or net.TCPListener

Maybe you should learn about network programming in general first? Then these would make more sense I think.

huangapple
  • 本文由 发表于 2013年12月28日 20:11:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/20814381.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定