我正在尝试在 flutter 上将数据发送到 python。但我只发送“数据”,我想发送不同的“字符串”,但此代码不起作用。我想发送“psk”和“ssid”字符串。你能帮我吗?
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)
我尝试在 flutter tcp socket-->Python 上发送数据,但我只发送“数据”。我想发送不同的字符串(例如:“ssid”和“psk”
我将以下部分添加到 flutter 代码中以发送不同的字符串。它起作用了,我能够发送一个新字符串。这是新代码。我刚刚改变了这些。
void messageReceivedd(String psk) {
setState(() {
wifi = psk;
});}
ElevatedButton(
onPressed: () {
socketConnection.isConnected();
socketConnection.sendMessage(wifi);
socketConnection.disconnect();
Future.delayed(const Duration(milliseconds: 2000));
socketConnection.connect(5000, messageReceivedd);