1_TCP
1.1_TCP服务端
import 'dart:convert';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: MyForm(),
);
}
}
class MyForm extends StatefulWidget {
const MyForm({Key? key}) : super(key: key);
@override
MyFormState createState() => MyFormState();
}
class MyFormState extends State<MyForm> {
String results = "";
String hostIP = "192.168.0.108";
int port = 8000;
startServer() {
ServerSocket.bind(hostIP, port).then((serverSocket) {
serverSocket.listen((socket) {
socket.listen((s) {
results = utf8.decode(s);
});
});
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("TCP服务端"),
backgroundColor: Colors.red,
),
body: Center(
child: Column(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Flexible(
child: TextField(
decoration: const InputDecoration(hintText: "IP地址"),
keyboardType: TextInputType.number,
inputFormatters: [
FilteringTextInputFormatter.allow(RegExp("[0-9.]")),
],
onSubmitted: (String value) {
setState(() {
hostIP = value;
});
},
),
),
Flexible(
child: TextField(
decoration: const InputDecoration(hintText: "端口号"),
keyboardType: TextInputType.number,
inputFormatters: [
FilteringTextInputFormatter.digitsOnly,
],
onSubmitted: (String value) {
setState(() {
port = value as int;
});
},
),
),
],
),
Text(results)
],
),
),
);
}
}
1.2_TCP客户端
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: MyForm(),
);
}
}
class MyForm extends StatefulWidget {
const MyForm({Key? key}) : super(key: key);
@override
MyFormState createState() => MyFormState();
}
class MyFormState extends State<MyForm> {
String results = "";
String hostIP = "192.168.0.104";
String port = '8000';
startClient() {
Socket.connect(hostIP, port as int).then((socket) {
socket.write(results);
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("TCP客户端"),
backgroundColor: Colors.red,
),
body: Center(
child: Column(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Flexible(
child: TextField(
controller: TextEditingController.fromValue(TextEditingValue(text: hostIP)),
decoration: const InputDecoration(
labelText: 'IP地址'),
keyboardType: TextInputType.number,
inputFormatters: [
FilteringTextInputFormatter.allow(RegExp("[0-9.]")),
],
onSubmitted: (String value) {
setState(() {
hostIP = value;
});
},
),
),
Flexible(
child: TextField(
controller: TextEditingController.fromValue(TextEditingValue(text: port)),
decoration: const InputDecoration(
labelText: "端口号"),
keyboardType: TextInputType.number,
inputFormatters: [
FilteringTextInputFormatter.digitsOnly,
],
onSubmitted: (String value) {
setState(() {
port = value;
});
},
),
),
],
),
TextField(
decoration: const InputDecoration(hintText: "发送内容"),
onSubmitted: (String value) {
setState(() {
results = value;
});
},
),
ElevatedButton(
child: const Text('发送'),
onPressed: () {
startClient();
},
),
],
),
),
);
}
}
2_UDP