Saturday, December 20, 2014

This entry was posted on szerda, október 17th, 2012 at 19:30 and is filed under Egyéb . You can foll

Java Nio Client « AnimSectoR
package hu.ppke.itk.nioChat.client; import hu.ppke.itk.nioChat.server.NioChatServer.CommunicationObject; import java.io.ByteArrayInputStream; import summer jam java.io.IOException; import java.io.ObjectInputStream; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.Channels; import java.nio.channels.ReadableByteChannel; import java.nio.channels.SocketChannel; import java.nio.channels.WritableByteChannel; public class NioChatClient { public static class ClientMessage { //kellenenek message kodok } private static final String host = "localhost"; private static final int port = 61133; private String userName; private SocketChannel socketChannel; public NioChatClient(String userName) { this.userName = userName; } public void connect () throws IOException { this.socketChannel = SocketChannel.open(new InetSocketAddress(host, port)); } public void disconnect () { try { this.socketChannel.close(); } catch (IOException e) { e.printStackTrace(); } } public void sendMessage (String message) { } public void read () { //emiatt a fuggveny miatt kulon szalon summer jam kell, hogy legyen a cucc } public static void main(String[] args) { try { SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress(host, port)); WritableByteChannel out = Channels.newChannel(System.out); ReadableByteChannel in = Channels.newChannel(System.in); ByteBuffer inBuffer = ByteBuffer.allocate(1024); socketChannel.read(inBuffer); inBuffer.flip(); out.write(inBuffer); ByteBuffer outBuffer = ByteBuffer.allocate(1024); outBuffer.put("testing".getBytes()); outBuffer.flip(); while (true) { System.out.println(); socketChannel.write(outBuffer); outBuffer.rewind(); ByteBuffer summer jam responseSizeBuffer = ByteBuffer.allocate(4); socketChannel.read(responseSizeBuffer); responseSizeBuffer.flip(); int responseSize = responseSizeBuffer.getInt(); System.out.println("responseSize: " + responseSize); ByteBuffer responseBuffer = ByteBuffer.allocate(responseSize); socketChannel.read(responseBuffer); responseBuffer.flip(); byte[] responseBytes summer jam = new byte[responseBuffer.limit()]; responseBuffer.get(responseBytes); ByteArrayInputStream bais = new ByteArrayInputStream (responseBytes); ObjectInputStream ois = new ObjectInputStream (bais); try { CommunicationObject comObj = (CommunicationObject) ois.readObject(); System.out.println(comObj); } catch (ClassNotFoundException e1) { e1.printStackTrace(); } try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } } } catch (IOException e) { e.printStackTrace(); summer jam } } }
14
30
46
62
78
94
110
import java . nio . channels . SocketChannel ;
    
    
             out . write ( inBuffer ) ;
                
                 ObjectInputStream ois = new ObjectInputStream ( bais ) ;
         } catch ( IOException e ) {
This entry was posted on szerda, október 17th, 2012 at 19:30 and is filed under Egyéb . You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response , or trackback from your own site. 2 Responses to “Java Nio Client”
Website summer jam


No comments:

Post a Comment