Ric*_*eap 12
这是连接到服务器上的 TCP 套接字的最简单的 Dart 程序。它发送 'hello',等待任何回复 5 秒,然后关闭套接字。您可以将它与您自己的服务器或像这样的简单回显服务器一起使用。
import 'dart:io';
import 'dart:convert';
import 'dart:async';
main() async {
Socket socket = await Socket.connect('192.168.1.99', 1024);
print('connected');
// listen to the received data event stream
socket.listen((List<int> event) {
print(utf8.decode(event));
});
// send hello
socket.add(utf8.encode('hello'));
// wait 5 seconds
await Future.delayed(Duration(seconds: 5));
// .. and close the socket
socket.close();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13164 次 |
| 最近记录: |