在 C 语言中在两个服务器之间传递数据的最佳方法?

发布于 2024-09-28 18:18:16 字数 426 浏览 0 评论 0原文

我编写了一个程序,用 C 语言创建 TCP 和 UDP 套接字并启动两台服务器。应用程序的目标是监视 TCP 套接字上的请求,了解要发送哪些 UDP 数据包(即监视类似“0x01 0x02”的内容,如果看到它,则让 UDP 服务器解析有效负载,并将其转发到到 TCP 服务器进行处理)。问题是,UDP 服务器将忙于保持另一个设备的运行,实际上与该设备来回发送数千个数据包。那么,持续监控来自 TCP 服务器的请求,但在请求时从 UDP 服务器发送某些有效负载(因为 UDP 服务器会很忙)的最佳方法是什么?

我研究了带有信号量和/或互斥锁的pthreads(不过,不确定所有套接字操作都是线程安全的,以及这是否是处理它的正确方法)以及fork / pipeline。将 UDP 服务器分叉为子进程似乎很容易,但我不知道如何在两个服务器之间传递我需要的数据类型(需要来自 TCP 的请求数据和来自 UDP 的有效负载数据)。

I wrote a program that creates a TCP and UDP socket in C and starts both servers up. The goal of the application is to monitor requests over the TCP socket as to what UDP packets to send it (i.e. monitor for something like "0x01 0x02" and if I see it, then have the UDP server parse the payload, and forward it over to the TCP server for processing). The problem is, the UDP server will be busy keeping another device up, literally sending thousands of packets back and forth with this device. So what is the best way to continuously monitor requests from the TCP server, but send it certain payloads from the UDP server when requested since the UDP server will be busy?

I looked into pthreads with semaphores and/or mutex (not sure all the socket operations are thread safe, though, and if this is the right way to approach it) as well as fork / pipe. Forking the UDP server off as a child process seems easy enough, but I don't see exactly how I would be passing the kind of data I need among both servers (need request data from TCP and payload data from the UDP).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

ゝ杯具 2024-10-05 18:18:16

首先,将这两台服务器放入一个程序中是否有意义?如果是这样,您就不必在进程之间进行通信,并且整个逻辑变得更加容易。您必须考虑进行异步输入和输出,select() 函数就是为此而设计的。关于如何执行此操作会有很多解释,快速浏览一下会发现 此页面

但是,如果您必须有两个独立的进程,那么您将需要为进程间通信,其中有好几种,你的选择会受到你操作系统的影响。 管道(如果可用)可能是合适的,Unix 命名管道。或者您可以研究第三方消息传递框架,或者仅使用共享内存和/或信号量(但要非常小心!)。

Firstly, would it make sense to put these two servers into one program? If so, you won't have to communicate between processes, and the whole logic becomes substantially easier. You will have to think about doing asynchronous input and output, and the select() function is designed for just this. There will be many explanations around on how to do this, and a quick look finds this page.

However, if you must have two separate processes, then you will need to choose a mechanism for inter-process communication, of which there are several, and your choice will be affected by your operating system. A pipe, if available, might be suitable, as might a Unix named pipe. Or you could look into third-party message passing frameworks, or just use shared memory and/or semaphores (but be very careful!).

予囚 2024-10-05 18:18:16

你应该看的是libevent,你自己重新发明轮子编写这个低级代码的任何其他东西。这是教程GoogleKrugle

此外,您还应该在服务器之间使用一些预定义的协议。有很多可供选择。从极其简单的 XDR协议缓冲区

What you should look at is libevent, anything else you are reinventing the wheel writing this low level code yourself. Here is a Tutorial, Google, Krugle

Also you should use some predefined protocol between the servers. There are lots to choose from. Ranging from the extremely simple XDR to Protocol Buffers.

戏剧牡丹亭 2024-10-05 18:18:16

你可以在 Unix 上使用管道。请参阅http://tldp.org/LDP/lpg/node11.html

You could use pipes on Unix. See http://tldp.org/LDP/lpg/node11.html

初见 2024-10-05 18:18:16

嗯,您确实选择了一篇有趣的 C 简介!

您可以尝试共享内存。什么操作系统?

Well, you certainly picked an interesting introduction to C!

You might try shared memory. What OS?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文