i made a client server echo program but it doesn't work, here is the client try{…
- User
i made a client server echo program but it doesn't work, here is the client try{ SocketAddress address = new InetSocketAddress(port); SocketChannel client = SocketChannel.open(address); client.configureBlocking(false);
code - Assistant
- User
i solved by changing the client to try{ SocketAddress address = new InetSocketAddress(port); SocketChannel client = SocketChannel.open(address); client.configureBlocking(false);
codeScanner scanner = new Scanner(System.in); System.out.println("Inserisci messaggio da inviare \n"); String input = scanner.nextLine(); ByteBuffer buffer = ByteBuffer.wrap(input.getBytes()); client.write(buffer); ByteBuffer readBuffer= ByteBuffer.allocate(1024); int numBytesRead; while(true){ numBytesRead = client.read(readBuffer); System.out.println("letto"); if(numBytesRead!=0){ break; } }; readBuffer.flip(); System.out.println("letti "+numBytesRead); byte[] bytes = new byte[readBuffer.remaining()]; readBuffer.get(bytes); System.out.println("Received message from server: " + new String(bytes)); } catch(IOException e){ e.printStackTrace(); } now there's a while that discards empty input and waits to read, is there a more elegant way to write this? - Assistant