简体   繁体   English

Java,NIO,具有读写功能的一个线程

[英]Java, NIO, one thread with reading and writing

I try to build simple client application sending data to server and receiving data from it . 我尝试构建简单的客户端应用程序,将数据发送到服务器并从中接收数据。 In most of time it'll asynchronic exchange data (beside logging process) 在大多数情况下,它会异步交换数据(在日志记录过程之外)

I've read a lot of tutorials, posts etc. but I can't find enough description for me (i'm newbe in java) how to construct code for: 我已经阅读了很多教程,文章等,但是我找不到足够的描述给我(我是Java的新人)如何为以下代码构造代码:

  • reading/writing (simple line -string with "\\n" at the end), 读/写(简单的-string字符串,末尾带有“ \\ n”),
  • reconnecting (if errors found) 重新连接(如果发现错误)

client with server (I've wrote it in another development environment), all in one thread? 服务器的客户端(我已经在另一个开发环境中编写了它),都在一个线程中?

Could somebody help me by giving simple example or addreess where can I find out about simple using NIO in presented purposes? 有人可以举一个简单的例子或地址帮助我,在哪里可以找到有关在目的中使用NIO的简单信息? All examples I found treated only for reading or writing (not mixed) or simple connect, send data and disconnect without explanation exception, reconnection. 我发现的所有示例仅用于读取或写入(不混合)或简单连接,发送数据和断开连接而无任何解释异常,重新连接。

This is not really an NIO problem, it's just an inter-thread communications problem. 这并不是一个真正的NIO问题,而仅仅是一个线程间通信问题。

The second thread will get the same exception the first one did, and it can look at a reconnectPending boolean that can be set by the first thread while it reconnects the socket. 第二个线程将获得与第一个线程相同的异常,它可以查看第一个线程在重新连接套接字时可以设置的reconnectPending布尔值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM