tcp_socket_connection 0.0.9 tcp_socket_connection: ^0.0.9 copied to clipboard
A flutter package to connect, listen to and send messages over tcp sockets.
import 'package:flutter/material.dart';
import 'package:tcp_socket_connection/tcp_socket_connection.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
//Instantiating the class with the Ip and the PortNumber
TcpSocketConnection socketConnection=TcpSocketConnection("192.168.1.113", 10251);
String message="";
@override
void initState() {
super.initState();
startConnection();
}
//receiving and sending back a custom message
void messageReceived(String msg){
setState(() {
message=msg;
});
socketConnection.sendMessage("MessageIsReceived :D");
}
//starting the connection and listening to the socket asynchronously
void startConnection() async{
await socketConnection.connect(5000, "EOS", messageReceived);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Text(
'You have received '+ message,
),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}