在C / C ++中实现跨平台,多线程服务器的最好方法是什么? [英] What is the best way to implement a cross-platform, multi-threaded server in C/C++?

查看:307
本文介绍了在C / C ++中实现跨平台,多线程服务器的最好方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与我合作的开发团队的一部分已经面临编写服务器以与我们的产品集成的挑战。我们有一些提供C SDK的低级传感器设备,我们想通过网络共享它们,以供收集数据的人使用。听起来很简单,对吧?有人会在建筑物的一部分中将传感器设备连接到他们的机器,并运行我们的服务器,从而与网络的其余部分共享设备。然后客户端将通过我们的应用程序连接到该服务器,并从设备收集传感器读数。

Part of the development team I work with has been given the challenge of writing a server for integration with our product. We have some low-level sensor devices that provide a C SDK, and we want to share them over a network for use by people collecting data. Sounds simple, right? Someone would connect a sensor device to their machine in one part of the building and run our server, thus sharing the device(s) with the rest of the network. Then a client would connect to that server via our application and collect sensor readings from the device.

我创建了一个简单的,语言无关的网络协议, Java。问题是创建一个实现,它将与我们的设备一起使用,只提供以C语言编写的SDK。我们正在考虑执行以下操作:

I created a simple, language-agnostic network protocol, and a reference implementation in Java. The problem is creating an implementation that will work with our devices that only provide an SDK written in C. We were thinking of doing the following:



  1. 使用多线程服务器将每个到达工作线程的传入连接分离出来。
  2. $ < b $ b
  3. 当工作线程接收到传感器读取请求时,轮询线程收集的最新值将返回给客户端。

  1. Create polling threads that collect and store the most recent readings from each connected device.
  2. Use a multi-threaded server to spin off each incoming connection to a worker thread.
  3. When a worker thread receives a request for a sensor reading, the most recent value collected by the polling thread is sent back to the client.

这是很多线程,特别是在C.所以,审查,一般要求是:

That's a lot of threading, especially in C. So, to review, the general requirements are:


    <在Windows XP / Vista,Linux和OS X机器上
  • 用C或C ++编写,与C SDK交互

  • 接受可变数目的并发连接(工作线程)

  • 必须使用线程,而不是分叉(不想处理另一层IPC)

  • Runs on Windows XP/Vista, Linux, and OS X machines
  • Written in C or C++, to interact with the C SDK we have
  • Accepts a variable number of simultaneous connections (worker threads)
  • Must use threads, not forking (don't want to deal with another layer of IPC)

任何人都可以建议一个库,最好是一些示例代码来开始使用。

Can anyone suggest a library and preferably some example code to get use started?

推荐答案

p>我使用了 Boost.Thread & ; Boost.Asio 来构建多线程服务器在Windows& Linux系统。这些教程让您轻松上手。

I've used Boost.Thread & Boost.Asio to build a multi-threaded server on Windows & Linux systems. The tutorials made it easy to get started.

这篇关于在C / C ++中实现跨平台,多线程服务器的最好方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆