如何通过套接字将Dart/FLIFT与C语言连接?
我正在尝试在linux和C语言上使用FlatterforDesktop制作一个非常简单的应用程序。在前端(flatter),我希望通过计算机中的套接字向服务器(C语言)发送和接收消息。当我运行时,两个连接都已建立,但未发送或接收任何消息 其思想是让服务器在终端上连续运行,等待消息,并根据消息的不同,在终端上打印从前端发送的消息,或向前端发送消息或关闭服务器。我对插座的概念非常陌生,感谢您的帮助:) 这是前端(客户端)颤振代码:如何通过套接字将Dart/FLIFT与C语言连接?,c,flutter,sockets,dart,flutter-desktop,C,Flutter,Sockets,Dart,Flutter Desktop,我正在尝试在linux和C语言上使用FlatterforDesktop制作一个非常简单的应用程序。在前端(flatter),我希望通过计算机中的套接字向服务器(C语言)发送和接收消息。当我运行时,两个连接都已建立,但未发送或接收任何消息 其思想是让服务器在终端上连续运行,等待消息,并根据消息的不同,在终端上打印从前端发送的消息,或向前端发送消息或关闭服务器。我对插座的概念非常陌生,感谢您的帮助:) 这是前端(客户端)颤振代码: import 'package:flutter/material.d
import 'package:flutter/material.dart';
import 'dart:io';
import 'dart:typed_data';
void main() async {
final socket = await Socket.connect('localhost', 4242);
print('Connected to: ${socket.remoteAddress.address}:${socket.remotePort}');
runApp(MaterialApp(
home: HomeTest(socket: socket),
debugShowCheckedModeBanner: false,
title: "Test Socket",
));
socket.destroy();
}
class HomeTest extends StatefulWidget {
final Socket socket;
const HomeTest({Key key, this.socket}) : super(key: key);
@override
_HomeTestState createState() => _HomeTestState();
}
class _HomeTestState extends State<HomeTest> {
TextEditingController sendController = TextEditingController();
TextEditingController receiveController = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
body: Row(
children: <Widget>[
Expanded(
child: Padding(
padding: EdgeInsets.fromLTRB(20.0, 0.0, 20.0, 0.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
TextField(
style: TextStyle(fontSize: 20.0),
controller: sendController,
),
Divider(color: Colors.white),
ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.deepPurple
),
child: Text("Send message", style: TextStyle(color: Colors.white)),
onPressed: (){
/* tells the server that the client is going to send a message */
widget.socket.write("send");
/* sends a message to the server */
widget.socket.write(sendController.text);
sendController.text = "";
}
)
]
)
)
),
Expanded(
child: Padding(
padding: EdgeInsets.fromLTRB(20.0, 0.0, 20.0, 0.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
TextField(
style: TextStyle(fontSize: 20.0),
controller: receiveController,
readOnly: true,
),
Divider(color: Colors.white),
ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.deepPurple
),
child: Text("Receive message", style: TextStyle(color: Colors.white)),
onPressed: (){
/* tells the server that the client wants to receive a message */
widget.socket.write("receive");
setState(() {
/* receives a message from the server and shows it in the text field above */
widget.socket.listen(
(Uint8List data) {
final serverResponse = String.fromCharCodes(data);
receiveController.text = serverResponse;
}
);
});
}
)
]
)
)
),
Expanded(
child: Padding(
padding: EdgeInsets.fromLTRB(20.0, 0.0, 20.0, 0.0),
child: ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.deepPurple
),
child: Text("End connection", style: TextStyle(color: Colors.white)),
onPressed: (){
/* sends message to shut down the server */
widget.socket.write("close");
}
)
)
)
]
)
);
}
}
导入“包装:颤振/材料.省道”;
导入“dart:io”;
导入“dart:键入的_数据”;
void main()异步{
最终套接字=等待套接字.connect('localhost',4242);
打印('连接到:${socket.remoteAddress.address}:${socket.remotePort}');
runApp(材料应用程序)(
home:home测试(插座:插座),
debugShowCheckedModeBanner:false,
标题:“测试套接字”,
));
socket.destroy();
}
类HomeTest扩展了StatefulWidget{
最终插座;
consthometest({Key-Key,this.socket}):super(Key:Key);
@凌驾
_HomeTestState createState();
}
类HomeTestState扩展了状态{
TextEditingController sendController=TextEditingController();
TextEditingController-receiveController=TextEditingController();
@凌驾
小部件构建(构建上下文){
返回脚手架(
正文:世界其他地区(
儿童:[
扩大(
孩子:填充(
填充:来自LTRB(20.0,0.0,20.0,0.0)的边缘设置,
子:列(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
文本字段(
样式:TextStyle(fontSize:20.0),
控制器:发送控制器,
),
分隔器(颜色:颜色。白色),
升降按钮(
样式:ElevatedButton.styleFrom(
原色:深紫色
),
子项:文本(“发送消息”,样式:TextStyle(颜色:Colors.white)),
已按下:(){
/*告诉服务器客户端将要发送消息*/
widget.socket.write(“发送”);
/*向服务器发送消息*/
widget.socket.write(sendController.text);
sendController.text=“”;
}
)
]
)
)
),
扩大(
孩子:填充(
填充:来自LTRB(20.0,0.0,20.0,0.0)的边缘设置,
子:列(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
文本字段(
样式:TextStyle(fontSize:20.0),
控制器:接收控制器,
只读:对,
),
分隔器(颜色:颜色。白色),
升降按钮(
样式:ElevatedButton.styleFrom(
原色:深紫色
),
子项:文本(“接收消息”,样式:TextStyle(颜色:Colors.white)),
已按下:(){
/*告诉服务器客户端希望接收消息*/
widget.socket.write(“接收”);
设置状态(){
/*从服务器接收消息并在上面的文本字段中显示*/
widget.socket.listen(
(UINT8列表数据){
final serverResponse=String.fromCharCodes(数据);
receiveController.text=serverResponse;
}
);
});
}
)
]
)
)
),
扩大(
孩子:填充(
填充:来自LTRB(20.0,0.0,20.0,0.0)的边缘设置,
儿童:升降按钮(
样式:ElevatedButton.styleFrom(
原色:深紫色
),
子项:文本(“结束连接”,样式:TextStyle(颜色:Colors.white)),
已按下:(){
/*发送消息以关闭服务器*/
widget.socket.write(“关闭”);
}
)
)
)
]
)
);
}
}
这是服务器的C代码:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#define PORT 4242
#define BUFFER_LENGTH 4096
int main(void){
/* Client and Server socket structures */
struct sockaddr_in client, server;
/* File descriptors of client and server */
int serverfd, clientfd;
char buffer[BUFFER_LENGTH];
fprintf(stdout, "Starting server\n");
/* Creates a IPv4 socket */
serverfd = socket(AF_INET, SOCK_STREAM, 0);
if(serverfd == -1) {
perror("Can't create the server socket:");
return EXIT_FAILURE;
}
fprintf(stdout, "Server socket created with fd: %d\n", serverfd);
/* Defines the server socket properties */
server.sin_family = AF_INET;
server.sin_port = htons(PORT);
memset(server.sin_zero, 0x0, 8);
/* Handle the error of the port already in use */
int yes = 1;
if(setsockopt(serverfd, SOL_SOCKET, SO_REUSEADDR,
&yes, sizeof(int)) == -1) {
perror("Socket options error:");
return EXIT_FAILURE;
}
/* bind the socket to a port */
if(bind(serverfd, (struct sockaddr*)&server, sizeof(server)) == -1 ) {
perror("Socket bind error:");
return EXIT_FAILURE;
}
/* Starts to wait connections from clients */
if(listen(serverfd, 1) == -1) {
perror("Listen error:");
return EXIT_FAILURE;
}
fprintf(stdout, "Listening on port %d\n", PORT);
socklen_t client_len = sizeof(client);
if ((clientfd=accept(serverfd,
(struct sockaddr *) &client, &client_len )) == -1) {
perror("Accept error:");
return EXIT_FAILURE;
}
fprintf(stdout, "Client connected.\nWaiting for client message ...\n");
do {
/* Zeroing buffers */
memset(buffer, 0x0, BUFFER_LENGTH);
int message_len;
/* buffer value: 'send' or 'receive' or 'close' */
if((message_len = recv(clientfd, buffer, BUFFER_LENGTH, 0)) > 0) {
buffer[message_len - 1] = '\0';
/* client will send a message to the server */
if(strcmp(buffer,"send")){
memset(buffer, 0x0, BUFFER_LENGTH);
/* receive the message and print */
if((message_len = recv(clientfd, buffer, BUFFER_LENGTH, 0)) > 0) {
buffer[message_len - 1] = '\0';
printf("Message: %s", buffer);
}
}
/* client wants to receive a message from the server */
else if(strcmp(buffer,"receive")){
send(clientfd,"any message",13,0);
}
}
/* client sent a message to shut down the server */
} while(strcmp(buffer,"close"));
close(clientfd);
close(serverfd);
printf("Connection closed\n\n");
return EXIT_SUCCESS;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#定义端口4242
#定义缓冲区长度4096
内部主(空){
/*客户端和服务器套接字结构*/
客户端、服务器中的结构sockaddr_;
/*客户端和服务器的文件描述符*/
intserverfd,clientfd;
字符缓冲区[缓冲区长度];
fprintf(stdout,“启动服务器\n”);
/*创建IPv4套接字*/
serverfd=socket(AF\u INET,SOCK\u STREAM,0);
如果(serverfd==-1){
perror(“无法创建服务器套接字:”);
返回退出失败;
}
fprintf(stdout,“使用fd创建的服务器套接字:%d\n”,serverfd);
/*定义服务器套接字属性*/
server.sinu family=AF\u INET;
server.sin_port=htons(端口);
memset(server.sin_zero,0x0,8);
/*处理已在使用的端口的错误*/
int yes=1;
如果(设置套接字选项)(服务器FD、SOL_套接字、SO_REUSEADDR、,
&是,sizeof(int))=-1){
perror(“套接字选项错误:”);
返回退出失败;
}
/*将套接字绑定到端口*/
if(绑定(serverfd,(struct sockaddr*)&server,sizeof(server))=-1){
佩罗尔