Udp 套接字处理

发布于 2025-01-02 14:33:12 字数 570 浏览 0 评论 0原文

我想使用 UDP 套接字来简单地发送音频流。我的问题是我无法让它工作,我认为它会比使用 TCP IP 更简单。

我做了什么,我在表单上放置了一个 UDPSocket 组件,对于服务器部分,我使用了这段代码

  with udpServer do
  begin
    LocalHost := '127.0.0.1';
    LocalPort := '5002';
    Open();
    Active := True;
  end;

对于客户端应用程序:

   with udpClient do
      begin
        RemoteHost := '192.0.168.100'; //my local address
        RemotePort := '5002';
        Open();
        Active := True;
      end;

问题是我没有收到任何东西。我做错了什么,我没有任何可以阻止连接的第三方软件。

我没有找到任何合适的例子来使用这个组件,任何灵感来源将不胜感激。

I want to use UDP Socket to simply send an audio stream. My problem is that i can't get it working i thought it would be simpler then using TCP IP.

What i did i droped a UDPSocket component on my form and for the server part i used this code

  with udpServer do
  begin
    LocalHost := '127.0.0.1';
    LocalPort := '5002';
    Open();
    Active := True;
  end;

For the client application this :

   with udpClient do
      begin
        RemoteHost := '192.0.168.100'; //my local address
        RemotePort := '5002';
        Open();
        Active := True;
      end;

The problem is i am not receving anything. What i am doing wrong i don't have any third-party software that can block the connection.

I didn't find any suitable example for using this component any source of inspiration will be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七月上 2025-01-09 14:33:12

您让服务器和客户端连接在同一 IP 上。

通常,如果将服务器应用程序 IP 地址设置为 0.0.0.0,它将绑定到给定端口上的任何可用 IP 地址,包括 127.0.0.1。

然后客户端必须连接到绑定的 IP 之一。相反,您让服务器侦听 127.0.0.1,而客户端连接到 192.0.168.100。

不要被“LocalHost”属性名称所迷惑。这里的“本地”只是意味着你必须设置一个“本地”IP,一个分配给本地机器的IP,而不是“远程”(另一台机器的)IP,而客户端当然会连接到“远程”IP,服务器的那个。

当且仅当您希望服务器仅对本地应用程序可用时,127.0.0.1 是一个不错的选择,因为该 IP 范围仅限于同一台计算机。如果你想让它在机器外部可用,你必须将其绑定到有效的IP。

无论您遇到什么问题,WiresharkMicrosoft 网络监视器 对于了解正在发生的情况非常有用。

You have the server and the client connect on the same IP.

Usually if you set the server application IP address to 0.0.0.0 it will bind to any available IP address on the given port, including 127.0.0.1.

Then the client must connect to one of the bound IPs. Instead, you had the server listening on 127.0.0.1 and the client connect to 192.0.168.100.

Don't be fooled by the "LocalHost" property name. "Local" here just mean you have to set a "local" IP, an IP assigned to the local machine, not a "remote" (of another machine) one, while the client of course will connect to a "remote" IP, that of the server.

127.0.0.1 is a good choice if and only if you want your server to be available only to local application, because that IP scope is limited to the same machine. If you want to make it available outside the machine, you have to bind it to a valid IP.

Whatever issue you have, tools like Wireshark or Microsoft Network Monitor are very useful to understand what's going on.

み零 2025-01-09 14:33:12

您将服务器绑定到 127.0.0.1,因此它只接受专门连接到 127.0.0.1 的客户端。您的客户端正在连接到 192.0.168.100(也许您的意思是 192.168.0.100?)。

您需要将服务器绑定到客户端实际连接的 IP,或者将其绑定到 0.0.0.0 以接受任何本地 IP 上的连接。

尽管 UDP 是无连接的,但同样的规则也适用于 UDP 和 TCP,因为它适用于它们共享的较低级别的 IP 路由层。

You are binding the server to 127.0.0.1, so it will only accept clients that connect to 127.0.0.1 specifically. Your client is connecting to 192.0.168.100 (perhaps you meant 192.168.0.100?) instead.

You need to bind the server to the IP(s) that clients are actually connecting to, or else bind it to 0.0.0.0 to accept connections on any local IP.

Even though UDP is connectionless, this same rule applies to both UDP and TCP, as it applies to the lower level IP routing layer that they both share.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文