C++程序流程:对象中的套接字和主函数

发布于 2024-09-05 01:28:21 字数 517 浏览 10 评论 0原文

关于使用套接字的 C++ 程序流,我有一个相当棘手的问题。

基本上我所拥有的是:一个简单的命令行套接字服务器程序,它侦听套接字并一次接受一个连接。当该连接丢失时,它会打开进一步的连接。

该套接字通信系统包含在一个类中。该类完全能够接收连接并将接收到的数据镜像到客户端。然而,该类使用 UNIX 套接字,它不是面向对象的。

我的问题是,在我的 main() 函数中,我有一行 - 创建该对象的实例的一行。然后该对象初始化并等待。但是一旦获得连接,对象的初始化函数就会返回,当这种情况发生时,程序就会退出。如何在程序退出之前等待该对象被删除?

摘要:

  1. main() 创建对象实例
  2. 对象监听
  3. 已收到连接
  4. 对象的初始化函数返回
  5. main() 退出 (!)

我想要的是 main() 以某种方式延迟,直到该对象完成它正在做的事情(也就是它会删除自己),然后再退出。

有什么想法吗?

I have a rather tricky problem regarding C++ program flow using sockets.

Basically what I have is this: a simple command-line socket server program that listens on a socket and accepts one connection at a time. When that connection is lost it opens up for further connections.

That socket communication system is contained in a class. The class is fully capable of receiving the connections and mirroring the data received to the client. However, the class uses UNIX sockets, which are not object-oriented.

My problem is that in my main() function, I have one line - the one that creates an instance of that object. The object then initializes and waits. But as soon as a connection is gained, the object's initialization function returns, and when that happens, the program quits. How do I somehow wait until this object is deleted before the program quits?

Summary:

  1. main() creates instance of object
  2. Object listens
  3. Connection received
  4. Object's initialization function returns
  5. main() exits (!)

What I want is for main() to somehow delay until that object is finished with what it's doing (aka it will delete itself) before it quits.

Any thoughts?

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

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

发布评论

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

评论(5

驱逐舰岛风号 2024-09-12 01:28:21

如果您发布了一些代码,那么会更容易自信地回答,但这似乎很简单......在 main() 的内容周围添加一个 while(1) 循环:

int main(int, char **)
{
   while(1)
   {
      [...]
   }
}

It would be easier to answer with confidence if you posted some code, but it seems straightforward enough.... add a while(1) loop around the contents of your main():

int main(int, char **)
{
   while(1)
   {
      [...]
   }
}
最笨的告白 2024-09-12 01:28:21

有多种可能的解决方案,但如果没有进一步的信息,很难确定哪一种最适合您的情况。

我的第一个想法是将通信部分放在不同的线程中。这使得编程稍微复杂一些,但是当连接关闭时,只有线程终止。

第二个想法是使用 while 循环对 Jeremy Friesner 的想法进行变体。你似乎因为效率而反对它。我的建议是:首先构建一些可以运行的东西,然后再处理效率。为了节省资源,请在构造函数之后添加 sleep() 调用。

There are several possible solutions, but without further information it is hard to decide which one is the best in your situation.

My first idea would be to put the communication part in a different thread. This makes programming slightly more complicated, but when the connection closes, only the thread terminates.

A second idea would be a variation on Jeremy Friesner's idea with the while loop. You seem to object to it because of efficiency. My advise: build something that works first, deal with efficiency later. To save resources, put in a sleep() call after the constructor.

青衫负雪 2024-09-12 01:28:21

我想要的是 main() 以某种方式延迟,直到该对象完成它正在做的事情(也就是它会删除自己),然后再退出。

在我看来,这听起来像是该对象正在启动一个线程。

假设是这种情况,您有两种选择:

  1. 调用 pthread_join 等待对象启动的线程完成。
  2. 主要调用pthread_exit。这只会结束线程而不是整个进程。

What I want is for main() to somehow delay until that object is finished with what it's doing (aka it will delete itself) before it quits.

That sounds to me like the object is starting a thread.

Assuming that is the case, you have a couple of choices:

  1. Call pthread_join to wait for the thread started by the object to finish.
  2. Have main call pthread_exit. This will just end the thread instead of the entire process.
如此安好 2024-09-12 01:28:21

如果对象正在启动一个线程,也许 API 中有某种 .wait() 方法,或者您可以到达内部线程对象并进行 join()?

如果没有并且您无法更改 API,那么您将不得不等待一段时间(1)并在里面睡觉......并且可能看起来不太好。

看来您的 Network 类在您不知道或没有能力等待的情况下启动了一个线程。如果 Network 类确实可以完成您需要的一切,那么在其中使用线程有什么意义,因为您同时只有一个客户端?

If the object is starting a thread, maybe there is a chance there is some kind of .wait() method in the API or you can reach the inside thread object and make a join()?

If there isn't and you can't change the API, then you'll have to have a while(1) and a sleep inside.. and may not look very nice.

It seems your Network class starts a thread without you knowing or having the ability to wait for it. If the Network class does do everything you need, what the point of using threads inside it because you have only one client at the same time?

浸婚纱 2024-09-12 01:28:21

通常,服务器有一个侦听套接字,并且您循环接受客户端连接,为每个在单独线程中执行的连接创建一个新的连接处理类。

Classically a server has a listening socket and you loop accepting client connections, creating a new connection-handling class for each one that executes in a separate thread.

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