从套接字通道读取字符串

插座M

他,

我有以下代码:

    public AppThread(SocketChannel socketChannel){
    this.socketChannel=socketChannel;
  }

      public void run(){
            try{
              ByteBuffer bb = ByteBuffer.allocate(11);    
              socketChannel.read(bb);
              //byte[] b = new byte[bb.capacity()];
             // bb.get(b, 0, 11);
              System.out.println(bb.toString());
              byte[] a = new byte[11];
              CharBuffer cb = bb.asCharBuffer();
              System.out.println(cb);
              bb.get(a);

              App app=new App();
             // String an = new String(b);
              //String zodie = Zodie.getZodie(an);
              //b = new byte[zodie.length()];
              //b = zodie.getBytes();
              bb.clear();
              //bb.put(b);
              socketChannel.write(bb);
              socketChannel.close(); 
            }
            catch(IOException e){
               System.err.println("Server comunication error : "+e.getMessage());
            }  
          }
}

zodie static方法接收一个字符串并返回一个字符串。

如何将字符串写入SocketChannel,以将其作为参数传递给十二生肖静态方法。

我提到在客户端,我发送了一个字节数组,我已经检查了,没关系。

客户端:

  byte[] a = an.getBytes();
        System.out.println(new String(a));
    ByteBuffer bb=ByteBuffer.allocate(11);
    // Varianta 1
    bb.put(a);
    // Varianta 2
    // LongBuffer lb=bb.asLongBuffer();
    // lb.put(0,m).put(1,n);
    try{
      sc.write(bb);
      bb.clear();
      sc.read(bb);
      // Varianta 1
      //a = new byte[bb.remaining()];
      zodie=bb.toString();
      // Varianta 2
      // r=lb.get(0);
      System.out.println("Zodia : "+ zodie);
      sc.close();

真挚地,

我收到服务器错误:

Server ready... 
    java.nio.HeapByteBuffer[pos=1 lim=11 cap=11]

    Exception in thread "pool-1-thread-1" java.nio.BufferUnderflowException
        at java.nio.HeapByteBuffer.get(Unknown Source)
        at java.nio.ByteBuffer.get(Unknown Source)
        at server.AppThread.run(AppThread.java:27)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at java.lang.Thread.run(Unknown Source)
插座M

问题出在客户端。我已经使用了charbuffer作为包装器

Scanner scanner=new Scanner(System.in);
String m;
System.out.println("Enter the date (yyyy-mm-dd) :");
m=scanner.next()+" ";

CharBuffer  c = CharBuffer.wrap(m);
System.out.println("Sending date ...: " + c);
ByteBuffer b = Charset.forName("ISO-8859-1").encode(c);
 b.compact();
System.out.println("Bytebuffer has the capacity of "+ 
b.capacity() + "pointer position on bytebuffer is on: "+ b.position() + " and the limit is:" + b.limit());
b.flip();

在服务器端:

CharBuffer  c;
    ByteBuffer bb = ByteBuffer.allocate(11);    
     System.out.println("Server allocated a number of 11 octets to ByteBuffer");

    socketChannel.read(bb);
    bb.flip();//sets the Position to 0 and limit to the number of bytes to be read.
    CharBuffer c = Charset.forName("ISO-8859-1").decode(bb);
           System.out.println("Got " + c);

    byte[] byteArray = new byte[11];
    bb.get(byteArray);
    System.out.println("Server got from client the string: " +new String(byteArray));
    String an = new String(byteArray);
     bb.clear();

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

通过NetworkStream(套接字)写入/读取字符串以进行聊天

来自分类Dev

如何在Swift中通过TCP套接字读取字符串

来自分类Dev

从套接字读取到字符串

来自分类Dev

读取字符串,直到换行

来自分类Dev

Lua读取字符串的开头

来自分类Dev

从文件C ++读取字符串

来自分类Dev

在Haskell中读取字符串

来自分类Dev

使用sscanf读取字符串

来自分类Dev

在C ++中读取字符串

来自分类Dev

从用户读取字符串

来自分类Dev

从BufferReader读取字符串

来自分类Dev

在Haskell中读取字符串

来自分类Dev

在C ++中读取字符串

来自分类Dev

JOptionPane不读取字符串

来自分类Dev

从文件中读取字符串

来自分类Dev

从指针数组读取字符串

来自分类Dev

从 csv 读取字符串值

来自分类Dev

python从套接字搜索字符串

来自分类Dev

套接字不接受字符串

来自分类Dev

从尚未关闭的服务器套接字读取所有字符串

来自分类Dev

使用一行telnet语句时,C套接字读取输出空字符串

来自分类Dev

读取整数后C无法读取字符串

来自分类Dev

C++ 从套接字中读取一个字符串,在结尾之前包含空字符

来自分类Dev

-读取字符串字符时出错

来自分类Dev

在C中逐字符读取字符串

来自分类Dev

ifstream错误从字符串中读取字符

来自分类Dev

在C中逐字符读取字符串

来自分类Dev

字符串的C ++错误读取字符

来自分类Dev

以二进制模式读取任何文件,将其附加到字符串,并通过 TCP 套接字发送