如何在颤动中连接到 TCP 套接字(不是 Web 套接字)?
Posted
技术标签:
【中文标题】如何在颤动中连接到 TCP 套接字(不是 Web 套接字)?【英文标题】:How can i connect to TCP socket (not web socket) in flutter? 【发布时间】:2021-11-26 13:38:44 【问题描述】:import 'dart:typed_data';
import 'package:flutter/foundation.dart';
import 'dart:io';
import 'package:flutter/material.dart';
void main() async
Socket sock = await Socket.connect('192.168.1.150', 2662);
print('Connected to: $sock.remoteAddress.address:$sock.remotePort');
runApp(MyApp(sock));
sock.listen(
(Uint8List data)
final serverResponse = String.fromCharCodes(data);
print('Server: $serverResponse');
,
onError: (error)
print(error);
sock.destroy();
,
onDone: ()
print('Server left.');
sock.destroy();
,
);
class MyApp extends StatelessWidget
Socket socket;
MyApp(Socket s)
this.socket = s;
s.listen(
(Uint8List data)
final serverResponse = String.fromCharCodes(data);
print('Server: $serverResponse');
,
// handle errors
onError: (error)
print(error);
s.destroy();
,
onDone: ()
print('Server left.');
s.destroy();
,
);
@override
Widget build(BuildContext context)
final title = 'Example';
return MaterialApp(
title: title,
home: MyHomePage(
title: title,
channel: socket,
),
);
class MyHomePage extends StatefulWidget
final String title;
final Socket channel;
MyHomePage(Key key, @required this.title, @required this.channel)
: super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
class _MyHomePageState extends State<MyHomePage>
TextEditingController _controller = TextEditingController();
@override
Widget build(BuildContext context)
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Form(
child: TextFormField(
controller: _controller,
decoration: InputDecoration(labelText: 'Send a message'),
),
),
StreamBuilder(
stream: widget.channel,
builder: (context, snapshot)
return Padding(
padding: const EdgeInsets.symmetric(vertical: 24.0),
child: Text(snapshot.hasData
? '$String.fromCharCodes(snapshot.data)'
: ''),
);
,
)
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _sendMessage,
tooltip: 'Send message',
child: Icon(Icons.send),
), // This trailing comma makes auto-formatting nicer for build methods.
);
void _sendMessage()
if (_controller.text.isNotEmpty)
widget.channel.write(_controller.text);
@override
void dispose()
widget.channel.close();
super.dispose();
当我在模拟器上运行代码时,我得到这个错误: " 调试服务监听 ws://127.0.0.1:59871/2EzZsU03nwc=/ws 正在将文件同步到 IA 模拟器上的设备 AOSP..."
与
“E/flutter (24126): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] 未处理异常: SocketException: OS Error: Connection timed out, errno = 110, address = 192.168.1.131, port = 51170"
我该如何解决这个问题?
谢谢。
【问题讨论】:
【参考方案1】:这是连接到服务器上 TCP 套接字的最简单的 Dart 程序。它发送“你好”,等待 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();
【讨论】:
嗨,我按照你说的做了,但我又遇到了同样的错误。 E/flutter (25374): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] 未处理异常:SocketException: OS 错误:连接超时,errno = 110,地址 = 192.168.1.150,端口 = 58384跨度> 你可以尝试清理并再次运行吗? 我打开了终端。我做了flutter -clear,然后flutter-run它构建了它,但不幸的是它给出了同样的错误。 嗨,G H Prakash,如何在使用 Flutter TCP 套接字按下按钮时发送消息?谢谢... 我在 python 中有一个接收器,绑定socket.bind("tcp://127.0.0.1:5556")
我使用 socket = await Socket.connect('127.0.0.1', 5556);
与颤振连接,但是当我使用 socket.add(utf8.encode('hello'));
发送消息时,其他程序没有收到任何内容以上是关于如何在颤动中连接到 TCP 套接字(不是 Web 套接字)?的主要内容,如果未能解决你的问题,请参考以下文章
在 Java (Android) 中连接到 IOCP 服务器
C# PowerShell 无法仅在 Web 应用程序中连接到 AzureAD
设备在Android中连接到WiFi时如何获取蜂窝网络的IP地址