如何将Flutter应用程序连接到TCP套接字服务器? [英] How to connect Flutter app to tcp socket server?

查看:1299
本文介绍了如何将Flutter应用程序连接到TCP套接字服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难将Flutter应用程序连接到服务器上的网络TCP套接字。我知道我必须使用某种中间选项,以便将tcp套接字转换为flutter,将Flutter转换为tcp套接字。

I had great difficulties to connect Flutter app to my network tcp socket on server. I know I have to use some sort intermediate option so translate data between tcp socket to flutter and Flutter to tcp socket.

任何想法,信息如何实现。问题是如何将Flutter应用程序连接到tcp套接字服务器?

Any idea, info how do achieve this. And question is How to connect Flutter app to tcp socket server?

推荐答案

这是连接到TCP的最简单的Dart程序服务器上的套接字。它会发送 hello,等待5秒钟才能收到答复,然后关闭套接字。您可以将其与您自己的服务器或简单的回显服务器(如此服务器)一起使用。 / p>

Here's pretty much the simplest Dart program to connect to a TCP socket on a server. It sends 'hello', waits 5 seconds for any reply, then closes the socket. You could use this with your own server, or a simple echo server like 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();
}

这篇关于如何将Flutter应用程序连接到TCP套接字服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆