• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Dart UDP 客户端/服务器

用户头像
it1352
帮助1

问题说明

我一直在尝试使用 RawDatagramSocket 来实现 udp 客户端,但我有点卡住了.我既不能发送也不能接收任何数据.据我所知,这是 Dart 中的一个非常新的功能,除了 tcp 之外我找不到任何示例.

I've been trying to implement a udp client by using the RawDatagramSocket but I'm kind of stuck. I can neither send or receive any data. It's a pretty new feature in Dart as far as I know and I can't find any examples except for tcp.

另外,我不知道是否有错误或什么,但似乎我只能绑定到本地主机.尝试绑定到另一台计算机 IPV4 地址时,我收到一个套接字异常(由于某些无效的 IP 地址而无法创建数据报套接字).我已经尝试过 tcp 套接字,将数据连接并发送到用 c# 实现的 tcp 服务器(而 dart 代码在 Mac OS 上运行),没有问题.

Also, I don't know if there is a bug or anything, but it seems like I can only bind to the localhost. When trying to bind to another computer IPV4 address, I receive a socket exception (failure to create datagram socket due to some invalid IP address). I've tried the tcp socket, connecting and sending data to a tcp server implemented in c# (while the dart code was running on Mac OS), without a problem.

有谁做过这方面的工作并且可以提供一个很好的例子吗?

Anyone who has worked on it and can provide a nice example?

我的代码:

import 'dart:io';
import 'dart:convert';

void main() {
  var data = "Hello, World";
  var codec = new Utf8Codec();
  List<int> dataToSend = codec.encode(data);
  //var address = new InternetAddress('172.16.32.73');
  var address = new InternetAddress('127.0.0.1');
  RawDatagramSocket.bind(address, 16123).then((udpSocket) {
    udpSocket.listen((e) {
      print(e.toString());
      Datagram dg = udpSocket.receive();
      if(dg != null) 
        dg.data.forEach((x) => print(x));

    });
    udpSocket.send(dataToSend, new InternetAddress('172.16.32.73'), 16123);
    print('Did send data on the stream..');
  });
}

编辑

忙了几天,但是在更彻底地阅读了 API 规范之后,在下面评论的帮助下,我了解到,由于它是一次性侦听器,因此每次发送都必须将 writeEventsEnabled 设置为 true.考虑到 Günter、Fox32 和 Tomas 的评论,其余更改非常简单.

Been busy for a couple of days but after reading the API spec more thoroughly, and with some help from the comments below, I learned that, since it's a one-shot listener, the writeEventsEnabled must be set to true for every send. The rest of the changes are pretty straightforward given the comments by Günter, Fox32 and Tomas.

我尚未测试将其设置为服务器,但我认为这只是绑定到首选端口的问题(而不是下面示例中的 0).服务器在 Windows 8.1 上用 C# 实现,而 Dart VM 在 Mac OS X 上运行.

I haven't tested to set it up as a server yet but I assume that's just a matter of binding to the preferred port (instead of 0 as in the example below). The server was implemented in C# on a Windows 8.1, while the Dart VM was running on Mac OS X.

import 'dart:async';
import 'dart:io';
import 'dart:convert';

void connect(InternetAddress clientAddress, int port) {
  Future.wait([RawDatagramSocket.bind(InternetAddress.ANY_IP_V4, 0)]).then((values) {
    RawDatagramSocket udpSocket = values[0];
    udpSocket.listen((RawSocketEvent e) {
      print(e);
      switch(e) {
        case RawSocketEvent.READ :
          Datagram dg = udpSocket.receive();
          if(dg != null) {
            dg.data.forEach((x) => print(x));
          }
          udpSocket.writeEventsEnabled = true;
          break;
        case RawSocketEvent.WRITE : 
          udpSocket.send(new Utf8Codec().encode('Hello from client'), clientAddress, port);
          break;
        case RawSocketEvent.CLOSED : 
          print('Client disconnected.');
      }
    });
  });
}

void main() {
  print("Connecting to server..");
  var address = new InternetAddress('172.16.32.71');
  int port = 16123;
  connect(address, port);
}

正确答案

#1

我不知道这是否是正确的方法,但它对我有用.

I don't know if this is the right way to do it, but it got it working for me.

import 'dart:io';
import 'dart:convert';

void main() {
  var data = "Hello, World";
  var codec = new Utf8Codec();
  List<int> dataToSend = codec.encode(data);
  var addressesIListenFrom = InternetAddress.anyIPv4;
  int portIListenOn = 16123; //0 is random
  RawDatagramSocket.bind(addressesIListenFrom, portIListenOn)
    .then((RawDatagramSocket udpSocket) {
    udpSocket.forEach((RawSocketEvent event) {
      if(event == RawSocketEvent.read) {
        Datagram dg = udpSocket.receive();
        dg.data.forEach((x) => print(x));
      }
    });
    udpSocket.send(dataToSend, addressesIListenFrom, portIListenOn);
    print('Did send data on the stream..');
  });
}

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcfhgfc
系列文章
更多 icon
同类精品
更多 icon
继续加载