Java TCP Server 中输入和输出的单独线程

发布于 2024-10-27 10:20:09 字数 1311 浏览 0 评论 0原文

我的任务是用 Java 编写 TCP 服务器和客户端。我在确定服务器的总体结构时遇到了一些麻烦。服务器必须接受多个连接,但没有请求响应协议。一旦建立连接,客户端就可以向服务器发送数据,服务器也可以向客户端发送数据。鉴于此,我认为每个连接都有单独的输入和输出线程是一个好主意。我已经查看了很多示例,但大多数都非常简单,并且没有单独的输入和输出线程。我非常感谢任何帮助或建议。

到目前为止,我的 tcp 服务器有一个监听线程,用于监听客户端连接。建立连接后,我会为该连接创建一个输入线程和一个输出线程。

在 TCPServer 中,

while (true)
{
    SocketChannel clientChannel = m_serverChannel.accept();         
    new Thread(new ReadWorker(clientChannel)).start();
    new Thread(new WriteWorker(clientChannel)).start();
}

我应该如何传递要发送到输出线程的数据?我最初的想法是让输出线程监听待处理数据的队列,在数据可用时发送数据。

在 TCPServer 中

ConcurrentHashMap<SocketChannel, LinkedBlockingQueue<ByteBuffer>> pendingWrites 
        = new ConcurrentHashMap<SocketChannel, LinkedBlockingQueue<ByteBuffer>>(); 

void broadcast(ByteBuffer buffer) {
    synchronized(pendingWrites) {

    Iterator it = pendingWrites.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry pairs = (Map.Entry)it.next();
        ((LinkedBlockingQueue<ByteBuffer>) pairs.getValue()).put(buffer);
    }
}

WriteWorker

pendingWrites.get(socketChannel).take()
// send the data

这是一个合理的解决方案吗?

我遇到的另一个问题是,如果一个工作人员由于客户端断开连接而失败,我如何将其传达给另一个线程?如果一个线程出现故障,我想终止另一个线程。

谢谢

I've been tasked with writing a tcp server and client in Java. I'm having some trouble working out the general structure of my server. The server must accept multiple connections but there is no request response protocol. Once a connection is made, the client can send data to the server, and the server can send data to the client. Given this, I decided it would be a good idea for each connection to have seperate input and output threads. I've looked through lots of examples but most are quite simple and don't have separate threads for input and output. I'd greatly appreciate any help or advice.

So far, my the tcp server has a listening thread which listens for client connections. Once a connection is made, I create an input thread and an output thread for that connection.

In TCPServer

while (true)
{
    SocketChannel clientChannel = m_serverChannel.accept();         
    new Thread(new ReadWorker(clientChannel)).start();
    new Thread(new WriteWorker(clientChannel)).start();
}

How should I pass the data to be sent to the output threads? My initial thoughts were to have the output threads listen on a queue of pending data, sending the data as it becomes available.

In TCPServer

ConcurrentHashMap<SocketChannel, LinkedBlockingQueue<ByteBuffer>> pendingWrites 
        = new ConcurrentHashMap<SocketChannel, LinkedBlockingQueue<ByteBuffer>>(); 

void broadcast(ByteBuffer buffer) {
    synchronized(pendingWrites) {

    Iterator it = pendingWrites.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry pairs = (Map.Entry)it.next();
        ((LinkedBlockingQueue<ByteBuffer>) pairs.getValue()).put(buffer);
    }
}

In WriteWorker

pendingWrites.get(socketChannel).take()
// send the data

Is this a reasonable solution?

The other problem I'm having is if one worker fails due to client disconnection, how can I communicate this to the other thread? If there is a failure in one thread, I'd like to terminate the other.

Thanks

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

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

发布评论

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

评论(1

染年凉城似染瑾 2024-11-03 10:20:09

您不使用单独的输入和输出线程。您有一个套接字,您可以对其进行读写。

  • 接受新连接
  • 启动新连接处理程序线程并将该套接字交给它
  • 阻塞读取等待输入。
  • 处理输入,给出输出

如果你需要让服务器输出一些东西而不从客户端获取输入,你会用超时来阻止读取,或者使用Selector(看起来你正在使用nio)

我在此处的答案中给出了一个示例,可能会对您有所帮助: 检查 Java TCP 服务器上的客户端断开连接 - 仅输出

这没有使用新的 NIO 类,但显示了如何使用超时的阻塞读取。

You do not use separate input and output threads. You have a socket, you can read and write to it.

  • accept new connection
  • start new connection handler thread and hand that socket to it
  • blocking read waiting for input.
  • process input, give output

If you need to have the server output something without getting input from the client, you would blocking read with a timeout, or use a Selector (It looks like you're using nio)

I gave an example in an answer here on SO that may help you: Checking for a client disconnect on a Java TCP server - output only

This is not using the new NIO classes, but shows how you would use a blocking read with a timeout.

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