简体   繁体   English

带Linux的TCP客户端/服务器

[英]TCP Client/Server with Linux

This may be a very basic question/design but I am struggling with the correct method to handle the system I am going to define here. 这可能是一个非常基本的问题/设计,但是我正在努力使用正确的方法来处理我将在此处定义的系统。

I have a system with a single client (PC) that will connect to an embedded Linux board (Raspberry Pi) via TCP/IP protocol. 我有一个带有单个客户端(PC)的系统,该系统将通过TCP / IP协议连接到嵌入式Linux板(Raspberry Pi)。 This will be a command/response system where the PC will ask for something and the raspberry PI will respond with the results. 这将是一个命令/响应系统,在该系统中,PC会询问一些信息,而树莓派PI将对结果做出响应。

For Example: 例如:
CMD => Read/Return ADC Channel X CMD =>读取/返回ADC通道X

RSP => ADC Channel X Data RSP => ADC通道X数据

For this type of system I have already defined a packet protocol that will allow for this interaction. 对于这种类型的系统,我已经定义了一种包协议,该包协议将允许这种交互。 My problem is how to handle this on the Raspberry PI. 我的问题是如何在Raspberry PI上处理此问题。 I envision having a single thread handling the TCP connection; 我设想只有一个线程来处理TCP连接。 placing incomming data into a thread safe queue and pulling outgoing data from a thread safe queue. 将传入数据放入线程安全队列中,并从线程安全队列中提取传出数据。 Then the main thread would poll the queue periodically looking for data. 然后,主线程将定期轮询队列以查找数据。 When data is found the command would be processed and a response will be generated. 找到数据后,将对命令进行处理并生成响应。 All commands have a response. 所有命令都有响应。

The main thread will also be doing other time critical tasks (PID control loop) so it cannot wait for incoming or outgoing data. 主线程还将执行其他时间紧迫的任务(PID控制循环),因此它无法等待传入或传出的数据。

My guess is this type of system is fairly common and there is probably a good approach to implementing this type of system. 我的猜测是这种类型的系统相当普遍,并且可能有一种实现此类系统的好方法。 I am very new to Linux programming but I have been programming highly embedded systems (No OS) forever. 我对Linux编程非常陌生,但是我一直在对高度嵌入式的系统(无OS)进行编程。 Just struggling with the correct approach for this type of design. 只是在为这种类型的设计寻找正确的方法。

Note I chose TCP/IP because it handles retying in case of failure. 注意我选择了TCP / IP,因为它可以在发生故障的情况下处理重新输入。 In my case every command has a response so UDP could be used if it makes the design easier/more flexible. 就我而言,每个命令都有响应,因此如果使设计更容易/更灵活,则可以使用UDP。

Any help is greatly appreciated. 任何帮助是极大的赞赏。

I tend to avoid threads if I can and only use them if I have to because they make debugging the program harder. 如果可能的话,我倾向于避免使用线程,仅在必要时才使用它们,因为它们会使调试程序变得更加困难。 They turn a determinsitic problem into a non-deterministic one. 他们将确定性问题转变为非确定性问题。 So my initial approach would be to see if I can do this without a thread and still achieve concurrency. 因此,我的最初方法是查看是否可以在没有线程的情况下执行此操作,并且仍然实现并发。 This is possible using select which will notify your main program when there is something on the socket that needs to be read. 使用select可以做到这一点,当套接字上需要读取某些内容时,它将通知您的主程序。 Then, when there is something on the socket, it can read the data, process it, and wait for the next event. 然后,当套接字上有东西时,它可以读取数据,对其进行处理并等待下一个事件。 Problems with this approach is if the computation on the received data takes longer than the acceptable time wanted to process the next element of data, you could end up with a backlog of unprocessed data on the socket. 这种方法的问题是,如果对接收到的数据进行的计算所花费的时间超过了处理下一个数据元素所需的可接受时间,那么您最终可能会在套接字上积压未处理的数据。 If this is going to happen then you can go ahead and run the receive loop in thread, and the work function in another thread, or fork a new process and deal with a copy of the data from the new process. 如果这将发生,那么您可以继续在线程中运行接收循环,在另一个线程中运行工作函数,或者派生一个新进程并处理来自新进程的数据副本。

the ultra classic linux approach is to have a listener program that forks a new copy of itself for each new client. 超经典的linux方法是拥有一个侦听器程序,该程序为每个新客户端派生自己的新副本。 Linux even has a built in demon that does that for you (initd - although that might have changed with all the systemd stuff). Linux甚至有一个内置的恶魔可以为您做到这一点(初始化-尽管所有systemd的东西可能都已改变)。 Thats how sshd, telnetd, ftpd all work. 那就是sshd,telnetd,ftpd都如何工作的方式。 No threads 没有线程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM