我很难将Flutter应用程序连接到服务器上的网络tcp套接字。我知道我必须使用某种中间选项,以便在tcp套接字到flutter和Flutter到tcp套接字之间转换数据。
任何想法,信息如何实现这一点。问题是如何将Flutter应用程序连接到tcp套接字服务器?
发布于 2019-02-02 03:34:55
下面是连接到服务器上的TCP套接字的最简单的Dart程序。它发送'hello',等待5秒等待任何回复,然后关闭套接字。您可以在自己的服务器上使用它,也可以在this one这样的简单回显服务器上使用。
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();
}https://stackoverflow.com/questions/54481818
复制相似问题