第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

NIO異常java.nio.BufferUnderflowException

NIO異常java.nio.BufferUnderflowException

德瑪西亞99 2019-03-01 10:38:37
package client; import java.io.DataInputStream; import java.io.IOException; import java.io.InputStream; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.SocketChannel; import java.util.concurrent.TimeUnit; public class NioClient { public static void main(String[]args){ ByteBuffer buffer = ByteBuffer.allocate(1024); SocketChannel socketChannel = null; ByteBuffer res=ByteBuffer.allocate(1024); try { socketChannel = SocketChannel.open(); socketChannel.configureBlocking(false); socketChannel.connect(new InetSocketAddress("localhost",8080)); if(socketChannel.finishConnect()) { int i=0; int size=0; while(true) { TimeUnit.SECONDS.sleep(1); String info = "I'm "+i+++"-th information from client"; buffer.clear(); buffer.put(info.getBytes()); buffer.flip(); while(buffer.hasRemaining()){ // System.out.println(buffer); socketChannel.write(buffer); } size=socketChannel.read(res); while((size)!=0){ res.flip(); byte result[]=new byte[size]; res.get(result); System.out.println(new String(result)); res.clear(); } } } } catch (IOException | InterruptedException e) { e.printStackTrace(); } finally{ try{ if(socketChannel!=null){ socketChannel.close(); } }catch(IOException e){ e.printStackTrace(); } } } } 解決方法:將while循環(huán)上的賦值放到while循環(huán)里 while((size=socketChannel.read(res))!=0){ 為什么會(huì)這樣,求解釋??? 另附贈(zèng)服務(wù)端代碼 package client; import java.io.*; import java.net.*; import message.IMessage; import message.LoginMessage; public class Server2 { public static void main(String[] args) throws IOException { Server2 socketService = new Server2(); socketService.oneServer(); } public void oneServer() { ServerSocket serverSocket = null; InputStream in=null; OutputStream out =null; try { System.out.println(" server start"); serverSocket = new ServerSocket(8080); int Size = 0; byte[] res = new byte[1024]; while(true){ Socket clntSocket = serverSocket.accept(); SocketAddress clientAddress = clntSocket.getRemoteSocketAddress(); System.out.println("Handling client at "+clientAddress); in = clntSocket.getInputStream(); out=clntSocket.getOutputStream(); int i=0; while((Size=in.read(res))!=-1){ byte[] temp = new byte[Size]; System.arraycopy(res, 0, temp, 0, Size); System.out.println(new String(temp)); String status="success"+i; i++; out.write(status.getBytes()); out.flush(); } } } catch (IOException e) { e.printStackTrace(); } finally{ try{ if(serverSocket!=null){ serverSocket.close(); } if(in!=null){ in.close(); } }catch(IOException e){ e.printStackTrace(); } } } }
查看完整描述

1 回答

  • 1 回答
  • 0 關(guān)注
  • 1855 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)