监听时在 Socket 上发送
我使用网络中的简单示例(用 JAVA)编写了一个套接字。 据我所知,我可以同时发送或收听。
是否有可能我可以发送/收听/接收全双工?
非常感谢。
I have programmed (in JAVA) a socket using simple examples in the web.
I can either send or listen at one time - as far as I understood.
Isn't there a possibility where I can send/listen/receive full duplex?
Thank you very much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用两个不同的线程发送和接收。
如果你有一个线程阅读,你可以有另一个写作。如果使用阻塞套接字,则读取线程需要一直等待。当你有东西要发送时,你只需要一个写作线程。
You can send and receive using two different threads.
If you have one thread reading you can have another writing. The reading thread need to be waiting all the time if you use a blocking socket. You only need a writing thread when you have something to send.
TCP/IP 通道本质上是全双工的。您只需使用另一个线程(在客户端和服务器端)来检查数据的可用性。
我想与大家分享代码仓库。这真的很简单,你可以得到让你的东西发挥作用的想法。这是一个详细的例子。这些步骤意外地看起来像 Ordous 的解决方案。
https://github.com/khanhhua/full-duplex-chat
随意克隆!这是我周末的作业。
The TCP/IP channel is full duplex in its nature. You just have to use another thread (on the client side as well as server side) to check the availability of data.
I would like to share with you guys the code repo. It is really simple, you can get the idea to make your stuff work. It is an elaborate example. The steps accidentally look like Ordous's solution.
https://github.com/khanhhua/full-duplex-chat
Feel free to clone! It's my weekend homework.