Unix 套接字:何时使用 bind() 函数? [英] Unix sockets: when to use bind() function?

查看:24
本文介绍了Unix 套接字:何时使用 bind() 函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道什么时候必须使用 bind() 函数.我想无论我使用 TCP 还是 UDP,只要我需要接收数据(即 recv() 或 recvfrom() 函数)都应该使用它,但有人告诉我情况并非如此.

I've not a clear idea about when I have to use the bind() function. I guess it should be used whenever I need to receive data (i.e. recv() or recvfrom() functions) whether I'm using TCP or UDP, but somebody told me this is not the case.

谁能解释一下?

编辑我已经阅读了答案,但实际上我不是很清楚.让我们举一个例子,我有一个 UDP 客户端,它将数据发送到服务器,然后必须得到响应.我必须在这里使用绑定,对吗?

EDIT I've read the answers but actually I'm not so clear. Let's take an example where I have an UDP client which sends the data to the server and then has to get a response. I have to use bind here, right?

推荐答案

这个答案有点啰嗦,但我认为它会有所帮助.

This answer is a little bit long-winded, but I think it will help.

当我们进行计算机网络时,我们实际上只是在进行进程间通信.假设在您自己的计算机上,您有两个想要相互通信的程序.您可以使用管道将数据从一个程序发送到另一个程序.当你说 ls |grep pdf 您正在获取 ls 的输出并将其输入 grep.通过这种方式,您可以在两个独立的程序 lsgrep 之间进行单向通信.

When we do computer networking, we're really just doing inter-process communication. Lets say on your own computer you had two programs that wanted to talk to each other. You might use pipe to send the data from one program to another. When you say ls | grep pdf you are taking the output of ls and feeding it into grep. In this way, you have unidirectional communication between the two separate programs ls and grep.

执行此操作时,需要有人跟踪每个进程的进程 ID (PID).该 PID 是每个进程的唯一标识符,它帮助我们跟踪我们要传输的数据的源"和目标"进程.

When you do this, someone needs to keep track of the Process ID (PID) of each process. That PID is a unique identifier for each process and it helps us track who the "source" and "destination" processes are for the data we want to transfer.

现在假设您有来自网络服务器的数据,而不是您想传输到浏览器的数据.嗯,这和上面的场景是一样的——两个程序之间的进程间通信,服务器"和浏览器".

So now lets say you have data from a webserver than you want to transfer to a browser. Well, this is the same scenario as above - interprocess communication between two programs, the "server" and "browser".

除了这次这两个程序在不同的计算机上.两台计算机之间的进程间通信机制称为套接字".

Except this time those two programs are on different computers. The mechanism for interprocess communication across two computers are called "sockets".

太好了.您获取一些数据,通过网络传输它,然后另一台计算机接收它.除了那台计算机不知道如何处理这些数据.还记得我们说过我们需要一个 PID 来知道哪些进程正在通信吗?在网络中也是如此.当您的计算机收到 HTML 数据时,它如何知道将其发送到firefox"而不是pidgin"?

So great. You take some data, lob it over the wire, and the other computer receives it. Except that computer doesn't know what to do with that data. Remember we said we need a PID to know which processes are communicating? The same is true in networking. When your computer receives HTML data, how does it know to send it to "firefox" rather than "pidgin"?

好吧,当您传输网络数据时,您指定它在特定的端口"上运行.80端口通常用于web,25端口用于telnet,443端口用于HTTPS等

Well when you transmit network data, you specify that it goes on a specific "port". Port 80 is usually used for web, port 25 for telnet, port 443 for HTTPS, etc.

那个端口"绑定到机器上的特定进程 ID.这就是为什么我们有端口.这就是我们使用 bind() 的原因.为了告诉发送方哪个进程应该接收我们的数据.

And that "port" is bound to a specific process ID on the machine. This is why we have ports. This is why we use bind(). In order to tell the sender which process should receive our data.

这应该可以解释人们发布的答案.如果你是发送者,你并不关心传出端口是什么,所以你通常不使用 bind() 来指定那个端口.如果您是接收者,那么其他人都必须知道去哪里寻找您.所以你bind()你的程序到80端口,然后告诉大家确保在那里传输数据.

This should explain the answers people have posted. If you are a sender, you don't care what the outgoing port is, so you usually don't use bind() to specify that port. If you are a receiver, well, everyone else has to know where to look for you. So you bind() your program to port 80 and then tell everyone to make sure to transmit data there.

要回答您的硬件问题,是的,您可能希望对服务器使用 bind().但是客户端不需要使用 bind() - 他们只需要确保它们将数据传输到您选择的任何端口.

To answer your hw question, yes, your probably want to use bind() for your server. But the clients don't need to use bind() - they just need to make sure they transmit data to whatever port you've chosen.

这篇关于Unix 套接字:何时使用 bind() 函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆