Flutter: How to send packages as a tcp client

2,312

First create a string variable with the message you want to send.

And in your code update this:

socket.write(HERE_STRING_MESSAGE);
Share:
2,312
DomingoMG
Author by

DomingoMG

Updated on December 15, 2022

Comments

  • DomingoMG
    DomingoMG over 1 year

    I am trying to connect to the HHC-NET2D relay device.

    This device allows connection to a tcp client. I have not found a solution with which I can send parameters to the device. If someone knows the recommended way to establish a connection through tcp client and send parameters it would be helpful.

    Support
    1. Support TCP Server, TCP client, UDP
    2. Supporting 4 client connections in the TCP server. Assume that the local port is 5000. Port 5000 can be connected to the TCP client.
    3. Support TCP MODBUS protocol.

    Control command
    5. Sent "on1" to the HHC-NET2D will turn on the First relay.
    6. Sent "on2" to the HHC-NET2D will turn on the Second relay.
    7. Sent "off1" to the HHC-NET2D will turn on the First relay.
    8. Sent "off2" to the HHC-NET2D will turn on the Second relay.

    Read more here: https://es.aliexpress.com/item/32358735415.html

    import 'dart:io';
    import 'package:flutter/material.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: HomeApp(),
        );
      }
    }
    
    class HomeApp extends StatefulWidget {
      @override
      _HomeAppState createState() => _HomeAppState();
    }
    
    class _HomeAppState extends State<HomeApp> {
      Socket socket;
      String name;
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text(name == null ? 'NO CONNECTED' : name),
          ),
          body: Container(
            child: Column(
              children: <Widget>[
                RaisedButton(
                  child: Text('Connect'),
                  onPressed: (){
                    connect();
                  },
                ),
                RaisedButton(
                  child: Text('Send Data'),
                  onPressed: (){
                    sendData();
                  },
                )
              ],
            ),
          ),
        );
      }
    
      // Socket connection
      void connect() {
        Socket.connect("ipHere", portHere).then((Socket sock) {
          socket = sock;
          socket.listen(
              dataHandler,
              onError: errorHandler,
              onDone: doneHandler,
              cancelOnError: false
          );
        });
      }
      void dataHandler(data){
        setState(() {
          name = new String.fromCharCodes(data).trim();
        });
      }
      void errorHandler(error, StackTrace trace){
        print(error);
      }
      void doneHandler(){
        socket.destroy();
      }
      void sendData(){
        //socket.write(...)
      }
    }