首页 \ 问答 \ 使用java中的非阻塞I / O发送消息(NIO API)(Sending message using non-blocking I/O in java(NIO API))

使用java中的非阻塞I / O发送消息(NIO API)(Sending message using non-blocking I/O in java(NIO API))

我正在编写客户端向服务器发送文本消息的服务器/客户端程序。我使用了非阻塞I / O(NIO API),但服务器上的消息无法正确显示。这是我在服务器上的代码:

private JTextArea displayArea;
private int numBytes;
private ByteBuffer buffer;
/*...
some code is here
...*/
displayArea = new JTextArea();
add(new JScrollPane(displayArea), BorderLayout.CENTER);
setSize(400, 500);
setVisible(true);
/*...
some code is here
...*/
buffer = ByteBuffer.allocate(20);
buffer.clear();
displayArea.append("reading data...");
do{
   numBytes = socketChannel.read(buffer);
}while(numBytes == -1);
displayArea.append("\nData read.");
buffer.flip();
int usedBytes = buffer.position();
byte[] bufferArray = buffer.array();
String message = new String(bufferArray, 0, usedBytes);
displayArea.append("\n"+message);

这是一段客户端代码:

byte[] byteData = message.getBytes();
buffer.put(byteData);
socketChannel.write(buffer);
buffer.clear();

在客户端向服务器发送消息的运行时,显示空格字符或一条消息。


I'm writing a server/client program that clients send text message to server.I have used non-blocking I/O (NIO API) but messages on the server do not display correctly.this is my code on server:

private JTextArea displayArea;
private int numBytes;
private ByteBuffer buffer;
/*...
some code is here
...*/
displayArea = new JTextArea();
add(new JScrollPane(displayArea), BorderLayout.CENTER);
setSize(400, 500);
setVisible(true);
/*...
some code is here
...*/
buffer = ByteBuffer.allocate(20);
buffer.clear();
displayArea.append("reading data...");
do{
   numBytes = socketChannel.read(buffer);
}while(numBytes == -1);
displayArea.append("\nData read.");
buffer.flip();
int usedBytes = buffer.position();
byte[] bufferArray = buffer.array();
String message = new String(bufferArray, 0, usedBytes);
displayArea.append("\n"+message);

And this is a piece of client code:

byte[] byteData = message.getBytes();
buffer.put(byteData);
socketChannel.write(buffer);
buffer.clear();

In run time when a client send message to server , space characters or a piece of message is shown.


原文:https://stackoverflow.com/questions/26072926
更新时间:2023-05-06 14:05

相关问答

更多
  • 另一种使用类方法或通过名称调用类(如其他人所示)的方法是使用闭包来保存对函数的引用: class Foo(object): def bar(): def bar(n): if n == 0: return "bar" return bar(n-1) return bar bar = staticmethod(bar()) (次要的)优势在于,当名称更改时,这样做不太容易中断。 例如 ...
  • 那是因为staticmethods真的不是方法。 staticmethod描述符按原样返回原始函数。 没有办法获得访问该函数的类。 但是无论如何都没有真正的理由使用static方法,总是使用classmethods。 我在staticmethods中找到的唯一用途是将函数对象存储为类属性,而不是将它们转换为方法。 That's because staticmethods really aren't methods. The staticmethod descriptor returns the origin ...
  • 你好像在使用python 3.在python 2中: In [1]: class Foo(object): ...: def f_standalone(x): ...: print("standalone version of f, x={}".format(x)) ...: In [2]: Foo.f_standalone(12) ------------------------------------------------------------------ ...
  • staticmethod不可能。 但是,使用classmethod是可能的 class A(object): @classmethod def f(cls): print cls not possible with staticmethod. is possible, however, with classmethod class A(object): @classmethod def f(cls): print cls
  • 使用staticmethod方法的原因是,如果你有一些可以作为独立函数(不是任何类的一部分)编写的东西,但是你想保留在类中,因为它在语义上与类相关。 (例如,它可能是一个函数,它不需要来自类的任何信息,但其行为是特定于该类的,因此子类可能需要覆盖它。)在许多情况下,它可能同样有意义将某些东西作为独立函数而不是静态方法来编写。 你的例子并不完全相同。 一个关键的区别是,即使你不使用self ,你仍然需要一个实例来调用static_add_one ---你不能直接在类test2.static_add_one(1 ...
  • 这是一篇关于静态方法的文章 。 综上所述: 实例方法 :将实例作为第一个参数 类方法 :将类作为第一个参数 静态方法 :不需要作为第一个参数 关于你的问题: 是。 虽然变量名self是约定,但它与实例有关。 静态方法可用于对同一类下的类似实用程序方法进行分组。 对于类中的方法,您需要将self添加为第一个参数,或者使用@staticmethod修饰它的方法。 没有参数的“非修饰方法”会引发错误。 通过参数调用时,可以更清楚地看到这些是如何工作的。 修改后的例子: class TestClass: ...
  • 这就是你应该怎么做的。 在其他编程语言中,您每次都必须使用static关键字。 That's how you are supposed to do it. In other programming languages you have to use the static keyword everytime, too.
  • 如果你真的有静态方法,一定要使用@staticmethod 。 @staticmethod不仅仅记录你的意图,它实际上改变了你的方法的行为。 它会停止绑定到实例的方法的第一个变量。 使用@staticmethod : >>> class Foo(object): ... def __init__(self, name): ... self.name = name ... @staticmethod ... def bar(argument): ... ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。