(线程新手)构建即时消息应用程序的最佳方法?

发布于 2024-10-20 19:56:01 字数 532 浏览 2 评论 0原文

我已经用Java编程有一段时间了,但以前从未处理过线程。在我的一门课程中,我们了解了易于使用但功能强大的 SocketServerSocket 类。我们制作了可以在同一台机器上相互通信的程序(使用“localhost”)。有两个类,Server.java 和 Client.java,但它们只能以回合制方式相互通信。

我想将 socket.accept() 方法放在一个新线程上,以便服务器和客户端程序都可以在 socket.accept() 等待时发送消息新消息。

到目前为止,我在线程方面做得很好,但我无法想到将 ServerSocket/Socket 对象放在哪里。如果我将它们放入等待传入消息的线程中,则无法将该对象用于传出消息。如果我将 ServerSocket/Socket 对象直接放入 Server.java/Client.java 文件中,我无法(或者我只是不知道如何)将对象传递给线程,而不创建它的副本。

关于如何构建这个程序有什么建议吗?

谢谢, 德里克

I have been programming in Java for a while, but have never dealt with threads before. In one of my classes, we learned about the easy-to-use yet vastly powerful Socket and ServerSocket classes. We made programs that could talk to each other through on the same machine (using "localhost"). There were two classes, a Server.java and a Client.java, but they could only talk to each other in a turn-based fashion.

I'd like to put the socket.accept() method on a new thread so the Server and Client programs can both send message while socket.accept() is waiting for new messages.

I have done well with threads so far, but I am unable to think of where to put the ServerSocket/Socket objects. If I put them in the thread that waits for incoming messages, I can't use that object for outgoing messages. If I put the ServerSocket/Socket objects in the Server.java/Client.java file directly, I can't (or I just don't know how) pass in the object to the thread, without creating a copy of it.

Any suggestions on how I should structure this program?

Thanks,
Derek

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

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

发布评论

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

评论(1

笨笨の傻瓜 2024-10-27 19:56:01

我想说,运行此应用程序的最佳方式是作为两个单独的进程/虚拟机,而不是两个线程。尽管您目前在同一台(虚拟)机器上运行它们,但即时消息传递和套接字的全部目的是允许不同进程(可能位于不同机器上)之间进行通信。您已经拥有这两个类 - 只需确保它们都有 main() 方法,以便它们可以单独运行。

另外,如果您想要全双工通信,每个进程需要两个线程 - 一个等待用户的输入并将其发送到另一端,另一个等待来自另一端的消息并将其呈现给用户。虽然您可以设置一个半双工系统,其中令牌在单个套接字上来回传递,但如果您为每一端设置一个传入套接字和一个传出套接字,您可能会发现它会容易得多。

如果你想从两端进行全双工通信,我认为这并不是真正的客户端/服务器关系。如果双方能够平等地交谈,他们就真的是同行了。无论如何,要有一个监听传入消息的“服务器”类,和一个发送传出消息的“客户端”类,但每台机器上都有一个。它们与发送和接收线程非常紧密地映射。

I would say that the best way of running this application is as two separate processes/virtual machines, rather than two threads. Although you're running them on the same (virtual) machine at the moment, the whole point of instant messaging - and sockets - is to allow communication between different processes, which may be on different machines. You already have the two classes - simply make sure that both have main() methods so that they can run on their own.

Also, if you want full-duplex communication, you'd need to have two threads per process - one to wait for input from the user and send it to the other side, the other to wait for messages from the other side and present them to the user. Although you could set up a half-duplex system where a token is passed back and forwards on a single socket, you will likely find it much easier if you set up one incoming socket and one outgoing socket per end.

If you want to do full duplex communication from both ends, I don't think this is really a client/server relationship. If both sides are able to talk to each other equally, they're really more peers. By all means have a 'server' class that listens for incoming messages, and a 'client' class that sends outgoing ones, but have one of each on every machine. These map quite closely to the sending and receiving threads.

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