英文:
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连接。我建议只在你的程序确实需要时使用它们,因为Dial
和Listen
更简单、更通用,并且可以轻松地使你的程序适应其他类型的网络连接。
英文:
Connecting
In Go, you use the <code>Dial</code> function from net
to connect to a remote machine.
net.Dial("tcp","google.com:80")
net.Dial("udp","tracker.thepiratebay.org:6969")
net.Dial("ip","kremvax.su")
net.Dial("unix","/dev/log")
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("tcp",":1337")
if err != nil {
// port probably blocked, insert error handling here
}
conn, err := ls.Accept()
if err != nil {
// error handling
}
conn.Write("Hello, world!")
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.DialTCP
或 net.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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论