使用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
相关问答
更多-
TCP/IP模型是一个________。[2023-05-19]
a -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
静态方法和递归?(staticmethod and recursion?)[2022-04-24]
另一种使用类方法或通过名称调用类(如其他人所示)的方法是使用闭包来保存对函数的引用: 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): ... ...