服务器客户端应用程序,我无法想出一种在服务器端处理客户端数据的干净方法

发布于 2024-10-06 19:00:21 字数 316 浏览 4 评论 0原文

我有一个在 Linux 上用 C++ 编写的服务器客户端应用程序。当客户端连接到我的服务器时,服务器会生成一个线程,等待客户端发送要执行的服务器命令。这些命令取决于操作系统。客户端与服务器通信的线程调用执行客户端所需命令的全局函数。因此,对于客户端发送到服务器以执行的每个依赖于操作系统的命令,我必须有两个函数。所有这些全局函数都在主线程函数所在的同一标头中定义。针对不同操作系统的所有这些功能变得有点混乱。我的想法是编写两个名为 WindowsFuncs 和 LinuxFuncs 的类,它们具有静态成员函数,这些函数执行该类设计的操作系统所需的命令。关于如何清理我的逻辑,stackoverflows 有哪些想法?

I have a server client app written in C++ on Linux. When a client connects to my server, the server spawns a thread that waits for the client to send the server commands to execute. the commands are OS dependent. the thread that the client is talking to the server on, calls global functions that perform the required command that the client wants. So I have to have two functions for every OS depended command the client sends to the server to be executed. All of these global functions are defined in the same header that the main thread function is. It's getting a bit messy with all of these functions for different OS's. My idea is to write two classes that are called WindowsFuncs and LinuxFuncs that have static member functions that perform the required command for that OS the class was designed for. What are some of stackoverflows ideas on how to clean my logic?

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

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

发布评论

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

评论(1

£烟消云散 2024-10-13 19:00:21

这听起来不像是线程问题。
听起来你可以使用简单的继承。

使用类似

abstract class OSMethods {
  void listDir();
}

class OSMethodsLinux : OSMethods {
  void listDir() { system.exec("ls"); }
} 
class OSMethodsWin : OSMethods {
  void listDir() { system.exec("dir"); }
}

“Then 服务器客户端处理代码”的方法有

  void accept(Socket s, OSMethods m) {
     s.readCommand();
     m.listDir();  // or whatever
  }

“确保传递正确的实例来接受 Linux 或 Win 类”之类的方法。
所以没有静态方法。

一般来说,我发现你的程序中不需要静态方法(main 除外),除非你正在做聪明的事情,大多数事情只是不需要它们,并且它们会导致设计不太灵活。

This doesn't sound like a threading problem.
Sounds like you can use simple inheritance.

Using something like

abstract class OSMethods {
  void listDir();
}

class OSMethodsLinux : OSMethods {
  void listDir() { system.exec("ls"); }
} 
class OSMethodsWin : OSMethods {
  void listDir() { system.exec("dir"); }
}

Then server client processing code has method like

  void accept(Socket s, OSMethods m) {
     s.readCommand();
     m.listDir();  // or whatever
  }

Make sure you pass correct instance to accept of either Linux or Win class.
So no static methods.

Generally I've found that you will need no static methods in your programs (except main) unless you are doing clever stuff, most things just don't need them and they lead to less flexible design.

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