bytebuffer を介して 2 つの整数と 1 つの文字列を送信する必要があります。そして、bytebuffer を介して別の文字列を受け取ります。
次のようなものが必要です: x (int) を keyboard.Enter から読み取ります。キーボードから y (int) を読み取ります。キーボードから文字列を読み取ります。入る。すべての値はどこかに行き、処理されており、文字列を受け取る必要があります。すべてはソケット チャネルを介して行われます。
私はこのようなことをしました: SEND
Scanner scanner=new Scanner(System.in);
int m,n;
String str,result;
System.out.println("x=");
m=scanner.nextInt();
System.out.println("y=");
n=scanner.nextInt();
System.out.println("String: ");
str=scanner.next();
ByteBuffer bb=ByteBuffer.allocate(100);
bb.putInt(0,m).putInt(8,n).put(str.getBytes()); // problem?
try{
sc.write(bb);
bb.clear();
sc.read(bb);
CharBuffer cbuf = bb.asCharBuffer();
result=cbuf.toString();
System.out.println("Result : "+result);
受け取る:
ByteBuffer bb = ByteBuffer.allocate(100);
socketChannel.read(bb);
int m=bb.getInt(0);
int n=bb.getInt(8);
String str=bb.toString().substring(16);
App app=new App();
String result=app.longestRepeatingSubstring(str,m,n);
bb.clear();
bb.put(result.getBytes());
socketChannel.write(bb);
socketChannel.close();
しかし、私はヌル文字列を受け取っています...
または、直接 bb.toString() を配置すると、次のようなものを受け取ります: java.nio.HeapByteBuffer[pos=237 lim=258 cap=798]