I'm trying send data to python on flutter. But I am only send "data", ı want send different "string" but this code not working. I want to send "psk" and "ssid" string. Can you help me?
import 'package:flutter/material.dart';
import 'package:tcp_socket_connection/tcp_socket_connection.dart';
class UsersPage extends StatefulWidget {
@override
_UsersPageState createState() => _UsersPageState();
}
class _UsersPageState extends State<UsersPage> {
TextEditingController nameController = TextEditingController();
TcpSocketConnection socketConnection =
TcpSocketConnection("my_ip", 9000);
String message = "";
String ssid = "";
String psk = "";
@override
void initState() {
super.initState();
startConnection();
}
//receiving and sending back a custom message
void messageReceived(String msg) {
setState(() {
message = msg;
});
}
//starting the connection and listening to the socket asynchronously
void startConnection() async {
socketConnection.enableConsolePrint(
true); //use this to see in the console what's happening
if (await socketConnection.canConnect(20000000, attempts: 1)) {
//check if it's possible to connect to the endpoint
await socketConnection.connect(2000000, messageReceived, attempts: 1);
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
TextField(
decoration: InputDecoration(
hintText: 'Enter Wifi Name',
border: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(20.0)),
),
enabledBorder: OutlineInputBorder(
borderSide:
BorderSide(color: Colors.blueAccent, width: 1.0),
borderRadius: BorderRadius.all(Radius.circular(20.0)),
),
contentPadding:
EdgeInsets.symmetric(vertical: 10.0, horizontal: 20.0),
prefixIcon: Icon(Icons.wifi),
),
onChanged: (text) {
setState(() {
ssid = text;
});
},
),
SizedBox(
height: 30,
),
TextField(
decoration: InputDecoration(
hintText: 'Enter Wifi Password',
border: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(20.0)),
),
enabledBorder: OutlineInputBorder(
borderSide:
BorderSide(color: Colors.blueAccent, width: 1.0),
borderRadius: BorderRadius.all(Radius.circular(20.0)),
),
contentPadding:
EdgeInsets.symmetric(vertical: 10.0, horizontal: 20.0),
prefixIcon: Icon(Icons.lock),
),
onChanged: (text) {
setState(() {
psk = text;
});
},
),
SizedBox(
height: 30,
),
ElevatedButton(
onPressed: () {
socketConnection.isConnected();
socketConnection.sendMessage(ssid);
socketConnection.disconnect();
Future.delayed(const Duration(milliseconds: 2000));
socketConnection.connect(5000, messageReceived);
},
child: Text('SSİD'),
),
SizedBox(
height: 30,
),
ElevatedButton(
onPressed: () {
socketConnection.isConnected();
socketConnection.sendMessage(psk);
socketConnection.disconnect();
Future.delayed(const Duration(milliseconds: 2000));
socketConnection.connect(5000, messageReceived);
},
child: Text('PSK'),
),
])));
}
}
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("0.0.0.0", 9000))
s.listen(5)
while True:
# now our endpoint knows about the OTHER endpoint.
clientsocket, address = s.accept()
print(f"Connection from {address} has been established.")
data = clientsocket.recv(1024)
psk = str(clientsocket.recv(1024))
ssid = str(clientsocket.recv(1024).decode())
print(ssid)
print(psk)
print(data)
I try send data on flutter tcp socket-->Python but I only send "data". I want to send different string (example: "ssid" and "psk"
I added the following parts to the flutter code to send a different string. And it worked I was able to send a new string. Here is the new code. I just changed these.